Learn CSS Visually - CSS explained one diagram at a time - flexbox, grid, the box model and more

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

By · Updated

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.

Table drawn as three coloured columns labelled A for ID selectors, B for classes, attributes and pseudo-classes, and C for element types and pseudo-elements. Five example selectors are scored: a is 0-0-1, .menu a is 0-1-1, #nav a:hover is 1-1-1, #nav .menu .item.on is 1-3-0 and :where(#nav) a is 0-0-1. An arrow underneath says compare A first, then B, then C.
The three specificity counts and five scored examples; compare left to right.

The same five scores as text:

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

Specificity measured by Learn CSS Visually in Chrome 158 (158.0.8127.91, macOS) on a p element inside html, body, main and section, December 2, 2026; every row matched the Selectors Level 4 rules
SelectorMeasured A-B-CWhat it shows
* and :where(#t)0-0-0both lose to a plain p in either order and tie with each other
html body main section p0-0-5five types, still below one class
[id="t"]0-1-0an attribute selector that matches an ID counts as a class
[data-k="v"][data-k]0-2-0every attribute selector counts
p:first-child0-1-1a pseudo-class counts like a class
#t#t2-0-0repeating an ID counts twice
#t.x.y[data-k]1-3-0one ID and three class-level parts
:nth-child(1)0-1-0one pseudo-class
:nth-child(1 of .x)0-2-0the pseudo-class plus the .x inside its argument
:nth-child(1 of .x, #t)1-1-0the 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.

Type a selector.

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

Where each specificity column stops counting - measured by Learn CSS Visually in Chrome 158, December 2, 2026
ColumnSmallest count that ties with a huge oneCompared with the next column
A (IDs)255A is the top column, so there is nothing for it to spill into
B (classes)255one ID still beat 70,000 classes
C (types)255one 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 functions measured by Learn CSS Visually in Chrome 158 on a p element, December 2, 2026
SelectorA-B-CWhy
:is(p)0-0-1the score of p
:is(.x, #t)1-0-0the highest in the list
:is(p, #zzz)1-0-0counted even though #zzz matches nothing
p, #zzz (a plain list)0-0-1only the part that matched counts
:not(.zzz)0-1-0the argument's score, not the pseudo-class's
p:not(.zzz, #zzz)1-0-1p plus the highest in the list
p:has(+ #w)1-0-1p plus the ID in the argument
.x:where(#t.x.x)0-1-0only 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.

Tie-breaks measured by Learn CSS Visually in Chrome 158, December 2, 2026 (red or blue is the colour that was applied)
RulesWinnerWhy
@scope (section) { p } red, then p blueredproximity beats order
@scope (section) { p } red, then .x bluebluespecificity (0-1-0) beats proximity
@scope (section) { :scope p } red, then .x bluered:scope p is 0-1-1
@scope (section) { p } red, then @scope (main) { p } blueredsection 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.

Ladder of six numbered boxes in the order the cascade checks them: origin and importance, the style attribute, cascade layers, specificity (highlighted in pink as step 4), scope proximity and order of appearance. A downward arrow beside it says to stop at the first step that differs.
The six cascade steps in order; specificity is step 4.

The ladder as text:

  1. Origin and importance: !important first, then animations, then normal declarations.
  2. The style attribute, for the same importance.
  3. Cascade layers: later layer wins, unlayered beats layered, reversed for !important.
  4. Specificity, A-B-C.
  5. Scope proximity.
  6. 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.

Which declaration won - measured by Learn CSS Visually in Chrome 158, December 2, 2026
ClashWinner
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 pthe higher specificity, #t#t
@layer a { #t } against unlayered pthe unlayered p
layer a declared before b: a { #t#t#t } against b { p }layer b, the later one
the same two layers, both !importantlayer a, the earlier one
layered !important against unlayered #t#t#t !importantthe layered one
inside one layer, #t against p#t, specificity decides
a directly targeted * (0-0-0) against #s#s#s set on the parentthe * 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.

Nesting measured by Learn CSS Visually in Chrome 158, December 2, 2026
CSSResultWhy
#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 blueredthe matching b c is only 0-0-2
article { color: blue; & { color: red } }redequal 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 }redthe 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.

Where the sources differ - compiled by Learn CSS Visually from MDN's Specificity page, W3C Selectors Level 4 (Working Draft, 22 January 2026), Cascading and Inheritance Levels 5 and 6, the Nesting draft and our Chrome 158 tests, December 2, 2026
QuestionOne sourceAnother sourceChrome 158
What can override a style attribute?MDN: The only way to override inline styles is by using !importantCascading Level 6: animation declarations rank above normal author declarationsan 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 IDSelectors 4: values are clamped, never overflowone 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-classNesting draft: it can differ from the expanded rulesblue nested, red flat for the same rules
Do plain lists and :is() score alike?Selectors 4: a plain list scores the part that matchesSelectors 4: :is() scores the highest, matched or notp, #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.

Ways to win without !important - compiled by Learn CSS Visually from MDN's Specificity page and our Chrome 158 tests, December 2, 2026
MethodExampleWho it suitsWhere it fails
Equal score, written later.card .title after the same selectoryour own stylesheetload order changes
Lower the old scorerewrite #nav a as :where(#nav) aCSS you ownnone, but every later override now wins
Put third-party CSS in a layer@import "lib.css" layer(lib);frameworks you cannot edittheir !important and inline styles still win
Repeat a class.btn.btn.btna one-off patchhard to read, so comment it
Attribute instead of ID[id="nav"] a (0-1-1)matching an ID with less weightno 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

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().

How this was written: Written from the W3C Selectors Level 4 (Working Draft, 22 January 2026), CSS Cascading and Inheritance Levels 6 (Working Draft, 6 September 2024) and 5 (Candidate Recommendation Snapshot, 13 January 2022), CSS Nesting Module Level 1 (Working Draft, 22 January 2026), Håkon Wium Lie's proposal of 10 October 1994, MDN Web Docs' Specificity page, Can I use, and Google autocomplete for "css specificity", "css specificity vs", "css specificity important" and "css specificity calculator", all read on December 2, 2026. Every score and winner in the tables is a measurement we took in Chrome 158 on that date. Code samples are simplified for learning.

Oh, one more thing...

If this helped, you'll like our free updates: the insights we discover while researching, and the moments worth sharing with other people learning CSS - no spam, no promotions.

One short email when there's something worth your time. Unsubscribe any time.