How Does CSS Specificity Work? The Weight Diagram
You probably won’t find this on Reddit, but Håkon Wium Lie’s CERN proposal of 10 October 1994 let style sheets claim influence as percentages, then admitted that for the more specific rule to win, "a precise definition of the rules for this is needed".
Every why is my CSS not applying
thread ends with the same answer: specificity.
I came to this page as the researcher: on December 2, 2026 I read the W3C Selectors Level 4 and Cascading Level 6 drafts and the original 1994 style sheet proposal instead of the blogs that quote each other, then scored 33 selectors and about 40 clashes in Chrome 158.
Three dates matter.
On October 10, 1994 Håkon Wium Lie asked for a precise definition of which rule is more specific.
The Selectors draft of January 22, 2026 gives it as three counts.
And the Nesting draft of the same day shows one case where nested code scores higher than flat code.
CSS specificity is the score a selector gets, and the rule with the higher score wins when two rules style the same thing.
If you like a printed reference beside the keyboard, the CSS Visual Dictionary paperback on Amazon draws selectors and properties as pictures.
Short answer: count the IDs (A), then the classes, attributes and pseudo-classes (B), then the element types and pseudo-elements (C), and compare A-B-C from left to right. #nav a is 1-0-1 and beats .menu .item .link .x at 0-4-0, because A is compared before B.
The same five scores as text:
a= 0-0-1 (one type)..menu a= 0-1-1 (one class, one type).#nav a:hover= 1-1-1 (one ID, one pseudo-class, one type).#nav .menu .item.on= 1-3-0 (one ID, three classes).:where(#nav) a= 0-0-1 (:where()counts as zero, so only theascores).
How do I calculate the specificity of a selector?
Add one to A for every ID, one to B for every class, attribute selector or pseudo-class, and one to C for every element type or pseudo-element. The universal selector * and the combinators (space, >, +, ~) add nothing.
That is the wording of the W3C Selectors Level 4 draft, and MDN’s Specificity page says the same with the names ID, CLASS and TYPE. Repeating a simple selector counts again: .x.x.x is 0-3-0.
We did not trust the lists, so we asked Chrome 158. For each selector we built reference selectors of every A-B-C size and checked which one it tied with (the later rule wins a tie in both orders).
| Selector | Measured A-B-C | What it shows |
|---|---|---|
* and :where(#t) | 0-0-0 | both lose to a plain p in either order and tie with each other |
html body main section p | 0-0-5 | five types, still below one class |
[id="t"] | 0-1-0 | an attribute selector that matches an ID counts as a class |
[data-k="v"][data-k] | 0-2-0 | every attribute selector counts |
p:first-child | 0-1-1 | a pseudo-class counts like a class |
#t#t | 2-0-0 | repeating an ID counts twice |
#t.x.y[data-k] | 1-3-0 | one ID and three class-level parts |
:nth-child(1) | 0-1-0 | one pseudo-class |
:nth-child(1 of .x) | 0-2-0 | the pseudo-class plus the .x inside its argument |
:nth-child(1 of .x, #t) | 1-1-0 | the pseudo-class plus the most specific selector in the list |
In one line: A counts IDs, B counts everything with a dot, bracket or single colon, C counts element names, and * counts nothing.
Can I check a selector without counting by hand?
Yes, use the box below. Type one or two selectors and it prints A-B-C and which one wins. It runs in your browser, sends nothing anywhere, and scores the selector alone: it assumes the same origin and layer, no !important and no style attribute. For a comma list it shows the highest score in the list.
We checked this calculator against the 33 selectors measured in Chrome plus 20 more from the spec and MDN examples. It matched 52 of the 53; the exception is a plain comma list such as p, #zzz, which it scores by its highest member, while a browser scores the part that matched.
Which wins, the selector with more parts or the one with a bigger left number?
The bigger left-most number wins, no matter how many parts the other selector has. A is compared first; B is only looked at when A ties; C is only looked at when A and B both tie.
MDN's own example makes the point: #myElement (1-0-0) beats .bodyClass .sectionClass .parentClass [id="myElement"] (0-4-0), though the second has four parts. And html body main input (0-0-4) loses to :root input (0-1-1) because B is compared before C.
Say your .title (0-1-0) does nothing because a plugin ships .card .title (0-2-0). The plugin wins on B, so the fix is a selector with a B of 2 or more, or the layers in the override section below, not a bigger C.
Can enough classes ever beat one ID?
No. In Chrome 158 one ID beat a selector of 65,536 repeated classes in either order, so the old "256 classes beat an ID" story does not hold there. What does exist is a cap: each column stops counting at 255.
The Selectors draft allows this: values higher than the limit must be clamped to that limit, and not overflow
. We found the cap by binary search, comparing a selector with m repeated parts against one with 70,000 (3,000 for types).
| Column | Smallest count that ties with a huge one | Compared with the next column |
|---|---|---|
| A (IDs) | 255 | A is the top column, so there is nothing for it to spill into |
| B (classes) | 255 | one ID still beat 70,000 classes |
| C (types) | 255 | one class still beat 3,000 types |
So 255 classes and 256 classes tie in Chrome 158 (the later rule wins), and neither spills into A. Nobody writes selectors like that on purpose; the number only matters if generated CSS piles up repeats.
How do :is(), :not(), :has() and :where() change specificity?
:is(), :not() and :has() take the score of the most specific selector inside their brackets, :where() always scores zero, and :nth-child() counts as one pseudo-class plus whatever follows of. The pseudo-class itself adds nothing for the first three.
The spec's example is :is(em, #foo) has a specificity of (1,0,0)
even against an em that has no ID. Chrome 158 agreed: :is(p, #zzz) on our p measured 1-0-0, where the plain list p, #zzz measured 0-0-1 because only the p part matched.
| Selector | A-B-C | Why |
|---|---|---|
:is(p) | 0-0-1 | the score of p |
:is(.x, #t) | 1-0-0 | the highest in the list |
:is(p, #zzz) | 1-0-0 | counted even though #zzz matches nothing |
p, #zzz (a plain list) | 0-0-1 | only the part that matched counts |
:not(.zzz) | 0-1-0 | the argument's score, not the pseudo-class's |
p:not(.zzz, #zzz) | 1-0-1 | p plus the highest in the list |
p:has(+ #w) | 1-0-1 | p plus the ID in the argument |
.x:where(#t.x.x) | 0-1-0 | only the .x outside :where() |
The practical use is the :where() row. A base stylesheet written as :where(.btn) scores 0-0-0, so any later one-class rule overrides it. MDN says the same: :where() always has its specificity replaced with zero
.
What happens when two selectors have the same specificity?
The rule written last wins, once nothing earlier in the cascade has settled it. If both rules live in @scope blocks with different roots, the nearer root wins before the order of writing is looked at.
Håkon Wium Lie's first proposal said the opposite about order. In the 10 October 1994 text he wrote that The order of the statements in the style sheets is irrelevant
and that the more specific statements override the more general ones. Today's CSS reads order as the final tie-break.
We measured the scope step too. With @scope (section) { p { color: red } } written first and an unscoped p { color: blue } after it, red won, so proximity beat order of appearance. A .x rule after it still won, because specificity comes before proximity.
| Rules | Winner | Why |
|---|---|---|
@scope (section) { p } red, then p blue | red | proximity beats order |
@scope (section) { p } red, then .x blue | blue | specificity (0-1-0) beats proximity |
@scope (section) { :scope p } red, then .x blue | red | :scope p is 0-1-1 |
@scope (section) { p } red, then @scope (main) { p } blue | red | section is nearer than main, though written first |
Scope proximity is a step in the Cascading and Inheritance Level 6 draft (Working Draft, 6 September 2024); the Level 5 snapshot of 13 January 2022 we read has no such step and never mentions @scope. Can I use puts @scope at 93.23% of users, so check your audience first.
Do !important, inline styles, layers and animations beat specificity?
Yes, all four act before specificity is looked at. The cascade checks origin and importance first, then the style attribute, then cascade layers, and only then specificity, as the ladder shows.
The ladder as text:
- Origin and importance:
!importantfirst, then animations, then normal declarations. - The
styleattribute, for the same importance. - Cascade layers: later layer wins, unlayered beats layered, reversed for
!important. - Specificity, A-B-C.
- Scope proximity.
- Order of appearance.
The W3C Cascading and Inheritance Level 6 draft lists the same order under Cascade Sorting Order
. We tested each step with colours that could not be confused.
| Clash | Winner |
|---|---|
style="color:red" against #t#t#t (3-0-0) | the style attribute |
style="color:red" against p { color: blue !important } | the !important rule, though it is only 0-0-1 |
both !important, #t#t against p | the higher specificity, #t#t |
@layer a { #t } against unlayered p | the unlayered p |
layer a declared before b: a { #t#t#t } against b { p } | layer b, the later one |
the same two layers, both !important | layer a, the earlier one |
layered !important against unlayered #t#t#t !important | the layered one |
inside one layer, #t against p | #t, specificity decides |
a directly targeted * (0-0-0) against #s#s#s set on the parent | the * rule: inherited values lose to any rule aimed at the element |
Our pick is to use layers to rank whole stylesheets (reset, framework, your own) and keep specificity for choosing between rules inside one layer. Can I use puts Cascade Layers at 97.09% of users.
Does CSS nesting change specificity?
Yes, in one surprising way. A nested & takes the highest specificity in its parent's selector list, exactly like :is(), so nested rules can score higher than the same rules written out flat.
Here's a moment almost every newbie knows: you write a rule, refresh, and nothing changes, because some other rule you forgot about is quietly scoring higher. It is not a bug in your browser. Two rules are in a fight, and the scoreboard is a three-number count.
The Nesting draft (Working Draft, 22 January 2026) gives an example: #a, b { & c { color: blue } } against .foo c { color: red } on <b class=foo><c>. It says the text will be blue
, because & c is 1-0-1.
We ran it. Chrome 158 painted the c blue. Flat, the rule would have been b c at 0-0-2, and .foo c (0-1-1) would win, so the same rules written flat painted it red.
| CSS | Result | Why |
|---|---|---|
#a, b { & c { blue } } against .foo c { red } | blue | & scores the highest parent item, #a |
the same rules written flat: #a c, b c in blue | red | the matching b c is only 0-0-2 |
article { color: blue; & { color: red } } | red | equal score, the nested rule comes after |
article { color: blue; :where(&) { color: red } } | blue | :where() makes the nested score zero |
#s { p { red } } against .x { blue } | red | the nested p inherits the parent's ID: 1-0-1 |
The spec explains the trade-off: matching :is()-style is required for performance reasons
, since tracking which selector matched would slow selector matching. Can I use puts CSS Nesting at 93.39% of users.
Where do MDN, the specs and a common claim disagree?
They disagree in three places. We list each claim next to what Chrome 158 did, and the first row is one we found no ranking page correcting.
| Question | One source | Another source | Chrome 158 |
|---|---|---|---|
| What can override a style attribute? | MDN: The only way to override inline styles is by using !important | Cascading Level 6: animation declarations rank above normal author declarations | an animation to blue beat style="color:red"; !important on the attribute beat the animation |
| Can many classes beat an ID? | a common claim (we cite no page for it): 256 classes beat one ID | Selectors 4: values are clamped, never overflow | one ID beat 65,536 classes; each column stops at 255 |
| Is nested code the same as flat code? | MDN: nesting is very similar to the :is() pseudo-class | Nesting draft: it can differ from the expanded rules | blue nested, red flat for the same rules |
| Do plain lists and :is() score alike? | Selectors 4: a plain list scores the part that matches | Selectors 4: :is() scores the highest, matched or not | p, #zzz 0-0-1; :is(p, #zzz) 1-0-0 |
The first row means an @keyframes animation can override an inline style without !important. That is the one place MDN's sentence is too strong for normal declarations, though its advice to use !important on a targeted selector still works.
How do I override a rule without !important?
Match its score and write yours later, lower the score of the rule you can edit, or put the other stylesheet in an earlier layer. MDN names the same three routes and adds that @layer is the standard way to beat third-party CSS.
| Method | Example | Who it suits | Where it fails |
|---|---|---|---|
| Equal score, written later | .card .title after the same selector | your own stylesheet | load order changes |
| Lower the old score | rewrite #nav a as :where(#nav) a | CSS you own | none, but every later override now wins |
| Put third-party CSS in a layer | @import "lib.css" layer(lib); | frameworks you cannot edit | their !important and inline styles still win |
| Repeat a class | .btn.btn.btn | a one-off patch | hard to read, so comment it |
| Attribute instead of ID | [id="nav"] a (0-1-1) | matching an ID with less weight | no pseudo-element reach |
Our pick: write low-specificity, class-only selectors (0-1-0 and 0-2-0), put anything you do not own in a layer, and never use an ID in a stylesheet selector. That keeps every override one step away.
Skip this advice if you must fight an inline style from a script: there a targeted !important is the honest tool. Our guides on the box model and CSS position show other places where one wrong rule hides behind a stronger one.
Do :is(), :has(), layers and nesting work in every browser?
Yes for most visitors: the features below reach 93 to 97 percent of users, with @scope and nesting at the low end. Can I use's data file, updated November 28, 2026, lists each feature below.
:is()- 97.17% of users
- selector list inside
:not() - 97.23% of users
:has()- 96.09% of users
- Cascade Layers (
@layer) - 97.09% of users
- CSS Nesting
- 93.39% of users
@scope- 93.23% of users
Count the history. In 1994 Håkon Wium Lie's proposal let a sheet ask for 40%
influence, and a later sheet could be granted the other 60%; his example mixed two newspaper styles as 30 percent and 70 percent. Today a property has exactly one winner, and the whole decision above is a ladder of six steps.
Which means for your day-to-day work: nothing is averaged, so a rule either wins at the first step that differs or it does nothing at all. Ask which step decided it, and the answer is almost always step 4 or step 6.
How we researched this
- Specifications read on December 2, 2026: W3C Selectors Level 4 (Working Draft, 22 January 2026), CSS Cascading and Inheritance Level 6 (Working Draft, 6 September 2024) and Level 5 (Candidate Recommendation Snapshot, 13 January 2022), and CSS Nesting Module Level 1 (Working Draft, 22 January 2026).
- The unusual source: Håkon Wium Lie, "Cascading HTML style sheets: a proposal", v0.92, 10 October 1994, hosted by the W3C. Its percentage-influence examples, its "order is irrelevant" line and its 30/70 mix of two newspaper sheets are the facts we used.
- Documentation: MDN's Specificity page and the Can I use data file (updated November 28, 2026).
- Searches: Google autocomplete for "css specificity", "css specificity vs", "css specificity important" and "css specificity calculator" on December 2, 2026. The headings and the calculator follow those suggestions.
- Our own tests: three small pages in headless Chrome 158.0.8127.91 (macOS): 33 selectors scored by tie tests, a binary search for each column's cap, and more than 40 clashes between inline styles,
!important, animations, layers, nesting and@scope. One browser, one run each. - What we did not do: we did not test Firefox or Safari, so the numbers are Chrome's, and we counted no user threads or surveys.
Open a page you made, find one rule that does not apply, and write both selectors into the calculator above. One look at the two A-B-C scores and the fight is over, no !important needed. Ten minutes of that and specificity stops being a mystery and becomes a scoreboard.
Sources and further reading
- W3C: Selectors Level 4 - Working Draft, 22 January 2026: how specificity is calculated, :is(), :not(), :has(), :where(), :nth-child(), clamping
- W3C: CSS Cascading and Inheritance Level 6 - Working Draft, 6 September 2024: Cascade Sorting Order, scope proximity
- W3C: CSS Cascading and Inheritance Level 5 - Candidate Recommendation Snapshot, 13 January 2022: cascade layers, no scope proximity
- W3C: CSS Nesting Module Level 1 - Working Draft, 22 January 2026: specificity of &
- Håkon Wium Lie: Cascading HTML style sheets, a proposal - v0.92, 10 October 1994: percentage influence, order irrelevant, 30/70 mix of two sheets
- MDN Web Docs: Specificity - weight categories, :where(), inline styles, !important, layers
- Can I use: :is() - 97.17% of users, data file updated November 28, 2026
- Can I use: Cascade Layers - 97.09% of users, data file updated November 28, 2026
- Can I use: CSS Nesting - 93.39% of users, data file updated November 28, 2026
- Can I use: @scope - 93.23% of users, data file updated November 28, 2026
Questions and answers
What is CSS specificity and how is it calculated?
Specificity is the score that decides which rule wins when two rules style the same element. Count the IDs (A), then the classes, attributes and pseudo-classes (B), then the element types and pseudo-elements (C), and compare A-B-C from left to right. #nav a:hover is 1-1-1.
Which CSS selector has the highest specificity?
The one with the highest left-most count. One ID (1-0-0) beats any number of classes, and in Chrome 158 it beat a selector of 65,536 repeated classes. Above all selectors sit the style attribute, animations and !important, which are settled before specificity.
Does an ID always beat a class in CSS?
Yes, when both rules are in the same origin and layer and neither is !important. A is compared before B, so #a (1-0-0) beats .a.b.c.d (0-4-0). A class in a later layer or an unlayered rule can still beat an ID in an earlier layer.
Does !important override specificity?
It overrides it, because importance is checked first. A rule with !important beats a normal rule of any specificity, and when two !important rules clash, specificity decides between them. We measured p { color: blue !important } beating a style attribute.
How can I increase specificity without !important?
Make your selector score higher, for example .card .title instead of .title, repeat a class (.btn.btn), or put the other stylesheet in an earlier @layer. Better still, lower the score of the rule you are fighting, for example with :where().