• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

nSiteful Web Builders

Building a Better Web - One Site at a Time.

  • Home
  • About
    • Testimonials
    • Resources
    • Partner With Me
    • Frequently Asked Questions
  • Web Sites
  • Online Marketing
  • WordPress Support
    • Customized WordPress Training
    • 60-for-60 Sessions
  • Web Applications
  • Blog
    • At-A-Glance
    • Blog Articles Grouped by Category
    • Case Studies
    • General
    • Portfolio
    • Reviews
    • Snippets
    • Techniques
    • Projects
  • Contact
    • Purchase Retainer Consulting Hours
    • About Retainer Consulting Hours
    • Book a Meeting with Jeff
    • Tell Me About Your Web Project
    • nSiteful Newsletter Archives
    • nSiteful Rewards

By Jeff - 10/5/2026
Cats: Snippets

snippets

Post-Specific CSS for WordPress

On This Page...

Toggle
  • Many Ways to Add CSS
  • Reasons to Use Post-Specific CSS
    • Unique edge cases
    • Testing
    • Embedding CSS in the post itself can be risky
  • How to implement post-specific CSS
    • Advanced Custom Fields: add Field Group
    • Adding action to wp_head hook
  • The Result

Many Ways to Add CSS

You might be wondering why I’m talking about post-specific CSS. After all, CSS rules belong in style sheets, right? (Mostly right.)

But you can also enter CSS rules inline. Just add a block like this, either in a Custom HTML block or inside the classic editor, and the rules will apply only to this post:

<style>
body {
	background-color: inherit;
}
.some-class {
	font-size: 3em;
}
...and so on
</style>

And if you’re using WordPress, there’s always the Additional CSS panel in the in the Appearance Customize Screen.

So, why would we ever want or need to have post-specific CSS (aka per-post CSS)?

Reasons to Use Post-Specific CSS

Unique edge cases

Sometimes the presentation rules for a specific post are just so unique that it doesn’t make sense to include them in the main CSS file or even in the Additional CSS panel.

For example, imagine you’re creating a landing page for a very narrow and specific purpose. The presentation requirements for this landing page are unique, and it’s highly unlikely you’ll ever need those rules again.

Testing

Even if the ultimate intention is to include the code in the regular CSS file, it’s a lot more efficient to just test it from within the post than to either update the main CSS file or add the rules to the customizer.

Yes, you can and should use your browser’s devtools for CSS testing. But for complex rules and declarations, it’s nice to have that markup on hand for the next time you refresh your browser.

Embedding CSS in the post itself can be risky

If always using the classic editor in code mode is your jam, there’s no problem. But for me that’s rare.

While I do still use the classic editor a lot, I’ll often start in code mode but then switch to visual mode. That’s so I can take advantage of the TinyMCE style formatting features. (Yeah, I’ve been coding HTML since 1994, but I’m not ashamed to admit I like shortcuts from time to time.)

And here’s the thing:

If you ever switch from Code mode to Visual mode in the classic editor, your CSS at the top of the file will be destroyed because newline characters are added in.

Also, TinyMCE HTML encodes some special characters. In most cases, that’s what you want. However, if (like me) you often include code snippets in your blog posts, TinyMCE can convert them in an undesirable way:

How to implement post-specific CSS

Advanced Custom Fields: add Field Group

Here’s the setup from one of my websites:

In Advanced Custom Fields, create a field group called Post-Specific CSS. It will include only one field.

  • Field Type: Textarea
  • Field Label: CSS Rules
  • Field Name: `post-specific_css`
  • Required: No
  • Instructions: Enter valid CSS rules here
  • Rows: 12

Location Rules: Set the field group to appear on all post types where you want to allow post-specific CSS. Typically, that would include posts and pages

NOTE: The field name is important. It can be any valid name, but it must correspond to the meta_key that is referenced in line 14 of the function below.

That’s really all there is to the ACF setup. Each applicable post or page now has a large textarea where I can enter CSS rules that apply specifically to that individual piece of content.

Adding action to wp_head hook

Add this code to a code snippet or a file in your mu-plugins directory

add_action( 'wp_head', 'nsiteful_output_per_post_css' );
function nsiteful_output_per_post_css() {

	if ( ! is_singular() ) {
		return;
	}

	$post_id = get_queried_object_id();

	if ( ! $post_id ) {
		return;
	}

	$css = get_post_meta( $post_id, 'post-specific_css', true);

	if ( empty( $css ) ) {
		return;
	}
	echo "\n\n<style id='nsiteful-per-post-css'>\n";
	echo wp_strip_all_tags( $css );
	echo "\n</style>\n\n";
}

Line 1: Sets wp_head as the hook
Line 14: Stores the post-specific CSS in the variable $css
Lines 19-21: Generates the style element

The Result

That’s all there is to it. Now you should see something like this in your post editor:

Post-Specific CSS

Like what you see? Share with others and join my mailing list. No long-term commitment, unsubscribe any time.

Related Posts

  1. Automatic “Visit Web Site” Links on WordPress Portfolio Posts
  2. Dive Into WordPress Custom Post Types – Part 2
  3. Avoid Direct Styling; Use CSS Instead
  4. Dive Into WordPress Custom Post Types – Part 1
  5. Dive Into WordPress Custom Post Types – Part 3

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

mailchimp signup

Subscribe to get notified when new articles are published. Unsubscribe any time. No spam. I promise. Check out my newsletter archives.

social

Twitter Facebook LinkedIn

Recent Articles

  • Post-Specific CSS for WordPress October 5, 2026
  • You built an online course, but they haven’t come. September 16, 2026
  • Custom MemberPress Pricing Pages December 19, 2025
  • CSS Data Attribute Override December 11, 2025
  • MemberPress Rules Using Custom Taxonomies and Custom Roles December 10, 2025

Filter By Category/Tag

Categories

  • Case Studies (8)
  • For Staff (1)
  • General (73)
  • Portfolio (7)
  • Reviews (14)
  • Snippets (24)
  • Techniques (52)

Popular Tags

Advanced Custom Fields Blogging Child Themes Content Marketing CSS Customer Service Custom Fields Custom Post Types Diagnostics Domain Names Facebook FooGallery Genesis Hosting HTML Images iPhone Libra Live Chat Marketing Media MemberPress MemberPress Courses Membership Sites Mobile-Friendly MySQL Photo Gallery php Pinterest Plugins Post Formats Pricing Project Management SEBA SEO Seth Godin Shortcodes Social Networking Surveys Taxonomies Twitter Video Web design Web forms WordPress

Footer

Background

Web Sites | WordPress Support | Web Applications.

Formally trained in liberal arts and education (I have a B.A. in Government from Harvard and studied Secondary Education at Rutgers Graduate School), I have honed my skills in the communication arts and sciences as a teacher, trainer, instructional designer, writer, photographer, calligrapher, helpdesk manager, database programmer, and multimedia developer.

(I've also been a group counselor, waiter, bartender, bicycle messenger boy, computer salesman, carpenter's helper, financial analyst, and school board president.)

Tech

Systems since 1983.
Web sites since 1994.
PHP since 2001.
WordPress since 2007.

Contact

Book Meeting
770-772-5134
Email Jeff
Send Money
All Ways

Copyright 2026, nSiteful Web Builders, Inc.

Cookies Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
View preferences
  • {title}
  • {title}
  • {title}
https://iframe.mediadelivery.net/embed/392008/42d18bc1-2adc-4741-b733-053d08d09c32
https://vz-000c5976-3ab.b-cdn.net/42d18bc1-2adc-4741-b733-053d08d09c32/play_720p.mp4

Receive occasional emails from Jeff