Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 139 ยท The Cascade

How the Cascade Works

Learn how the browser decides which CSS declaration wins when several rules target the same element, from origins and importance to specificity and order.

Sooner or later every CSS author writes a rule, saves the file, reloads the page and sees... nothing. The rule is fine. The selector matches. But some other rule is setting the same property on the same element, and that other rule wins. The process that picks the winner is called the cascade, and it is the "C" in CSS.

The cascade is not magic and it is not random. It is a short, fixed list of questions the browser asks, in order, until one declaration comes out on top. Once you know that list, "my CSS doesn't work" turns into a quick diagnosis. This lesson walks through every step, and the rest of this chapter zooms in on the trickier ones.

Conflicts are normal

A conflict happens when two or more declarations try to set the same property on the same element. Conflicts are not mistakes. Good stylesheets are full of them on purpose: a general rule sets a default, and a more targeted rule adjusts it.

A general rule and a specific one

The second paragraph matches p and .warning. Both rules set color, so they conflict, and the cascade decides that .warning wins. There is no conflict over font-size, though: only one rule sets it, so the paragraph simply gets 18px. Each property is decided separately. A rule never wins or loses as a whole; individual declarations do.

The questions the cascade asks

For each property on each element, the browser collects every declaration that applies and sorts them. It compares them by these criteria, in this order, and stops at the first one that tells them apart:

  1. Origin and importance. Where did the declaration come from, and is it marked !important?
  2. Inline styles. Is it in the element's own style attribute?
  3. Cascade layers. Which @layer is it in, if any?
  4. Specificity. How specific is the selector?
  5. Order of appearance. Which one comes last?

The key word is in order. Specificity only matters when origin, importance, inline-ness and layer are all tied. Source order only matters when specificity is tied too. That is why a very specific selector can still lose: it lost at an earlier step.

(There is also a step for styles inside shadow DOM, which matters for web components. You can ignore it for ordinary pages.)

Step 1: origins

Declarations come from three origins:

  • User-agent styles are the browser's built-in stylesheet. They are why headings are big and bold, links are blue and underlined, and <body> has a margin.
  • User styles come from the person using the browser, for example a minimum font size or a high-contrast setting or extension.
  • Author styles are yours: everything in <style> elements, linked stylesheets and style attributes.

For normal declarations, author beats user, and user beats user-agent. That is why your h1 { font-size: 2rem } beats the browser's default size even though both are "just a rule for h1".

!important flips this order, so an important user style beats an important author style. That rule protects people who need, say, larger text: their important styles win over yours. You will see the full picture in !important.

Author styles override browser defaults

The <h3> at the bottom has no author rule, so it keeps the browser's default bold, large style. Any property you don't set falls back to whatever the lower origins provide.

Step 2: inline styles

A declaration in a style attribute applies to that one element and beats any declaration from a selector in the same origin and importance. You saw this in Adding CSS to a Page: inline styles are hard to override from a stylesheet, which is the main reason to avoid them for everyday styling.

Step 3: cascade layers

@layer lets you put groups of rules into named layers and declare which layer beats which. When two declarations are in different layers, the layer order decides, no matter how specific their selectors are. Styles not in any layer beat all layered styles. Layers are a powerful way to keep a reset, a component library and your own overrides from fighting, and they get a full lesson: Cascade Layers.

Step 4: specificity

If everything so far is tied, the browser compares specificity: a score based on how many IDs, classes and element names the selector contains. An ID beats any number of classes, and a class beats any number of element names.

Specificity beats source order

All three rules match the paragraph, and the p rule comes last. It still loses, because source order is the last question and specificity is asked first. The ID selector has the highest specificity, so the text is teal. Delete the #intro rule and the class wins; delete that too and the paragraph finally turns blue. Specificity shows exactly how the score is calculated.

Step 5: order of appearance

When everything else is tied, the last declaration wins. "Last" means last in the order the browser reads CSS: rules further down a stylesheet beat earlier ones, and a stylesheet linked later beats one linked earlier.

Last one wins

The same applies inside a single rule. If you write color: red; color: green;, the element is green. Authors sometimes use this on purpose to provide a fallback for older browsers: the first declaration is understood everywhere, and the second one, using a newer feature, replaces it only in browsers that understand it. A browser that does not understand a declaration ignores it, so the fallback survives.

Why a rule "doesn't work"

When a declaration seems to be ignored, go through the cascade from the top. In practice the cause is almost always one of these:

  • The selector does not match. A typo in a class name, or a selector for the wrong element. Check it in DevTools: if the rule does not appear in the Styles panel for that element, it is not matching.
  • The declaration is invalid. A misspelled property or value is dropped silently. DevTools shows it with a warning icon.
  • A more specific selector wins. DevTools shows your declaration crossed out. Look higher in the Styles panel for the winner.
  • A later rule with the same specificity wins. Often a second stylesheet, or a rule further down the same file.
  • An inline style or !important declaration wins. Look for them in the HTML or the third-party CSS you load.
  • The property is inherited, not set. If no rule targets the element at all, it may be inheriting a value from its parent. Inheritance covers this.

Inspecting CSS with DevTools shows you where to find each of these clues. The crossed-out declaration is your best friend: it tells you the cascade considered your rule and something beat it.

Fix the cause, not the symptom. When a rule loses, the tempting fix is a heavier selector or !important. That works once and makes the next override harder. Usually the better fix is to lower the specificity of the rule that is winning, or to move rules into the right layer.

The cascade and inheritance are different

The cascade picks between declarations that directly target an element. Inheritance is what happens when no declaration targets the element for a property: some properties, like color and font-family, then copy the parent's value. A directly targeted declaration always beats an inherited value, even one with low specificity, because inherited values are not part of the contest at all.

Any matching rule beats inheritance

The #article rule has much higher specificity than em, but it targets the paragraph, not the <em>. The <em> would only inherit pink if nothing set its color directly. Because em { color: ... } targets it, that rule wins without any comparison.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Two rules set color on the same paragraph. One uses an ID selector and appears first; the other uses a type selector and appears last. Both are unlayered author styles. Which wins?

    Order of appearance is the very last tie-breaker. Specificity is checked first, and an ID outranks a type selector.
  2. 2. What does the cascade compare first?

    Origin and importance come first. Layers, specificity and source order only matter when that step is tied.
  3. 3. A rule sets both color and font-size. A more specific rule sets only color. What happens to font-size?

    The cascade works per property, not per rule. Nothing else sets font-size, so that declaration applies unopposed.
  4. 4. Which of these is NOT an origin in the cascade?

    The three origins are user-agent, user and author. Inheritance is a separate mechanism that only applies when no declaration targets the element.
  5. 5. In DevTools, your declaration appears crossed out. What does that tell you?

    A crossed-out declaration matched the element but lost to another declaration. A rule that does not match is not listed for the element at all.

Summary

  • The cascade decides which declaration wins when several set the same property on the same element.
  • It asks, in order: origin and importance, inline style, layer, specificity, order of appearance.
  • Author styles beat user styles, which beat browser defaults; !important reverses that order.
  • Each property is decided on its own; rules never win or lose as a whole.
  • Any declaration that targets an element beats a value it would inherit from its parent.
  • When a rule "doesn't work", check that it matches, that it is valid, and what beat it in DevTools.

Next: Specificity โ€” how the browser scores selectors, and how to keep those scores low.