CSS Specificity Calculator
Enter a CSS selector to see its specificity weight.
What the (a, b, c) triple means
Specificity is written as a triple (a, b, c) and compared left to right. Column a counts ID selectors such as #main. Column b counts classes such as .post, attribute selectors such as [type="text"], and pseudo-classes such as :hover. Column c counts type selectors such as a and pseudo-elements such as ::before. When two rules target the same element, the browser sorts them by this triple, not by source order.
The numeric score is a convenience for quick reading: with fewer than 100 items in each column, a bigger score always means a more specific selector. Browsers themselves compare the triple lexicographically, so there is no real carrying between columns — 101 classes do not outweigh 1 ID, even though the compressed score would suggest otherwise.
Worked example at the default selector
The default selector #main .post a:hover decomposes into four parts. The ID #main contributes 1 to column a. The class .post contributes 1 to column b, the pseudo-class :hover contributes another 1 to column b, and the type selector a contributes 1 to column c. The result is (1, 2, 1), scored as 1 × 10000 + 2 × 100 + 1 = 10,201.
A rule written as .post a alone would score (0, 1, 1), and #main .post would score (1, 1, 0). Both lose to #main .post a:hover, the first because its a column is 0, the second because the b and c columns compare 1,0 against 2,1 — the ID matches, then the extra class and pseudo-class tip the balance.
Specificity of common selectors
| Selector | (a, b, c) | Why |
|---|---|---|
| * | (0, 0, 0) | The universal selector contributes nothing |
| p | (0, 0, 1) | One type selector |
| p::before | (0, 0, 2) | Type plus pseudo-element, both in column c |
| .card | (0, 1, 0) | One class |
| a:hover | (0, 1, 1) | One pseudo-class plus one type |
| #nav .item | (1, 1, 0) | ID plus class |
| #main .post a:hover | (1, 2, 1) | This calculator’s default |
The table shows the pattern behind every override: a single ID outranks any number of classes, and a single class outranks any number of types. That is why styling rebooted by one #id rule is so hard to undo from a stylesheet that only uses classes.
What sits above specificity
Three mechanisms outrank the triple entirely. Inline style attributes beat every selector. Declarations marked !important beat non-important ones of any specificity. And when both are important, origin layers decide: author !important styles lose to user-agent !important styles, a reversal designed for accessibility overrides. This calculator measures selector specificity only, which is the layer you work in most of the day.
The modern additions
CSS Cascade Layers sit below inline styles but above normal declarations: anything in a later layer beats everything in earlier layers regardless of specificity. Within selectors themselves, :is() and :not() take the specificity of their most specific argument, so :is(#main, .card) counts as an ID. :where() always counts as zero, which makes it the standard tool for writing safely overridable base styles.
Common specificity mistakes
- Chaining an ID into every rule (#main .post a:hover instead of .post a:hover), which forces later overrides to chain the same ID again
- Reaching for !important to beat one stubborn selector, which then must be beaten by another !important in a wider war
- Forgetting that attribute selectors count in column b, so a[href] is as strong as a class
- Assuming source order can fix a specificity loss — order only breaks ties between equal triples
- Treating the numeric score as a real base-1000 number and expecting 100 classes to beat 1 ID
The fix that scales
Keep most rules in the (0, 1, 0) range by styling single classes, add a second class only when you truly need a variant, and reserve IDs for JS hooks or state attributes such as [aria-expanded]. A stylesheet where the heaviest selector is (0, 2, 0) never needs !important to resolve its own conflicts.
How to use the result when debugging
When a rule fails to apply, compute both triples and compare column by column. If the winner has a bigger a, no class-based fix can beat it — either remove the ID from the competing rule or match it. If the b columns tie and c decides, adding one class to your selector wins cleanly. The Hex Color Mixer Calculator and Color Hex Calculator are handy companions once the cascade battle is settled and you are picking the actual values.
Frequently asked questions
Which wins, one ID or ten classes?
The ID wins. Specificity compares column a first, and any selector with a ≥ 1 beats every selector with a = 0, no matter how large its class count. Ten classes give (0, 10, 0), which loses to (1, 0, 0).
What is the specificity of #main .post a:hover?
(1, 2, 1). The ID #main fills column a, the class .post and pseudo-class :hover fill column b, and the type selector a fills column c. The numeric score is 1 × 10000 + 2 × 100 + 1 = 10,201.
Do pseudo-classes count like classes?
Yes. :hover, :focus, :nth-child and friends each add 1 to column b. Pseudo-elements such as ::before and ::after add to column c instead, so a:hover is stronger than a::before.
Does the universal selector have specificity?
No. * contributes (0, 0, 0), so it loses to every real selector. Combinators such as >, + and ~ contribute nothing either; only the simple selectors on each side of them count.
How do inline styles and !important interact with specificity?
Inline styles beat every selector, and !important beats every non-important declaration. Only an !important declaration in your stylesheet beats an inline style, and only because important declarations reverse the normal ordering.
What specificity does :is() have?
The specificity of its most specific argument. :is(#main, .card) counts as an ID, while :is(p, .card) counts as one class. :where() always contributes zero regardless of its arguments.
Can 100 classes beat 1 ID?
No. The triple comparison has no carrying between columns: (0, 100, 0) still loses to (1, 0, 0). The numeric score 10,000 vs 10,201 is only a display shortcut that holds while each column stays below 100.
Why is my later rule being ignored?
Almost always specificity, not order. Source order only breaks ties between equal triples. Compute both selectors here; if the earlier rule has a higher triple, raising your selector by one class or pseudo-class will fix it without !important.
Do attribute selectors count as classes?
Yes. [type="text"] or [href] each add 1 to column b exactly like a class, so a[href] scores (0, 1, 1), the same as a.link.
Method and sources
- CSS Selectors Level 4 specification, specificity calculation rules.