CSS specificity is the weight a browser gives each selector to decide which rule wins when several rules set the same property on one element. The weight has three columns, IDs, then classes, then element types, and the browser compares them left to right. If the scores tie, the rule written last wins.
How CSS specificity is calculated
CSS specificity is calculated by counting three kinds of selector parts into three separate columns. The Selectors Level 4 rules define them like this:
| Column | What counts | Example selector | Score |
|---|---|---|---|
| A (ID) | ID selectors | #nav |
1-0-0 |
| B (class) | classes, attribute selectors, pseudo-classes | .card, [type="radio"], :hover |
0-1-0 |
| C (type) | element types and pseudo-elements | p, h1, ::before |
0-0-1 |
| none | universal selector * and combinators |
*, >, +, ~, a space |
0-0-0 |
You add up each column for the whole selector. A few worked scores from the specificity reference:
#myElement input.myClassscores 1-1-1.input[type="password"]:requiredscores 0-2-1.html body main inputscores 0-0-4.[id="myElement"] input.myClassscores 0-2-1, because[id="..."]is an attribute selector, not an ID.
That last one is useful: it targets an element by its id without the ID weight.
Why the columns are not a single number
The columns are compared one at a time, starting from the left, so a higher column always beats any amount in a lower one. It is not a base-10 sum. One ID beats a selector with no IDs, whatever the class and type counts are, and a class always beats any number of types. The specificity lesson gives (1,0,0) beating (0,4,3) as the example.
People often ask whether 11 classes overflow into the ID column. They don't. I put one #t1 rule against .a.b.c.d.e.f.g.h.i.j.k (11 classes) on the same element in Chromium 156 and read the color with getComputedStyle. The ID won.
What :is(), :not(), :has() and :where() do to the score
The functional pseudo-classes :is(), :not() and :has() add nothing themselves; they take the score of the most specific selector inside them, while :where() always scores zero.
That rule has two surprises.
- An unused ID still counts.
:is(p, #fakeId)scores 1-0-0 even when nothing on the page has that id. In my test,:is(u, #nope)beat.t3.t3, although#nopematched no element. :where()throws its contents away. The:where()page says it always has 0 specificity.:where(#t2)lost to a bareemselector in my test.
Wrap a reset or base style in :where() and any component class overrides it.
Two related cases follow the same pattern. :nth-child(An+B of S) counts as one pseudo-class plus the most specific selector in S, so :nth-child(even of li, .item) scores (0,2,0). And the & in native CSS nesting behaves like :is(). Under #a, b { & i {} }, every match of & i scores 1-0-1, even on an <i> inside a plain <b>. In my test, that nested rule beat .foo i (0-1-1).
Where specificity sits in the cascade
Specificity is only one step of the cascade, and the steps before it can override any score. The cascade guide lists the order as relevance, origin and importance, specificity, scoping proximity, then order of appearance.
Three things beat a high score before it is even compared:
- Inline styles. A
style="..."attribute is a separate, earlier step, not part of the score. You can think of it as 1-0-0-0. - !important. It does not raise specificity. It moves the declaration into a stronger group, so a
.t8rule with!importantbeat an inline style in my test. Between two!importantdeclarations, specificity applies again. - Normal styles inside a cascade layer (
@layer) lose to unlayered styles in the same origin. In my test,#t5inside@layer baselost to an unlayeredspan.t5.
Two things never count. DOM distance is ignored: body h1 and html h1 tie, and the later rule applies. And inherited values never compete with specificity. A rule that targets the element directly always beats a value it inherits, so h1 beat a color inherited from #parent.
Test results: which rule actually won
I ran nine matchups in Chromium 156 on November 26, 2026, and each winner matched what the rules predict. Each rule set color, and I read the result with getComputedStyle.
| Matchup | Winner | Why |
|---|---|---|
#t1 vs 11 chained classes |
ID | ID column compared first |
:is(u, #nope) vs .t3.t3 |
:is() |
takes the ID's weight, matched or not |
:where(#t2) vs em |
em |
:where() is zero |
@layer base { #t5 } vs span.t5 |
span.t5 |
unlayered beats layered |
nested & i under #a, b vs .foo i |
nested rule | & acts like :is() |
inherited from #parent vs h1 |
h1 |
direct rules beat inheritance |
button.btn vs .btn |
button.btn |
0-1-1 beats 0-1-0 |
.t8 { ... !important } vs inline style |
!important |
importance is checked first |
two identical s.t10 rules |
the later one | tie goes to source order |
Limitation: this was one Chromium build. I didn't run it in Firefox or Safari. The table checks the rules; it does not prove cross-browser behavior.
How to keep specificity low
The easiest specificity bugs to fix are the ones you never create, so keep selectors only as specific as they need to be. My take: style with single classes, skip IDs in CSS, and reach for :where() or @layer before you ever type !important.
When you have to override third-party CSS you can't edit:
- Put the library in a cascade layer, so your unlayered rules win without matching its selectors.
- Or repeat a class to bump the score, since
.myClass.myClass.myClass spanscores 0-3-1. - Use
[id="..."]instead of#idwhen you need to target an id but keep the weight low.
To find the rule that is beating yours, open the element in DevTools. The Styles pane strikes through every declaration that lost. If the competing rule lives in a minified vendor file, paste it into a CSS formatter first so you can read its selectors. For more on reading compressed stylesheets, see how to format minified CSS and what CSS minification removes. If your fight is over sizes, em vs rem covers how relative units inherit.
FAQ
Do 11 classes beat one ID in CSS?
No. Specificity columns are compared left to right, so any selector with one ID beats any selector with zero IDs, however many classes it has. A Chromium test with 11 chained classes against one ID confirmed it: the ID won.
Does !important increase specificity?
No. !important does not change the specificity score. It moves the declaration into a higher-priority group of the cascade. Between two !important declarations, specificity decides again.
What is the specificity of :where()?
Always zero, even when it contains an ID. :is(), :not() and :has() are different: they take the specificity of the most specific selector inside them.
What happens when two selectors have the same specificity?
The rule that appears later in the stylesheet wins. Distance in the HTML tree does not matter, so body h1 and html h1 tie and the last one applies.