CSS Specificity Calculator
Paste any CSS selector and get its specificity score broken into the three components the cascade uses: ID count, class/attribute/pseudo-class count, and element/pseudo-element count. The result tells you exactly why one rule wins over another when both target the same element.
Why Specificity Wins Cascade Fights
CSS resolves conflicts between competing rules using specificity before it falls back to source order. A selector's specificity is a three-number tuple — commonly written (a,b,c) or 0-0-0. a counts ID selectors, b counts classes, attributes, and pseudo-classes, and c counts element and pseudo-element selectors. Higher beats lower, comparing left to right. (0,1,0) (one class) beats (0,0,9) (nine elements) every time, because the class column outranks the element column regardless of how many elements stack up.
Inline styles add a fourth column above all three. !important sidesteps the whole calculation and creates its own priority lane, with !important inline styles beating !important author rules beating !important user rules. The cascade gets predictable once you can name each rule's score.
How the Calculator Works
The selector gets parsed into atoms. Each #id adds 1 to column a. Each .class, [attr], or :pseudo-class adds 1 to column b. Each tag name or ::pseudo-element adds 1 to column c. The universal selector * and combinators (>, +, ~) contribute zero. Two CSS Level 4 wrinkles get special handling: :where() always contributes zero, no matter what's inside it, and :is() and :not() take the specificity of their most-specific argument.
Cascade Layers (@layer) sit above specificity entirely — a layered rule loses to any unlayered rule regardless of selector score, which is why you can ship reset CSS in a layer and have author rules win effortlessly.
When Specificity Bites
Most specificity bugs come from one of three patterns: a long selector chain like div.container ul.nav li.item a that's hard to override without copying the chain; an !important declaration in a vendor stylesheet that forces a second !important just to match it; and an inline style= attribute set by JavaScript that wins against any author rule that isn't !important. Knowing the score makes the fix obvious: lower the original specificity, use a layer, or, as a last resort, match the !important. Use the CSS button generator or the CSS animation generator to produce overridable single-class rules.
Worked example: 30 selectors against a reference parser
On October 12, 2026 we ran the tool's own calculation on 30 selectors (the ten examples on this page plus attribute selectors with dots and colons, :has(), :host, ::part, ::marker, namespaces and :root) and compared each with @bramus/specificity, a parser that follows Selectors Level 4. The old regex counter agreed on 26 of 30; the four it got wrong are below. The tool now runs that parser itself (version 2.4.2, self-hosted), so all 30 match, and a selector it cannot parse is marked as not valid instead of getting a made-up score.
| Selector | Old counter (a,b,c) | Selectors Level 4 | Why |
|---|---|---|---|
li:nth-child(2 of .item) | 0,2,2 | 0,2,1 | "of" is counted as an element |
:is(:not(.a), #b) | 1,2,0 | 1,0,0 | nested functions are cut at the first ")" |
::slotted(span) | 0,1,0 | 0,0,2 | treated as a pseudo-class, argument ignored |
.a\:hover (escaped colon, as Tailwind writes it) | 0,2,0 | 0,1,0 | the escaped ":hover" is counted as a pseudo-class |
- Everyday selectors are right. IDs, classes, attributes (including values with dots, colons and slashes), pseudo-classes, pseudo-elements, combinators, :where() at zero and single-level :is()/:not()/:has() all matched the reference.
- Nested functions and utility-class names are now counted correctly. Functions inside functions, the “of S” form of :nth-child, ::slotted() and escaped characters such as Tailwind’s .sm\:flex are parsed by a real selector parser (css-tree, via @bramus/specificity) rather than counted with regular expressions; re-run on October 12, 2026, all 30 test selectors matched.
- Specificity is only one step of the cascade. Before it, the browser compares origin and !important, then cascade layers (@layer), then the @scope proximity; a selector in an earlier layer loses to any selector in a later layer, whatever its score. After it, equal specificity is decided by source order.
- Inline styles beat every selector (they are not part of the (a,b,c) count), and !important on a declaration in a stylesheet beats inline styles without it.
- The score is shown as a,b,c. Compare column by column from the left: one ID (1,0,0) beats any number of classes (0,15,0).
Sources
Checked on .
- W3C Selectors Level 4, section 17: Calculating a selector's specificity (:is/:not/:has, :nth-child(… of S), :where)
- W3C CSS Scoping: ::slotted() specificity (pseudo-element plus its argument)
- @bramus/specificity 2.4.2 (MIT), the parser the tool now uses
- W3C CSS Cascading and Inheritance Level 5: cascade layers come before specificity
Frequently Asked Questions
Built by Derek Giordano · Part of Ultimate Design Tools