Short answer
CSS specificity is the tuple (A, B, C): A counts ID selectors, B counts classes, attributes, and pseudo-classes, and C counts type selectors and pseudo-elements. Selectors Level 4 says to ignore the universal selector and to compare A, then B, then C. A larger A wins even when the other selector has more total pieces. The style attribute is a separate, earlier cascade step.
Last verified against the sources listed below.
Key takeaways
- Selectors Level 4 calculates specificity as the tuple (A, B, C), compared from A to C.
- The spec's UL OL LI.red example is (0, 1, 3). Its #x34y example is (1, 0, 0).
- :is(), :not(), and :has() take the most specific argument. :where() is zero.
- Cascade Level 5 compares a style attribute before it compares selector specificity.
- Combinators do not add to the count. Repeating a simple selector does.
How is CSS specificity calculated? Count three integers and compare them from the left. Selectors Level 4, a W3C Working Draft dated 22 January 2026, defines the count. It is not a sum you can treat as a single base-10 number, and it is not the first thing the cascade checks.
MDN describes specificity as the weight used to choose a declaration after the browser has already picked the winning cascade origin and layer. A class in your stylesheet does not beat a user-agent rule by having a larger tuple. Origin comes first.
The formula
For one selector, Selectors Level 4 says to calculate:
- A equals the number of ID selectors.
- B equals the number of class selectors, attribute selectors, and pseudo-classes.
- C equals the number of type selectors and pseudo-elements.
- Ignore the universal selector.
Write the result as (A, B, C). MDN writes the same three columns as ID, CLASS, and TYPE. web.dev uses the same triad and the same left-to-right comparison.
Compare A first. The larger A is more specific, then B if A ties, then C if B ties. If all three tie, the specificities are equal.
The spec says an implementation may clamp a component that does not fit in storage, and it must not let a huge B overflow into A. That is why (1, 0, 0) beats (0, 4, 3) in the web.dev lesson. The single ID is more specific than four class-like pieces and three type-like pieces.
Combinators such as the descendant space, >, +, and ~ do not add a count. MDN says they change what matches, not the weight. Repeating a simple selector does add. The spec says repeated occurrences of the same simple selector are allowed and do increase specificity.
A worked example from the spec
Take the selector the spec annotates as a=0 b=1 c=3:
UL OL LI.red { color: red; }
Count it once:
- ID selectors: none. A is 0.
- Class selectors:
.redis one. Attribute selectors: none. Pseudo-classes: none. B is 1. - Type selectors:
UL,OL, andLIare three. Pseudo-elements: none. C is 3. - The spaces between those pieces are combinators. They add 0.
The tuple is (0, 1, 3).
Now take the spec’s #x34y, annotated a=1 b=0 c=0. One ID, no class, no type. The tuple is (1, 0, 0).
Compare (1, 0, 0) with (0, 1, 3). A is 1 against 0, so the ID wins, and the longer selector does not get a bonus for having four pieces. That is the numeric result the comparison rule requires.
A second spec line is .foo :is(.bar, #baz), annotated a=1 b=1 c=0. .foo contributes one class, so B gets 1 from outside the function.
Inside :is(), .bar is (0, 1, 0) and #baz is (1, 0, 0). The most specific argument is #baz because its A is larger. The function is replaced by that argument, not added as an extra pseudo-class, and the total is (1, 1, 0).
The spec’s own list, copied as tuples:
| Selector in the spec | Tuple (A, B, C) |
|---|---|
* |
(0, 0, 0) |
LI |
(0, 0, 1) |
UL LI |
(0, 0, 2) |
UL OL+LI |
(0, 0, 3) |
H1 + *[REL=up] |
(0, 1, 1) |
UL OL LI.red |
(0, 1, 3) |
LI.red.level |
(0, 2, 1) |
#x34y |
(1, 0, 0) |
#s12:not(FOO) |
(1, 0, 1) |
.foo :is(.bar, #baz) |
(1, 1, 0) |
H1 + *[REL=up] is the check on the universal selector. * adds nothing, H1 adds one type, [REL=up] adds one attribute, and the + adds nothing. The tuple is (0, 1, 1), which matches the comment in the spec.
#s12:not(FOO) is (1, 0, 1) because :not() is replaced by its argument. FOO is one type. The ID is one. The function itself is not an extra point in B.
What is CSS specificity and how is it calculated for functions?
The special cases are in the same section of Selectors Level 4.
:is(), :not(), and :has() do not keep a weight of their own. Each one is replaced by the most specific complex selector in its argument list. The spec’s :is(em, #foo) is (1, 0, 0), like the ID, whether it matches em or the element with that ID. :not(em, strong#foo) is (1, 0, 1), like strong plus #foo, against any element.
:where() is replaced by zero. The spec’s .qux:where(em, #foo#bar#baz) is (0, 1, 0). Only .qux counts. web.dev uses that to show a later type selector overriding a :where() that contains an ID.
:nth-child() and :nth-last-child() are different. They count as one pseudo-class, then add the most specific selector in the of list if there is one. The spec says :nth-child(even of li, .item) is (0, 2, 0): one pseudo-class plus the class .item, which beats the type li inside that list.
For a comma-separated list, score each selector alone. The match uses the most specific selector in the list that actually matches. MDN walks through [type="password"] at 0-1-0, input:focus at 0-1-1, and :root #myApp input:required at 1-2-1. A focused password field takes the weight of the selector that matched, not a blend of all three.
Where the tuple sits in the cascade
Cascade Level 5 sorts declarations in this order: origin and importance, encapsulation context, element-attached styles, cascade layers, specificity, then order of appearance. Specificity is step five. It never rescues a declaration that already lost on origin.
The origin list, from highest to lowest, starts with transition declarations, then important user-agent, important user, and important author declarations, then animations, then normal author, normal user, and normal user-agent. MDN’s worked cascade shows a more specific user rule losing to a normal author rule because origin is decided first.
Element-attached styles, including a style attribute, beat other declarations of the same importance before specificity runs. web.dev says you do not outrank that attribute by lengthening a selector. !important also skips the specificity contest against a normal declaration, because it changes origin. When both sides are important, specificity applies again inside that origin.
Layers sit above specificity too. Cascade Level 5 says a normal declaration in a later layer wins over an earlier layer even when its specificity is lower. The spec’s note shows an unlayered 0,0,1 beating a layered 0,1,1 for normal rules, because unlayered styles are the implicit final layer.
If the tuples tie inside the winning layer, order of appearance picks the winner. The last declaration in document order wins. Imported sheets are ordered as if they were pasted in place of @import. Linked sheets follow the document’s link order.
CSS is that whole cascade, not the tuple alone.
The box model is a set of properties this weight can assign. A class selector does not change the property. It only changes which declaration is allowed to set it.
A button generator will usually emit classes. Each class adds 1 to B, and two classes on one selector are (0, 2, 0), which is enough to beat one class and not enough to beat one ID.
Frequently asked questions
What is CSS specificity and how is it calculated?
MDN calls specificity the weight the cascade uses to pick which declaration applies, after origin and importance. Selectors Level 4 calculates it for a selector by counting three groups. A is the number of ID selectors. B is the number of class selectors, attribute selectors, and pseudo-classes. C is the number of type selectors and pseudo-elements. The universal selector is ignored. You compare the tuples from the left, so one ID beats any number of classes.
Does a style attribute count as 1,0,0?
Not in CSS Cascading and Inheritance Level 5. That spec sorts a declaration attached to the element, such as a style attribute, before it sorts by selector specificity. Element-attached styles of a given importance beat selector-mapped declarations of the same importance. web.dev's specificity lesson says the same thing: the style attribute is an earlier cascade step, not a bump to the (A, B, C) count. Override it from an earlier step, such as an important author declaration, not by adding more classes.
Do commas add the counts together?
No. Selectors Level 4 says a selector list is scored per selector. For a given match, the specificity in effect is the most specific selector in the list that matches. MDN's example is a password field that can match an attribute selector, a focus selector, or an ID selector, and the weight comes from the matching one with the greatest weight. Writing three selectors in one rule does not produce a sum.
What do :is() and :where() do to the count?
Selectors Level 4 replaces the specificity of :is(), :not(), and :has() with the most specific complex selector in the argument. It replaces :where() with zero, so IDs inside :where() add nothing. :nth-child() and :nth-last-child() count as one pseudo-class, then add the most specific argument if the of-list is present. The spec's :nth-child(even of li, .item) is (0, 2, 0).
What happens when two tuples match?
Selectors Level 4 says the specificities are equal when A, B, and C all tie. Cascade Level 5 then uses order of appearance: the last declaration in document order wins, after layers and the other earlier steps. MDN also notes scoping proximity when two selectors in the winning layer share a specificity. Equal tuples are not a reason to add an ID. Move the rule, or change the selector on purpose.
Sources
- Selectors Level 4 · W3C, 2026
- CSS Cascading and Inheritance Level 5 · W3C, 2026
- Specificity · MDN, 2026
- Specificity · Chrome Developers, 2026
- Selectors Level 4 (editor's draft) · CSS Working Group, 2026