Skip to content

How to add custom CSS to WordPress

For a few rules, use the "Additional CSS" box that WordPress provides, in the Customizer on a classic theme or in the Site Editor's Styles panel on a block theme. For a long stylesheet, use a child theme. Never edit the theme's own files, because its next update overwrites them.

By
WP Ministry
Published

In short

  • WordPress has its own place for your CSS. A classic theme has it in the Customizer, a block theme in the Site Editor's Styles panel.
  • CSS added there is kept when the theme updates. It belongs to that theme and does not follow you to another one.
  • A change made in the theme's own files is lost at the theme's next update.
  • Your browser's developer tools show which selector to write, and which rule is beating yours.
  • When a rule does not apply, make its selector more specific before you reach for !important.
  • To undo a change, delete the rule and save.

Custom CSS is a set of rules of your own that change how your theme draws the site: a color, a font size, the space around a heading, an element you would rather hide. Your rules sit on top of the theme's. Where you put them decides whether they are still there after the theme's next update.

Where to put your CSS

WhereUse it whenAfter a theme update
"Additional CSS" in the CustomizerYou have a classic theme and a handful of rulesKept
"Additional CSS" in the Site Editor's Styles panelYou have a block theme and a handful of rulesKept
The style.css of a child themeYou have many rules, or want them in a fileKept
The theme's own filesNeverLost

To see which kind of theme you have, open the Appearance menu in the dashboard. A classic theme has Customize there. A block theme has Editor, and WordPress hides the Customizer unless a plugin needs it.

Both boxes keep your CSS in the database, apart from the theme's files. That is why an update does not touch it. The CSS belongs to the theme that was active when you wrote it. WordPress's documentation says CSS added in the Styles panel is cleared when you switch themes, and the Customizer stores its CSS separately for each theme. Copy your rules out before you change theme.

On a classic theme, use the Customizer

  1. Step 1: Open the Customizer

    Go to Appearance, then Customize.

  2. Step 2: Open "Additional CSS"

    It is one of the panels in the list on the left.

  3. Step 3: Write the rule

    Type or paste your CSS. The preview beside the panel changes as you type. Visitors see nothing yet.

    css
    .site-title a {
      color: #1a4d2e;
    }
  4. Step 4: Publish

    Select "Publish". The rule now applies for every visitor.

.site-title is an example. The names in your theme will differ, and the section on finding a selector shows how to read them.

On a block theme, use the Styles panel

Look through the Styles panel before you write anything. Colors, fonts, spacing and the look of each kind of block can be set there with no CSS, and WordPress's documentation recommends starting with those options. CSS is for what they do not reach.

  1. Step 1: Open Styles

    Go to Appearance, then Editor, and open Styles.

  2. Step 2: Open "Additional CSS"

    Open the menu with three dots at the top of the Styles panel and choose "Additional CSS".

  3. Step 3: Write the rule and save

    CSS written here applies across the whole site once you save.

CSS for one kind of block

Under Styles, then Blocks, choose a block. Its settings include a section called "Additional block CSS". What you write there applies to every block of that type on the site.

WordPress already knows which block you mean, so leave out the selector and the braces and write only the declaration. In the Quote block's box, this line sets every quote in italics:

css
font-style: italic;

In a child theme's stylesheet

A child theme is a small theme of your own that takes everything from your present theme and holds your changes to it. Its style.css is the place for a stylesheet that has outgrown a box in the dashboard, or one you want to keep as a file and copy between sites. Your rules go below the comment at the top of the file.

style.css
.entry-title {
  font-size: 2rem;
}

How to create a WordPress child theme covers making one, and checking that its stylesheet is loaded after the parent theme's.

Why not to edit the theme's own files

An update replaces the theme's files with those of the new version. A rule you added to the theme's own style.css goes with the old file, and nothing warns you. The two boxes and the child theme exist so that you never need to open those files.

If you have already edited them, copy your changes into one of the three places above before the theme's next update.

Find the right selector

A rule has two parts. The selector says which elements the rule applies to. The declarations between the braces say what changes. To write the selector you need the names your theme has given the element, and your browser's developer tools show them.

  1. Step 1: Inspect the element

    On your site, right-click the thing you want to change and choose "Inspect". Some browsers call it "Inspect Element". A panel opens showing the page's HTML, with that element picked out.

  2. Step 2: Read its class

    Look at the class of the element and of the elements around it. A heading written as <h2 class="entry-title"> is reached with .entry-title. A class is written with a dot in front of it, an ID with a #.

  3. Step 3: Look at the rules already applied

    Beside the HTML, the tools list every CSS rule that applies to the element, the most specific first, each with the name of the file it comes from. The rule at the top that sets the property you want to change is the one you have to beat.

  4. Step 4: Try the change there first

    Click a value in that list and type a new one. The page changes at once. Nothing is saved, and a reload brings the old value back. When it looks right, write the rule into WordPress.

When a rule does not apply

Inspect the element again and look for your rule in the list.

  • Your rule is not there. The selector does not match the element, or you are looking at an old copy of the page. Check the selector for a missing dot or a misspelled name. Then empty the cache of your caching plugin and of your host, and reload.
  • Your rule is there with a warning beside a line. The browser does not accept that property or value. Check the spelling.
  • Your rule is there with a line through it. Another rule has won.

When two rules set the same property on the same element, the browser picks the one whose selector is more specific. IDs count for most, then classes, then element names. If the two selectors are equally specific, the rule that comes later in the page wins.

So read the selector of the rule that beat yours, and write one that is a step more specific. Adding a class from an element further up the page is usually enough.

css
/* The theme's rule: one class and one element name */
.entry-content h2 {
  color: #333333;
}

/* Too weak: one element name */
h2 {
  color: #1a4d2e;
}

/* Strong enough: two classes and one element name */
.site-main .entry-content h2 {
  color: #1a4d2e;
}

If your selector matches the theme's exactly and still loses, the theme's rule comes later in the page than yours. That happens with a child theme whose style.css is loaded before the parent's. The fix is to load it after the parent's, which the child theme guide shows.

Writing !important after a value, as in color: #1a4d2e !important;, makes that declaration beat every ordinary one, whatever the selectors. That is also what is wrong with it. The next time you want to change the same thing, only another !important will do. MDN's advice is not to use it to get past specificity. Keep it for the one case that needs it: a style that a plugin or a script writes directly onto the element, which no selector can beat.

How to undo a change

  • Customizer: delete the rule from "Additional CSS" and select "Publish".
  • Site Editor: delete the rule from "Additional CSS" and save. The same menu has "Reset to defaults", but that takes back every change you made in Styles, not only your CSS.
  • Child theme: remove the rule from style.css and upload the file again. Keep a copy of the file from before each edit.

A wrong rule changes how a page looks, and removing the rule puts the page back.

If you would rather send the change than write the CSS, our website edits cover small changes to what is already on the site, and anything larger is quoted as developer time before work starts.

Common questions

Can I add CSS that applies to one page only?

Yes. WordPress puts classes on each page's body element that say which page it is: page-id- and the page's number on a page, postid- and the number on a post, home on the front page. Inspect the body element to read them, and begin your selector with one.

.page-id-42 .entry-title {
  display: none;
}
How do I change something on phones only?

Put the rule inside a media query. This one applies only when the browser window is 600 pixels wide or narrower.

@media (max-width: 600px) {
  .site-title {
    font-size: 1.5rem;
  }
}
Is CSS from the Customizer part of my backup?

It is part of a database backup. WordPress stores it in the database as a post of the type custom_css, one for each theme. A backup that holds only the site's files does not include it.

Is it safe to try CSS on the live site?

For a few rules, yes. The Customizer shows a preview, and nothing reaches visitors until you publish. For a larger restyle, work on a copy of the site first. How to set up a WordPress staging site shows how to make one.

More on this subject

Not sure what is wrong?

Tell us what you see. We reply with the cause and a fixed quote, and the diagnosis is free.