Sub topic 11
The cascade: origin, specificity, and source order
The cascade is the algorithm the browser uses when several declarations set the same property on the same element. This chapter assumes you can read a CSS rule and write type, class, and ID selectors. It also assumes you can compute a specificity triple (IDs, classes, types) as covered in the previous chapter. Here you will learn the order in which the browser compares competing declarations, and how to trace a result by hand.
What the cascade decides
Every element has, for each property, exactly one final value. Many rules can match the element, and several can set the same property. The cascade picks one winner per property. It works per declaration, not per rule. Two rules can each win different properties on the same element.
The cascade only runs when declarations conflict. If one rule sets color and another sets margin, both apply, and no comparison is needed.
The comparison order
The browser gathers every declaration that applies to an element and property. It then compares them in a fixed sequence of steps. A later step matters only if all earlier steps tie.
- Origin and importance.
- Inline styles (the
styleattribute), for normal declarations. - Cascade layers.
- Specificity.
- Order of appearance (source order).
Layers have their own chapter later, and !important has one too. This chapter focuses on origin, specificity, and source order, with brief notes on the others so the full picture is accurate.
Step 1: Origin
Every declaration comes from one of three origins.
- User-agent: the browser's built-in stylesheet. It makes links blue and underlined, gives
h1a large bold font, and adds default margins tobody. - User: preferences set by the person using the browser, such as a minimum font size or a custom stylesheet.
- Author: the stylesheets you write for the site.
For normal (not !important) declarations, author beats user, and user beats user-agent. This is why your rules replace the browser defaults without any special effort.
a { color: green; }The user-agent stylesheet sets link color to blue. Your author rule wins, even though both selectors are a single type selector with specificity (0,0,1). Origin is compared first, so specificity never enters the comparison.
Origin outranks specificity. A user-agent rule with an ID selector would still lose to an author rule with a type selector. In practice, user-agent rules use low-specificity selectors anyway, but the order is what guarantees the result.
Importance reverses the origin order
When a declaration is marked !important, the order of origins flips. Important user-agent declarations beat important user declarations, which beat important author declarations. Important author declarations in turn beat all normal declarations. The reversal lets a user's !important accessibility setting override a site's own !important. The next chapters cover !important in detail.
| Priority (highest first) | Origin | Importance |
|---|---|---|
| 1 | User-agent | `!important` |
| 2 | User | `!important` |
| 3 | Author | `!important` |
| 4 | Author | normal |
| 5 | User | normal |
| 6 | User-agent | normal |
CSS animations and transitions also take part in this ordering. Running transitions sit above all of these rows, and running animations sit between the normal author row and the important rows. You rarely need this detail, but it explains why an animated value overrides a regular rule.
Step 2: Inline styles and layers
A style attribute on an element counts as an author declaration that beats any normal selector-based declaration in the same origin, regardless of how many IDs the selector contains. You can think of it as specificity above (1,0,0), but the spec treats it as a separate step.
Cascade layers come next. For normal declarations, a rule in a later layer beats a rule in an earlier layer, whatever their specificity. Unlayered author styles beat layered ones. Here is the example from the spine:
@layer reset, components;
@layer reset {
#nav a { color: black; }
}
@layer components {
a { color: blue; }
}The #nav a selector has specificity (1,0,1) and a has (0,0,1). The links are still blue, because components is declared after reset and layers are compared before specificity. Without layers, the #nav a rule would win. For !important declarations, the layer order reverses, much as the origin order does.
Step 3: Specificity
If the declarations share an origin, importance, and layer, the browser compares selector specificity. The specificity of a selector is a triple, compared left to right:
- The first number counts ID selectors.
- The second number counts class selectors, attribute selectors, and pseudo-classes.
- The third number counts type selectors and pseudo-elements.
The triples are compared position by position, not added into a single number. An ID always beats any number of classes, and a class always beats any number of types. Ten classes still lose to one ID.
p { color: black; } /* (0,0,1) */
.note { color: green; } /* (0,1,0) */
p.note { color: blue; } /* (0,1,1) */
#main p.note { color: purple; } /* (1,1,1) */
ul li.active > a[href] { color: red; } /* (0,2,3) */For a <p class="note"> inside <div id="main">, all of the first four rules match. Their triples are (0,0,1), (0,1,0), (0,1,1), and (1,1,1). The last one has the highest first number, so the text is purple. The ul li.active > a[href] rule targets a different element, a link, so it does not compete here. Its triple is (0,2,3): one class and one attribute give 2, and ul, li, and a give 3. The combinator > adds nothing.
The universal selector * and combinators contribute zero. Specificity is a property of the selector that actually matched. If a selector list such as h1, .title matches an element, the browser uses the specificity of the part that matched.
Step 4: Source order
When origin, importance, layer, and specificity all tie, the declaration that appears later wins. Order is counted across all stylesheets in the order the browser loads them. Declarations in a second <link> come after the first. A <style> element placed after a <link> comes after it.
.btn { background: gray; }
.btn { background: teal; }Both selectors are (0,1,0), so the later rule wins and the button is teal. Order never overrides a higher-priority step. A later .btn rule loses to an earlier #save rule on the same element, because specificity is checked before order.
Tracing a conflict by hand
Take this markup and CSS.
<div id="card">
<p class="note" style="margin: 0">Hello</p>
</div>#card p { color: navy; margin: 1rem; }
p.note { color: teal; margin: 2rem; }
.note { color: gray; }For color on the paragraph, all three rules are author normal declarations from the same stylesheet, with no layers. Specificity decides: (1,0,1), (0,1,1), and (0,1,0). The first wins, so the text is navy. For margin, the inline style attribute beats both selector rules, so the margin is 0. Each property is resolved on its own.
Use this routine for any conflict:
- List every declaration for the property that matches the element.
- Remove those from a lower-priority origin or importance level.
- If an inline style or a later layer is present, it wins over selector rules in the same origin.
- Compare specificity triples left to right.
- If they still tie, pick the one that appears last.
Common mistakes
- Adding up the triple as a single number. (0,11,0) does not beat (1,0,0).
- Assuming a later rule always wins. It does so only when everything before it ties.
- Forgetting the
styleattribute when a rule in your stylesheet seems to be ignored. - Counting a combinator or the universal selector as part of specificity.
- Expecting a later layer to lose to an earlier one because its selector is weaker. Layers are compared first.
Exercise
A page has <a id="cta" class="link" href="/buy">Buy</a> and these author rules, in this order: a.link { color: green; }, #cta { color: red; }, a[href] { color: blue; }. All are in one stylesheet with no layers and no !important. What color is the link, and which step of the cascade decides it?
Show answer
The link is red. Origin, importance, and layers tie, so specificity decides. The three triples are (0,1,1), (1,0,0), and (0,1,1). The ID selector has the highest first number, so #cta wins. Source order is never reached, even though a[href] appears last.
Exercise
Two rules both have specificity (0,1,0) and set padding on the same element. The first is in base.css and the second is in theme.css. In what situation does the base.css rule win?
Show answer
It wins when base.css is loaded after theme.css, for example because the <link> tags are in that order. With equal specificity, the declaration that appears later in the combined load order wins, regardless of file names.