Which CSS selector wins?

Type two selectors. See the score, and exactly why.

1,000 characters max. Local analysis; edits update URL.

Specificity 1, 0, 1. Wins.

Wins

1,000 characters max. Local analysis; edits update URL.

Specificity 0, 3, 0.

Loses

#nav a wins in the first column: 1 ID beats 0.

Want a different winner?

Ways to change the result, from the gentlest to the most heavy-handed. Read more in why is my CSS not applying?

  • Lower the winner with :where()

    Wrap #nav a in :where() so it scores (0,0,0). Then .menu .link:hover wins with (0,3,0).

    :where(#nav a) {
      /* … */
    }
  • Move the rules into ordered layers

    Layers are compared before specificity. Put #nav a in an earlier layer and .menu .link:hover in a later one, and .menu .link:hover wins whatever the scores.

    @layer base, components;
    
    @layer base {
      #nav a { /* … */ }
    }
    
    @layer components {
      .menu .link:hover { /* … */ }
    }
  • Raise the loser

    #nav a has more IDs, and no number of classes beats an ID. Lower the winner or use layers instead.

  • Use !important (last resort)

    Mark the declaration in .menu .link:hover as !important. It wins over every normal declaration, whatever the specificity.

    .menu .link:hover {
      color: rebeccapurple !important;
    }

    Note: Only another !important can beat it, and inside layers the order reverses. It tends to start an arms race, so reach for it last.

Anatomy

Every part of each selector above and what it adds. Underlines show the column: solid for IDs, dashed for the class column, dotted for the type column, double for functional pseudo-classes. Read how specificity is calculated.

Selector 1: #nav a (1,0,1)

Breakdown of #nav a
PartKindIDsClassesTypesWhy
#navID100Adds 1 to the first column.
spaceCombinator000Combinators add nothing.
aType001Adds 1 to the third column.
Total101(1,0,1)

Selector 2: .menu .link:hover (0,3,0)

Breakdown of .menu .link:hover
PartKindIDsClassesTypesWhy
.menuClass010Adds 1 to the second column.
spaceCombinator000Combinators add nothing.
.linkClass010Adds 1 to the second column.
:hoverPseudo-class010Adds 1 to the second column, like a class.
Total030(0,3,0)

Nesting

Paste nested CSS to see the selectors it really produces, and their specificity. Read how nesting affects specificity.

& .title becomes :is(.card, #promo) .title, which scores (1,1,0). :is() takes its most specific argument, even for elements that only match a weaker parent selector.

Expanded rules
As writtenExpands toSpecificityInside
.card, #promo.card
#promo
(0,1,0)
(1,0,0)
–
& .title:is(.card, #promo) .title(1,1,0)–
&:hover:is(.card, #promo):hover(1,1,0)–
&:is(.card, #promo)(1,0,0)@media (width > 40em)

Note: Nested @media and @supports blocks become separate rules, which can change the order of appearance compared with how the code reads.

Cascade

Specificity is only step five. Describe two to four competing declarations for the same element and watch all seven steps of the cascade decide. Read the cascade order.

Like @layer base, components;

Declaration 1
Declaration 2
Declaration 3

h1 wins: color: crimson.

Decided at step 4, layers: the unlayered rule beats any layered rule for normal declarations, so specificity never got a vote.

  1. Origin and importance Tie

    All are normal author declarations.

  2. Context Tie

    All come from the page itself; no shadow tree is involved.

  3. Element-attached styles Tie

    None are inline styles.

  4. Layers Decided here

    The unlayered rule beats any layered rule for normal declarations, so specificity never got a vote.

  5. Specificity Not reached

  6. Scope proximity Not reached

  7. Order of appearance Not reached

Show as CSS
@layer base, components;

/* order 1 */
@layer components {
  #hero .title { color: navy; }
}

/* order 2 */
h1 { color: crimson; }

/* order 3 */
@layer base {
  .title { color: teal; }
}

Explainer

Specificity and the cascade, explained

When two rules set the same property on the same element, the browser has to pick one. Most people learn that "the more specific selector wins". That's true, but it's only one step of a longer process called the cascade. This guide covers both, with examples you can load straight into the tool above.

How is CSS specificity calculated?

Specificity is three separate counts, written (A, B, C). Look at every simple selector in the selector and add it to one column:

  • A, IDs: each #id.
  • B, classes: each .class, attribute selector like [type="text"] and pseudo-class like :hover.
  • C, types: each element name like p and pseudo-element like ::before.

The universal selector * and combinators (space, >, +, ~) add nothing. Namespace prefixes such as svg| add nothing either.

To compare two selectors, read the columns from left to right. The first column that differs decides the result and the rest never get a look. Columns never carry over: eleven classes don't add up to an ID.

One ID beats any number of classes
Try this in the duel
Selector IDs Classes Types
#nav a 1 0 1
.menu .link:hover 0 3 0

#nav a wins in the first column: 1 ID beats 0.

Eleven classes still lose to one ID
Try this in the duel
Selector IDs Classes Types
.a.a.a.a.a.a.a.a.a.a.a 0 11 0
#b 1 0 0

#b wins in the first column: 1 ID beats 0.

If every column is equal, specificity can't separate them, and the rule written later wins. That's the last step of the cascade, order of appearance.

Old single-colon pseudo-elements such as a:before still count as pseudo-elements, in the third column. A selector list like .a, #b isn't scored as a whole: each selector has its own specificity, and the rule uses the one that matched.

How do :is(), :not(), :has() and :where() count?

:is(), :not() and :has() take the specificity of their most specific argument. That happens even when the element only matches a weaker argument, or, for :not(), matches none of them.

:where() always counts as zero, whatever is inside it. That makes it the best tool for writing defaults that are easy to override.

The most specific argument counts; :where() counts nothing
Try this in the duel
Selector IDs Classes Types
.foo :is(.bar, #baz) 1 1 0
.qux:where(em, #foo#bar#baz) 0 1 0
:not(em, strong#foo) 1 0 1

.foo :is(.bar, #baz) wins in the second column: 1 class, attribute or pseudo-class beats 0. Its closest rival is :not(em, strong#foo).

:is() and :where() also take a forgiving list: an argument the browser can't parse is ignored instead of breaking the whole rule. :not() and :has() don't forgive.

What about :nth-child(… of S)?

:nth-child() and :nth-last-child() count as one pseudo-class. Add the of S syntax and they also add the most specific selector in S. Other pseudo-classes with arguments, such as :nth-of-type(2) or :lang(en), count as one pseudo-class.

One pseudo-class, plus the strongest argument after of
Try this in the duel
Selector IDs Classes Types
:nth-child(even of li, .item) 0 2 0
:nth-last-child(odd of #a .b) 1 2 0
:nth-of-type(2) 0 1 0

:nth-last-child(odd of #a .b) wins in the first column: 1 ID beats 0. Its closest rival is :nth-child(even of li, .item).

Shadow DOM selectors

Inside a component's shadow tree, :host counts as a pseudo-class. :host() and :host-context() count as a pseudo-class plus their argument. ::slotted() counts as a pseudo-element plus its argument, and ::part() counts as one pseudo-element.

Shadow DOM selectors add their arguments
Try this in the duel
Selector IDs Classes Types
:host 0 1 0
:host(#x) .y 1 2 0
::slotted(span.x) 0 1 2
x-foo::part(label):hover 0 1 2

:host(#x) .y wins in the first column: 1 ID beats 0. Its closest rival is x-foo::part(label):hover.

Specificity isn't the whole story here. Styles from the page and styles from inside a shadow tree are compared at the context step of the cascade first: for normal declarations the page wins, and for !important the shadow tree wins.

How does nesting affect specificity?

In native CSS nesting, & stands for the parent selector wrapped in :is(). When the parent is a list, the nested selector takes the specificity of the strongest parent, even for elements that only match a weaker one.

A nested rule takes its strongest parent's specificity
Try this in the nesting expander

CSS and HTML

.a, #b {
  & .c { color: crimson; }
}
.a .c.c { color: teal; }

<div class="a"><p class="c">This text is</p></div>

Rendered by your browser

& .c becomes :is(.a, #b) .c, which scores (1,1,0) and beats .a .c.c (0,3,0), even though the element is only inside .a.

A nested rule without & behaves as if it started with & and a space. Use the nesting expander to paste your own CSS and see the result. Nested @media and @supports blocks become separate rules, which can change the order of appearance compared with how the code reads.

The cascade order

Specificity is step five of seven. The browser walks these steps in order and stops at the first one that separates the declarations:

  1. Origin and importance. Transitions, then !important browser, user and author styles, then animations, then normal author, user and browser styles.
  2. Context. The page versus a shadow tree.
  3. Element-attached styles. The style attribute beats rules from stylesheets.
  4. Layers. The order of @layer blocks, and unlayered styles.
  5. Specificity. The (A, B, C) comparison.
  6. Scope proximity. How close the @scope root is.
  7. Order of appearance. The last one written wins.

The cascade resolver above walks these steps for your own declarations and marks the step where the winner was decided.

Does @layer beat specificity?

Yes. Layers are compared at step four, before specificity gets a vote. For normal declarations, later layers beat earlier ones, and styles outside any layer beat all layered styles.

Unlayered beats layered, whatever the specificity
Try this in the cascade resolver

CSS and HTML

@layer components {
  #hero .title { color: navy; }
}
p { color: crimson; }

<div id="hero"><p class="title">This text is</p></div>

Rendered by your browser

Decided at step 4, layers: the unlayered rule beats any layered rule for normal declarations, so specificity never got a vote.

For !important the order reverses: the earliest layer wins, and layered !important beats unlayered !important. This lets a low-level reset protect a few declarations without fighting everything above it.

!important reverses the layer order
Try this in the cascade resolver

CSS and HTML

@layer reset, components;
@layer reset {
  p { color: red !important; }
}
@layer components {
  .note p { color: blue !important; }
}
#intro { color: green !important; }

<div class="note"><p id="intro">This text is</p></div>

Rendered by your browser

Decided at step 4, layers: for !important the layer order reverses: the earlier layer reset beats components.

Calling !important a "cascade layer" is a common mistake. It's part of the first step, origin and importance, and it interacts with layers by flipping their order.

What is @scope proximity?

@scope (.card) { p { … } } limits a rule to elements inside .card. When two scoped rules tie on everything before, the one whose scope root is fewer DOM steps away from the element wins. Unscoped rules count as infinitely far away.

Proximity comes after specificity, so a more specific selector beats a closer scope:

Specificity is checked before proximity
Try this in the cascade resolver

CSS and HTML

@scope (.card) {
  p { color: teal; }
}
.x p { color: purple; }

<div class="x"><div class="card"><p>This text is</p></div></div>

Rendered by your browser

Decided at step 5, specificity: .x p has the higher specificity: (0,1,1) beats (0,0,1).

Where do inline styles fit?

A style attribute isn't a fourth column of specificity. It's a separate step, element-attached styles, which runs before layers and specificity. A normal inline style beats every normal rule in a stylesheet, even #id#id#id. An inline !important beats any author !important from a stylesheet.

A normal inline style beats any selector
Try this in the cascade resolver

CSS and HTML

#card#card { color: navy; }

<p id="card" style="color: crimson">This text is</p>

Rendered by your browser

Decided at step 3, element-attached styles: an inline style attribute beats every normal rule from a stylesheet, whatever its specificity.

Only an author !important beats a normal inline style, and that's decided at step one.

Why is my CSS not applying?

When a rule seems to be ignored, check these, roughly in the order the cascade does:

  • The rule is invalid. A typo, an unknown pseudo-class or a pseudo-element that isn't last makes the browser drop the whole rule. The anatomy view flags these.
  • Something is !important. Normal declarations can never beat it, however specific.
  • An inline style attribute, often set by a script, beats your stylesheet.
  • Your rule is in a layer and the other one isn't, or is in a later layer. Specificity never gets a vote.
  • An ID is hiding in the other selector. One ID beats any number of classes. Wrap it in :where() to neutralise it.
  • A list inside :is() or nesting gave the other rule the specificity of its strongest argument.
  • The other rule comes later, in the same file or a stylesheet loaded after yours.
  • It isn't a cascade problem. The property may not apply to that element, or it's inherited from a parent instead of set directly.

The fixes, gentlest first: lower the winner with :where(), move rules into ordered layers, raise the loser by repeating a class (.btn.btn) rather than adding an ID, and only then reach for !important. The duel suggests all four for your selectors.

More from the same author on suprabhat-dev.com.