Specificity and the cascade
Three rules set the colour of one button, and the one you wrote last does not win. The browser picks the winner by a fixed sequence of tests; here they are, scored one selector at a time.
01 The rule you wrote last does not win
You add a rule at the bottom of the stylesheet to make the Buy button green. It stays red. DevTools shows your rule with a line through it.
“Last one wins” is only the final tie-breaker. Before the browser looks at order, it runs several other tests, and the red rule wins one of them.
02 The cascade: tests in a fixed order
For each property of each element, the browser collects every declaration that applies, then runs these tests in order. The first test that separates them decides; later tests are only used to break a tie.
| # | Test | Who wins |
|---|---|---|
| 1 | Origin and importance | Your `!important` beats your normal rules, which beat the browser’s default styles |
| 2 | Inline style | A `style="..."` attribute beats any selector |
| 3 | Cascade layers | Rules outside any `@layer` beat layered ones; among layers, the one declared last wins |
| 4 | Specificity | The more specific selector wins |
| 5 | Order of appearance | The declaration that comes last wins |
The specification also has a step for shadow DOM scoping, between 1 and 2, which only matters inside web components. Most fights in ordinary stylesheets are settled at step 4, specificity.
03 Specificity is three numbers, not one
A selector’s specificity is a triple (A, B, C):
- A: one for each id, such as
#buy. - B: one for each class (
.btn), attribute ([type="submit"]) and pseudo-class (:hover). - C: one for each type (
button) and pseudo-element (::before).
The universal selector * and combinators (space, >, +, ~) score nothing. Triples are compared column by column from the left, like versions, with no carrying: any id beats any number of classes.
- just scored
- scored
- not scored yet
- wins
- loses
Five rules match button#buy.btn.primary inside nav.top, and each sets a different color. None is scored yet.
04 Selectors that score differently
| Selector | Score | Why |
|---|---|---|
| :is(#buy, .btn) | (1, 0, 0) | :is() takes its most specific argument |
| :not(.disabled) | (0, 1, 0) | :not() and :has() work the same way |
| :where(#buy, .btn) | (0, 0, 0) | :where() always scores zero |
| button:hover | (0, 1, 1) | a pseudo-class counts like a class |
| p::first-line | (0, 0, 2) | a pseudo-element counts like a type |
:where() is the tool for library and reset styles: wrap the selector in it and any rule a user writes later will beat it.
05 Beyond specificity: !important and layers
Tests 1 to 3 run before specificity, so they beat any selector, however long.
@layer base {#buy { color: red; }}
button {color: green;}
The button is green. Unlayered rules beat layered ones at test 3, so specificity is never compared. For !important declarations the order of layers is reversed: an !important in the first layer beats one in a later layer, which lets a base layer protect a rule.
Reaching for !important to win a fight starts an arms race: the next override needs !important too, and then only specificity and order decide between them. Lower the score of the rule you are fighting instead, for example with :where() or a layer.
06 Ask the browser which rule won
In Chrome or Firefox DevTools, select the element. The Styles pane lists matching rules from the winner down; losing declarations are struck through. In recent versions of Chrome, hovering a selector shows its specificity as a triple. The Computed tab shows the final value and, when expanded, every rule that tried to set it.
If your rule does not appear at all, it does not match the element: check the selector before you check the score.
07 When the cascade surprises you
#sidebar a { color: gray }Ids make rules hard to overrideEvery later override has to bring an id too. Style with classes and keep ids for anchors and JavaScript hooks.* { color: black }Inherited values always loseInheritance is not part of the score. A child whose parent is #buy { color: red } inherits red only if no rule sets color on the child itself; even *, scoring (0, 0, 0), beats it.:is(#main, .card) p:is() borrows its strongest argumentThis scores (1, 0, 1) even when it matches through .card. Use :where() if you want the grouping without the weight.style="color: red"Inline styles beat every selectorStyles a framework writes into the style attribute can only be beaten by !important in a stylesheet.<link href="theme.css">
<link href="app.css">File order is source orderFor two rules with the same score, the one in the file linked later wins. Swapping the tags, or the import order in a bundler, can change the page.