Sub topic 2
Managing the cascade, specificity and source order
This chapter assumes you know CSS selectors, the box model, and how to link a stylesheet to a page. It builds a working model of how the browser picks a winning declaration and shows which habits keep that choice predictable as a codebase grows.
How the browser picks a winner
When several rules set the same property on the same element, the browser compares them in a fixed order:
- Origin and importance: user-agent, user and author styles, with
!importantreversing the usual order. - Cascade layer: declared with
@layer, covered in a later chapter. - Specificity: how precise the selector is.
- Source order: the later rule wins when everything above is equal.
Most bugs in large codebases come from steps 3 and 4. Two rules fight, and the winner depends on selector weight or on which file loads last, not on what the author intended.
Reading specificity
Specificity is a three-part score, written (IDs, classes, types). Classes, attribute selectors and pseudo-classes such as :hover count as classes. Element names and pseudo-elements count as types. The universal selector * and :where() add nothing. :is() and :not() take the score of their most specific argument.
| Selector | Score |
|---|---|
| `p` | 0,0,1 |
| `.card` | 0,1,0 |
| `.card p` | 0,1,1 |
| `.card.featured` | 0,2,0 |
| `#main .card` | 1,1,0 |
| `:where(.card) p` | 0,0,1 |
The parts are compared left to right, not added. One ID beats any number of classes.
Why specificity escalates
Suppose a generic rule styles links, and a later requirement needs one link to look different:
.nav a {
color: #0b5fff;
}
.promo {
color: crimson; /* loses: .nav a is 0,1,1 and .promo is 0,1,0 */
}The .promo rule loses despite coming later. The usual reaction is to make the new selector heavier, such as .nav .promo, then #header .nav .promo, and finally !important. Each step raises the bar for the next override. Over time the stylesheet becomes an arms race, and moving any rule risks changing the result.
A strategy that keeps specificity flat
Aim for a codebase where almost every rule has the same low score, so source order alone decides conflicts. Four habits get you there:
- Style with single class selectors. Avoid IDs and avoid long descendant chains.
- Avoid qualifying classes with element names, such as
a.promo, because it adds weight for no benefit. - Keep the weight of rules that must be easy to override low.
:where()is useful for base styles because it contributes zero. - Treat
!importantas a last resort, and reserve it for utilities that must always win.
:where(.nav a) {
color: #0b5fff; /* 0,0,0 */
}
.promo {
color: crimson; /* 0,1,0, wins without any hack */
}With the :where() wrapper, the base rule is trivially overridable and .promo wins as intended.
Making source order deliberate
When specificity is equal, order decides. That is only safe if the order is controlled. Load stylesheets in a fixed sequence: broad, low-impact rules first, specific component rules after, overrides last. If a bundler or lazy-loaded component can reorder files, a rule can win in one page and lose in another. A common error is importing a component's CSS in two places and getting a different order depending on which page loads first.
Common errors
- Overriding with a more specific selector instead of lowering the specificity of the original.
- Using
!importantto beat another!important, which forces still heavier selectors. - Styling by DOM structure (
.sidebar ul li a), which couples CSS to markup and inflates the score. - Assuming
*or inherited values override a rule. Inherited values lose to any rule that targets the element directly.
Exercise
A rule .card .title { margin: 0; } is being overridden by a later rule .title { margin: 1rem; }. Why does the earlier rule still win, and how would you make the later rule win without !important or a heavier selector?
Show answer
.card .title scores 0,2,0 and .title scores 0,1,0, so the earlier rule wins despite coming first. To let the later rule win, lower the first rule's weight, for example by writing it as .title or :where(.card) .title. Both then score 0,1,0 or less, and source order lets the later rule win.