How CSS Specificity Works (And Why Your Style Isn't Applying)
CSS specificity determines which rule wins when multiple selectors target the same element. Learn the calculation and how !important overrides it.
Published September 21, 2026
When several CSS rules target the same element, the one with the highest specificity wins, then the later one wins a tie. Specificity is counted as IDs, then classes/attributes/pseudo-classes, then elements.
The concept
Think of specificity as three numbers (a, b, c): a = IDs, b = classes, attributes and pseudo-classes, c = element and pseudo-element selectors. Compare left to right.
Inline styles beat all selectors, and !important beats inline styles. The universal selector adds nothing.
Scoring selectors
p 0,0,1
.btn 0,1,0
nav .btn 0,1,1
#header .btn 1,1,0 ← wins
Practical examples
Why the style loses
.card p { color: blue; } /* 0,1,1 */
p.note { color: red; } /* 0,1,1 — later wins */Debug in DevTools
Elements → Styles pane: crossed-out rules lost the cascade.Avoid !important with :where()
:where(.card) p { color: gray; } /* zero specificity */Cascade layers
@layer base, components;
@layer base { a { color: blue; } }Common mistakes
- Reaching for !important instead of fixing the selector.
- Using IDs for styling then being unable to override them.
- Deeply nested selectors that cannot be overridden.
Key takeaways
- Count IDs, classes, elements.
- Equal specificity → later rule wins.
- Keep selectors flat and class-based.
FAQ
Does order matter?
Yes, only when specificity is equal.
Does inheritance beat specificity?
No, a directly matched rule beats an inherited value.