-- online Search!

The Cascade, Specificity, and Inheritance

by kirupa   |   filed under HTML and CSS

TL;DR

When several CSS rules set the same property on one element, the browser asks three questions in order: is one more important, is one more specific, and which one comes last? That process is the cascade. Specificity counts IDs, then classes, then types, and text properties like color and font also pass down from parents through inheritance.

  • Conflicts are settled one property at a time, so a paragraph can take its color from #intro, its font size from p, and its weight from .note.
  • Specificity is compared one column at a time, so #intro at 1-0-0 beats .card .note:hover at 0-3-0, and no number of classes can outrank an ID.
  • Inline styles beat every selector, and !important beats inline styles, which is why it's best saved for the rare rule that truly must win.
  • Inherited values have no specificity, so even a plain p rule beats the color a paragraph would have inherited from its parent.

We write a CSS rule, save the file, refresh the page, and...nothing changes. The rule is right there, the selector looks correct, and the element stubbornly keeps its old color anyway. What happened is that our rule lost a fight. Some other rule was also trying to style that same element, and the browser picked that one instead.

The browser doesn't pick winners at random. It follows a short list of tie-breakers, and once we know them, we can look at any two rules and predict which one wins. That's what we're going to learn here, along with the one way an element can get styled without any rule targeting it at all.

Onwards!

What Happens When Two Rules Collide?

Let's start with a single paragraph that has both an id and a class:

<p id="intro" class="note">Hello!</p>

Now, let's say our stylesheet has three rules that all match it:

p {
  color: gray;
  font-size: 20px;
}

.note {
  color: blue;
  font-weight: bold;
}

#intro {
  color: red;
}

Before reading on, take a guess at what color our paragraph ends up. All three rules match it, and all three set a color, but only one of them can win. Here's what the browser does with them:

Three rules match the same paragraph. The p rule sets color to gray and font-size to 20px, the .note rule sets color to blue and font-weight to bold, and the #intro rule sets color to red. Only #intro's red color survives the fight over color, but font-size still comes from p and font-weight from .note, so the paragraph ends up red, 20px, and bold.

Our paragraph ends up red, thanks to the #intro rule. What's noteworthy is that the other two rules didn't lose completely. The browser settles the fight one property at a time. The color goes to #intro, but no other rule sets font-size, so that still comes from the p rule. In the same way, font-weight still comes from .note. Our "Hello!" ends up red, 20 pixels, and bold, with a little bit from each rule.

How Does the Browser Pick a Winner?

When two or more declarations try to set the same property on the same element, the browser asks a few questions, in order. The first question with a clear answer picks the winner, and the rest never get asked:

Three questions, asked in order. First: is one declaration more important, such as one marked !important, or ours versus the browser's built-in styles? If it's still a tie: is one selector more specific? If it's still a tie: which rule comes last in the code? The first question with a clear answer picks the winner.

Let's walk through each of those questions:

  1. Is one more important? Our own styles beat the browser's built-in styles (the ones that make headings big and links blue). A declaration marked !important beats one that isn't, and we'll look at that a bit later.
  2. Is one more specific? If both declarations are equally important, the one with the more specific selector wins. An ID selector is more specific than a class selector, and a class selector is more specific than a type selector.
  3. Which one comes last? If the selectors are equally specific, the declaration that shows up later in our CSS wins.

This process of sorting through competing declarations is called the cascade, and it's the "C" in CSS. In our example, all three rules are ours and none of them use !important, so the first question is a tie. The second question settles it, since #intro is the most specific selector of the three.

The last question is the easiest one to see in action. Here are two rules with the exact same selector:

.note {
  color: blue;
}

.note {
  color: green;  /* this one wins */
}

The second rule wins because it comes later. This also works across files. If our HTML links two stylesheets, every rule in the second one comes after every rule in the first, which is why the order of our link elements matters. The Where Will Your Styles Live? tutorial covers the different places our CSS can live.

Newer CSS also has a feature called cascade layers (@layer), which adds one more question between importance and specificity. It lets us say that one whole group of styles should always beat another group, no matter what selectors they use, and the Cascade Layers in CSS tutorial goes deep on it. For styles that don't use layers, the three questions above are the whole story.

How Is Specificity Calculated?

"More specific" needs a more precise definition, and it has one. The browser gives every selector a score by counting three kinds of things in it:

  • IDs, like #intro
  • Classes, like .note, along with attribute selectors like [type="email"] and pseudo-classes like :hover
  • Types, like p or article, along with pseudo-elements like ::before

This score is known as specificity. We write it as three numbers in that order, and here's how a handful of selectors add up:

Five selectors and their specificity, written as IDs, classes, and types. p is 0, 0, 1. .note is 0, 1, 0. article p.note is 0, 1, 2. .card .note:hover is 0, 3, 0. #intro is 1, 0, 0, which beats all of the others because a single ID outranks any number of classes.

To compare two scores, we start with the IDs. If one selector has more IDs, it wins, and we don't even look at the other columns. If the IDs are tied, we compare the classes, and if those are tied too, we compare the types. It works a lot like comparing version numbers, where version 1.0.0 is newer than version 0.3.0 even though 3 is bigger than 1. That's why #intro at 1‑0‑0 beats .card .note:hover at 0‑3‑0. The columns never carry over into each other, so even a selector with 11 classes can't beat a single ID.

A few things don't count toward specificity at all. The universal selector * and the combinators that connect selectors (a space, >, +, and ~) add nothing. The :is(), :not(), and :has() pseudo-classes don't count themselves, but they take on the score of the most specific selector inside them. Then there's :where(), which always scores 0‑0‑0 no matter what's inside it. That makes it handy for writing default styles that any other rule can easily override. If you'd like a refresher on the selectors themselves, the CSS Selectors: Type, Class, and ID and CSS Attribute Selectors tutorials cover them.

Here's a place to try all of this out. Two rules are trying to set the color of the same paragraph, and we can type any selector we like into either one. The demo scores both selectors and tells us who won:

Try it

  1. Type article p into Rule B
  2. Now try #intro
  3. Then .note, for a tie

Whenever both rules match the paragraph, the higher score wins. When the scores are tied, Rule B wins because it comes second. If you type a selector that only matches the article around the paragraph, like article, something different happens, and we'll see why in a little bit.

What About Inline Styles and !important?

There are two ways to jump ahead of everything we've seen so far. The first is an inline style, which is CSS written directly on an element using its style attribute:

<p id="intro" class="note" style="color: purple;">Hello!</p>

An inline style beats any selector in our stylesheets, no matter how many IDs that selector has. This is also what's going on when JavaScript sets a style with something like element.style.color, which the Changing CSS using JavaScript tutorial covers.

The second way is !important. Adding it to the end of a declaration bumps that declaration up past all of the normal ones, inline styles included:

.note {
  color: green !important;
}

Here's how all of these stack up against each other, from weakest to strongest:

A staircase that climbs from left to right. The bottom step is a type selector, p. Then a class selector, .note, then an ID selector, #intro, then an inline style attribute, and at the top, a declaration marked !important.

If two declarations both use !important, the browser goes back to comparing their specificity and then their order. That leads to the problem with !important. Once one rule uses it, the only way to beat that rule is with another !important, and soon our stylesheet turns into an arms race where every declaration is shouting. It's fine for the rare case where something truly must win, like a utility class whose whole job is to hide an element. For everyday conflicts, a better fix is to lower the specificity of the rule that's winning or to move our rule later in the file.

What Gets Inherited?

So far, every rule we've looked at matched our paragraph directly. There's one more way an element can get a style, and that's from its parent. Let's say we style an article and nothing else:

<article>
  <h2>Hello!</h2>
  <p>Some text.</p>
</article>

Here's the only CSS on the page:

article {
  color: purple;
  border: 4px solid;
}

The heading and paragraph don't have any rules of their own, yet both of them show up purple. Neither of them gets a border, though:

An article with color: purple and a border sits above its two children, an h2 and a p. Both children inherit the purple text color without asking for it, but neither gets a border, because borders aren't inherited.

Some properties pass down from a parent to its children (and to their children, and so on) unless something says otherwise. This is known as inheritance. Properties that have to do with text are usually inherited, because we almost always want the text inside an element to match the element. Properties that have to do with the box itself aren't, because if every child inherited its parent's border, we'd end up with boxes inside boxes inside boxes. Here are some common ones on each side:

Inherited?Common properties
Yescolor, font-family, font-size, font-weight, line-height, letter-spacing, text-align, visibility, cursor
Nowidth, height, padding, margin, border, background, display, position

Inheritance has one quirk that trips people up. An inherited value is the weakest thing in the whole cascade. It doesn't have any specificity at all, so any rule that targets an element directly beats a value the element inherited, even a plain type selector:

#main {
  color: red;
}

p {
  color: gray;  /* wins for every p inside #main */
}

The #main rule has an ID in it, but it only matches the #main element itself. The paragraphs inside it would have inherited red, except the p rule targets them directly, so they're gray. This is what the demo above was showing us. If you go back and type article.card into Rule A and p into Rule B, Rule A scores 0‑1‑1 and still loses to a plain p, because Rule A only matches the article around the paragraph.

We can also take control of inheritance ourselves with a few special values that work on any property:

  • inherit takes the parent's value, even for properties that don't normally inherit.
  • initial resets the property to its default value from the CSS specification.
  • unset acts like inherit for properties that normally inherit and like initial for everything else.
  • revert rolls the property back to whatever the browser's built-in styles would have given it.

The one we'll reach for most often is inherit. The browser's built-in styles give form controls like buttons and text fields their own font, which is why they often show up in a different font than everything around them. One rule makes them match the rest of the page:

button,
input,
select,
textarea {
  font: inherit;
}

Why Isn't My CSS Working?

Putting it all together, here's a checklist to run through the next time a rule refuses to apply:

  1. Does the selector actually match the element? A typo in a class name means the rule never even enters the fight.
  2. Is the declaration valid? A misspelled property or an invalid value gets quietly ignored.
  3. Is another rule winning on specificity? Look for a selector with an ID or more classes that sets the same property.
  4. Is another rule winning on order? A rule with the same specificity that comes later in the file, or in a stylesheet linked after ours, wins.
  5. Is our rule only styling the parent? If so, the element only inherits the value, and any rule that targets the element directly beats it.
  6. Is there an !important or an inline style somewhere?

We rarely have to work through this list by hand. In the browser's developer tools, we can select an element and look at the Styles panel. It lists every rule that matches the element, with the winning rules at the top, and any declaration that lost the fight is crossed out. In Chrome and Firefox, hovering over a selector there even shows its specificity.

Conclusion

Whenever more than one rule tries to set the same property on the same element, the cascade picks a winner by asking three questions in order. Is one more important? Is one more specific? Which one comes last? Specificity is a score made up of IDs, classes, and types, compared one column at a time, which is why a single ID beats any number of classes. Inline styles and !important jump ahead of all of that, and that's exactly why we should use them sparingly. When no rule targets an element at all, it can still inherit text-related properties like color and font from its parent, though any rule that does target it directly will win.

With these few rules in our back pocket, "why isn't my CSS working?" goes from a mystery to a quick investigation. If you want to keep going, the box model and CSS units tutorials cover how the sizes we set actually turn into boxes on the page.

Before you go, here are a few things you can do:

  1. Ask a question

    Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.

Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright © KIRUPA 2026 //--