Skip to content

Specificity and the cascade

BasicsConcept

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.

index.html
<nav class="top">
<button id="buy" class="btn primary">Buy</button>
</nav>
styles.css
#buy { color: red; }
/* added later, at the bottom */
nav.top .btn.primary { color: green; }

“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.

Cascade order (CSS Cascading and Inheritance Level 5)
#TestWho wins
1Origin and importanceYour `!important` beats your normal rules, which beat the browser’s default styles
2Inline styleA `style="..."` attribute beats any selector
3Cascade layersRules outside any `@layer` beat layered ones; among layers, the one declared last wins
4SpecificityThe more specific selector wins
5Order of appearanceThe 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.

Scoring five selectors
Step 1 of 7
rules that match the Buy button
A idsB classesC typescolor
buttongray
.btn.primarywhite
nav .btnblue
#buyred
nav.top .btn.primarygreen
  • 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

Pseudo-classes that take a selector list
SelectorScoreWhy
: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.

Layered, high score
@layer base {
#buy { color: red; }
}
Unlayered, low score
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.

Text
element.style {
}
#buy { ← hover: Specificity (1,0,0)
color: red;
}
nav.top .btn.primary { ← hover: Specificity (0,3,1)
color: green; ← shown struck through
}

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.

08 Check yourself

Question

Which wins: a selector of eleven classes, .a.b.c.d.e.f.g.h.i.j.k, or #x, both matching the same element?

Question

Which wins: :where(#main) p { color: red } or p.note { color: blue }, for p.note inside #main?

Question

@layer base { #buy { color: red } } and, outside any layer, button { color: green }. What colour is the button?

09 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?