CSS Specificity Calculator

Paste a selector (or a comma-separated list) to get its (a, b, c) specificity with a part-by-part breakdown, or compare two selectors to see which one wins.

Examples:
#nav .item:hover

specificity (1, 2, 0)

1

IDs

2

Classes

0

Types

  • #nav 1,0,0
  • .item 0,1,0
  • :hover 0,1,0

Legend

IDs: #id

Classes: .class, [attr], :hover and other pseudo-classes

Types: div, ::before and other pseudo-elements

Grey parts (*, :where()) add nothing. Combinators (space, >, +, ~) are not counted.

Why a rule "doesn't apply"

When two CSS rules set the same property on the same element, the browser picks one with the cascade. After origin, !important and cascade layers, the next tie-breaker is specificity: a score computed from the selector alone. Most "my style is ignored" bugs come down to a selector that is less specific than another one you forgot about. This calculator shows the exact score of any selector and explains where every point comes from, so you can fix the selector instead of reaching for !important.

How to use it

  1. Type or paste a selector into CSS selector, or click one of the Examples. Only the selector: leave out the { … } block.
  2. Read the score: three numbers for IDs, Classes and Types. The coloured chips underneath show what each part of the selector contributed; hover a chip for an explanation.
  3. If you pasted a list such as h1, .title, you get one row per selector, because each has its own specificity.
  4. Tick Compare with a second selector to type a rival selector. The banner tells you which one wins and why; on a tie, the rule written later in the stylesheet wins.
  5. Use Copy result to paste the selector and its score into a code review or ticket.

Worked example

Take a button that refuses to turn red:

#sidebar a:hover          → (1, 1, 1)
.btn.btn-danger:hover     → (0, 3, 0)
.card :is(.title, #main)  → (1, 1, 0)
:where(#app) .btn         → (0, 1, 0)
p::before                 → (0, 0, 2)
li:nth-child(2n+1 of .x)  → (0, 2, 1)

The first rule wins over the second even though it has fewer classes, because it has one ID and the ID column is compared first. The third line shows how :is() takes the score of its strongest argument (#main), even when the element actually matched.title. The fourth shows the opposite tool: wrapping a selector in:where() removes all of its weight, which is why design systems use it for easily overridable defaults.

The counting rules

  • IDs (a): every #name.
  • Classes (b): every .class, attribute selector like [type="text"], and pseudo-class like :hover or :nth-child(2).
  • Types (c): every element name like div or svg|circle, and every pseudo-element like ::before. The old single-colon forms :before, :after, :first-line and :first-letter also count here.
  • Zero: the universal selector *, combinators, and :where() with everything inside it.
  • Arguments: :is(), :not() and :has() add the score of their most specific argument and nothing for themselves. :nth-child(An+B of S) adds one pseudo-class plus the most specific selector in S.

Limitations

The calculator scores selectors; it does not know your stylesheet. It cannot tell you about rule order, cascade layers (@layer), !important, inline styles or scoping, all of which are decided before or after specificity. The nesting selector& is counted as zero with a warning, because its real weight is that of the parent rule. The parser checks structure (brackets, strings, combinators) but does not validate that a pseudo-class name actually exists, so a typo like :hovr still counts as a pseudo-class.

Tips

  • Prefer classes over IDs for styling. An ID makes a rule hard to override without another ID.
  • Keep selectors short. nav ul li a span is (0, 0, 5) and forces every override to be at least as long.
  • Use :where() for resets and library defaults, and @layer when you need whole groups of rules to lose regardless of specificity.
  • Remember that :not(.a, #b) is as strong as its strongest argument, which can surprise you.

Working on styles? Shrink the result with the CSS minifier, or build effects with the CSS box-shadow generator.

Frequently Asked Questions

How is CSS specificity calculated?

Specificity is three counters (a, b, c): a counts ID selectors, b counts classes, attribute selectors and pseudo-classes, and c counts type selectors and pseudo-elements. Two selectors are compared column by column from the left, so the first column that differs decides the winner.

Can 11 classes beat one ID?

No. Specificity is not a base-10 number: (0, 11, 0) still loses to (1, 0, 0) because the ID column is compared first. Any number of classes never outweighs a single ID.

What specificity do :is(), :not(), :has() and :where() have?

:is(), :not() and :has() take the specificity of their most specific argument, so :not(#id) is (1, 0, 0). :where() always contributes zero, whatever is inside it.

How are selector lists like "h1, .title" handled?

Each selector in a comma-separated list has its own specificity; they are never added together. The tool shows one row per selector, and when comparing it uses the most specific selector of each list.

Do inline styles and !important count as specificity?

Not as part of the (a, b, c) score. Inline style attributes beat any selector in the same origin and layer, and !important declarations are sorted before specificity is even considered, so the calculator only covers selectors.

Is my CSS sent to a server?

No. The selector is parsed by JavaScript running in your browser and nothing is uploaded or stored.