CSS Specificity
When two rules set the same property on the same element, which one wins? The browser decides using the cascade, and one of its key steps is specificity: a score calculated from the selector. The more specific selector wins, no matter where it appears in the stylesheet.
How the cascade decides
In order of priority:
- Origin and importance —
!importantdeclarations beat normal ones; author styles beat browser defaults. - Cascade layers (
@layer) — later layers beat earlier ones. - Specificity — the higher score wins.
- Source order — if everything else ties, the rule that comes last wins.
Inline style="…" attributes beat any selector in a stylesheet.
Calculating specificity
Specificity is three numbers, often written (A, B, C):
| Column | Counts | Examples |
|---|---|---|
| A | ID selectors | #header |
| B | Classes, attribute selectors, pseudo-classes | .btn, [type=text], :hover |
| C | Type selectors and pseudo-elements | p, div, ::before |
| (none) | Universal selector and combinators | *, >, +, ~, space |
Compare column by column from the left. One ID beats any number of classes; one class beats any number of type selectors.
| Selector | Specificity |
|---|---|
p |
(0, 0, 1) |
ul li a |
(0, 0, 3) |
.nav |
(0, 1, 0) |
a:hover |
(0, 1, 1) |
.nav .item.active |
(0, 3, 0) |
#main |
(1, 0, 0) |
#main .nav a |
(1, 1, 1) |
style="…" |
beats all of the above |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; font-size: 20px; }
#title { color: green; } /* (1,0,0) wins */
.heading.big { color: blue; } /* (0,2,0) */
h1.heading { color: orange; } /* (0,1,1) */
h1 { color: red; } /* (0,0,1) */
</style>
</head>
<body>
<h1 id="title" class="heading big">What colour am I?</h1>
<p>Green: the ID selector wins even though it is written first.</p>
</body>
</html>Delete the #title rule and run again: blue wins next, because (0,2,0) beats (0,1,1).
Source order breaks ties
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.box { padding: 16px; color: white; font-weight: bold; }
.warning { background: #ff9800; }
.danger { background: #e53935; }
</style>
</head>
<body>
<div class="box danger warning">class="danger warning"</div>
<p>Red, because <code>.danger</code> comes later in the CSS. The order of class names in the HTML does not matter.</p>
</body>
</html>Tip: The order of classes in the
classattribute never affects which rule wins — only the order of the rules in the stylesheet.
Special cases
| Selector | Specificity rule |
|---|---|
:is(a, #b) / :not(…) / :has(…) |
Take the specificity of their most specific argument |
:where(…) |
Always zero — great for easily overridden defaults |
* |
Zero |
| Inherited values | Have no specificity — any rule targeting the element directly beats inheritance |
The last row surprises many people:
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
#article { color: purple; } /* very specific, but on the parent */
a { color: #1e88e5; } /* targets the link directly */
:where(.card) h3 { color: #00897b; } /* (0,0,1) thanks to :where */
h3 { color: #f4511e; } /* (0,0,1), comes later — wins */
</style>
</head>
<body>
<div id="article">Purple text with a <a href="#">blue link</a> — the link's own rule beats the inherited purple.
<div class="card"><h3>Orange: :where() adds zero specificity</h3></div>
</div>
</body>
</html>Keeping specificity low
High-specificity selectors make CSS hard to override later, leading to "specificity wars" and !important everywhere. Good habits:
- Style mostly with single classes (
.card-title) rather than IDs or long chains. - Avoid IDs in CSS; keep them for anchors and JavaScript.
- Use
:where()for base styles you expect to be customised. - Use your browser's DevTools: hovering a selector shows its specificity, and overridden declarations are crossed out.
Test yourself
What is the specificity of nav .link:hover?
- (0,1,1)
- (0,2,1)
- (1,1,1)
- (0,2,0)
Which wins: #a or .b.c.d.e.f.g.h.i.j.k.l (11 classes)?
- The 11 classes
- #a
- Whichever is later
- Neither
What is the specificity of :where(#nav .item)?
- (1,1,0)
- (0,1,0)
- (0,0,0)
- (1,0,0)