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
pand 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.
| 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.
| 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.
:where() counts nothing| 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.
| 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.
| 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.
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
This text is
& .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:
- Origin and importance. Transitions, then
!importantbrowser, user and author styles, then animations, then normal author, user and browser styles. - Context. The page versus a shadow tree.
- Element-attached styles. The
styleattribute beats rules from stylesheets. - Layers. The order of
@layerblocks, and unlayered styles. - Specificity. The
(A, B, C)comparison. - Scope proximity. How close the
@scoperoot is. - 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.
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
This text is
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.
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
This text is
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:
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
This text is
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.
CSS and HTML
#card#card { color: navy; }
<p id="card" style="color: crimson">This text is</p> Rendered by your browser
This text is
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
styleattribute, 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.