Add Custom CSS to a Page or Post (With and Without Plugins)

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
3 (60%) 2 votes

Recently, I had to add some custom CSS to a certain page and I found it useful to share how to do it with and without plugins.

There are many ways to conditionally enqueue a stylesheet to a certain page or post. I’ll explain how to do that by using a simple plugin and by writing a snippet for the people who want to do their stuff without plugins.

Add Custom CSS to a Page or Post using a Plugin

I’ve found this super easy plugin, WP Add Custom CSS. You can use it to add custom CSS to the whole website. Also, you can use it to add conditional styles to individual posts, pages, and custom post types.

Once installed and activated, you will find a custom CSS box added to your dashboard when you edit any of your posts or pages.

page builder banner

Add Custom CSS to a Page or Post Without Plugins

The code I used is a combination of wp_enqueue_style and Conditional Tags. Simply, you enqueue the style if the conditional tag is met such as page ID, post ID, page template etc.

First, you will need to add your custom CSS to a .css file. Preferrably in a child theme. In the following example, I added the custom CSS to home.css.
Then, I used this snippet in my child theme’s functions.php file. You’ll need to change PAGE_ID to your actual page ID or page name.

Again, you can replace is_page() with your desired conditional tag. For posts, you can use is_single(). For a specific page template, you can use something like is_page_template( 'templates/about.php' ) and so on.

READ  5 SEO Opportunities for an eCommerce Website

I hope you find this useful. If you have any feedback please feel free to use the comments section below. Thanks for reading!

(Visited 217 times, 1 visits today)

Nagdy

I write about Multilingual WordPress, SEO, Website Performance. Also, I like answering WordPress question on Quora. When I have the time, I run online sessions to teach WordPress for my personal FB page fans!

Hopefully you'll come back soon!
Get More Stuff
WordPress Multilingual, Performance, SEO, Reviews and HOT Deals. One email per week!
Stay Updated
Give it a try, you can unsubscribe anytime.
Close
GET MORE STUFF
IN YOUR INBOX
WordPress Multilingual, Performance, SEO, Reviews and HOT Deals. One email per week!
Stay Updated
Give it a try, you can unsubscribe anytime.
neque. libero id et, Curabitur nec dolor leo

Pin It on Pinterest

Do you find this interesting?

Share it with your friends!