Specificity: How CSS Decides Which Selector Wins
How browsers weigh selectors against each other: the three specificity columns, what counts in each, :is(), :not(), :has(), :where() and :nth-child(of), why columns never carry over, inline styles and !important, and practical ways to keep specificity low and predictable.
kvi5hnuCode Sprout team member9 min readOn this page
When two rules set the same property on the same element, the browser needs a way to pick one. After origin, inline styles and cascade layers (article 17 explains the whole order), it compares the specificity of the selectors: roughly, how precisely each selector describes the element. The more specific selector wins, and if they're equal, the later rule wins.
h2 { color: #333; }
.book-card h2 { color: #8a3b12; } /* more specific: this wins */
The three columns
Specificity is a set of three numbers, usually written A,B,C:
| Column | Counts | Examples |
|---|---|---|
| A | ID selectors | #cart |
| B | class selectors, attribute selectors, pseudo-classes | .sale, [type="email"], :hover, :nth-child(2) |
| C | type selectors and pseudo-elements | p, h2, ::before |
These count nothing:
- the universal selector
* - combinators: space,
>,+,~ :where()and everything inside it

Some examples:
li /* 0,0,1 */
nav a /* 0,0,2 */
.price /* 0,1,0 */
a:hover /* 0,1,1 */
.book-card .title /* 0,2,0 */
input[type="checkbox"] /* 0,1,1 */
p::first-line /* 0,0,2 */
#cart .count /* 1,1,0 */
body > main ul li.sale /* 0,1,4 */
Comparing specificity
Specificities are compared column by column from the left. The first column that differs decides, and the later columns don't matter:
1,0,0beats0,15,0: one ID outweighs any number of classes.0,1,0beats0,0,12: one class outweighs any number of types.0,2,1beats0,2,0.
The columns never carry over. Specificity isn't a decimal number in which eleven classes add up to an ID. (Some old browsers stored each column in 8 bits, so 256 classes could overflow into the ID column. Current browsers don't: in Chrome, even 300 classes lose to one ID.)
If the specificities are equal, the declaration that comes later wins: later in the stylesheet, or in a stylesheet loaded later.
.book-card .title { color: #8a3b12; } /* 0,2,0: wins, although it comes first */
article h2 { color: #1d4ed8; } /* 0,0,2 */
h2.title { color: #2f7d4f; } /* 0,1,1 */
h2 { font-family: Georgia, serif; } /* 0,0,1: no conflict, still applies */
Specificity is compared for each property separately. The losing rule's other declarations still apply. Here, font-family from h2 applies because nothing else sets it.

Each panel adds one rule, and each new rule wins only because its specificity is higher. The more specific rules are written first in these stylesheets, so the order isn't what decides. The last panel shows that classes never add up to an ID.
Functional pseudo-classes
The pseudo-classes that take selectors have their own rules:
| Selector | Specificity |
|---|---|
:is(...), :not(...), :has(...) | that of the most specific selector in the argument list |
:where(...) | always 0,0,0 |
:nth-child(An+B of S), :nth-last-child(An+B of S) | one pseudo-class (0,1,0) plus the most specific selector in S |
:is(h2, .title) /* 0,1,0: the class is the most specific argument */
:is(h2, #lead) /* 1,0,0, even for an h2 that isn't #lead */
li:not(.sold-out) /* 0,1,1 */
.card:has(> img) /* 0,1,1 */
:where(#nav) a /* 0,0,1 */
li:nth-child(2 of .sale) /* 0,2,1 */
The rule for :is() surprises people: the specificity of the most specific argument applies, whichever argument actually matched. :is(h2, #lead) gives every h2 an ID's weight.

The element doesn't have the ID in either panel. Only the :is() version carries the ID's weight anyway.
Selector lists
In a selector list, each selector is a separate rule with its own specificity:
h1, .title, #page-title { color: #8a3b12; }
/* behaves as three rules: 0,0,1 / 0,1,0 / 1,0,0 */
If an element matches more than one selector in the list, the most specific matching one is used. This is different from :is(h1, .title, #page-title), which is a single selector with the specificity 1,0,0 for every element it matches.
IDs, attribute selectors and inline styles
An ID selector and an attribute selector that matches the same id have very different weights:
#newsletter { } /* 1,0,0 */
[id="newsletter"] { } /* 0,1,0 */
Inline styles, in the style attribute, have no selector at all, so they have no specificity. The cascade puts them in a separate step, before specificity: an inline declaration beats every normal declaration in the author's stylesheets, whatever its selector. Only !important declarations, animations and transitions can override inline styles (article 17).
!important isn't specificity
!important doesn't add specificity. It moves the declaration into a separate group, which is compared before specificity:
.note { color: #555 !important; }
#intro .note { color: #222; } /* loses: it isn't important */
Specificity only decides between declarations that are both important or both normal. Article 17 explains where !important fits in the cascade and why to keep it rare.
Specificity of other selectors
- Pseudo-elements count in column C, like types:
p::beforeis 0,0,2. Legacy single-colon forms such as:beforealso count as pseudo-elements, not pseudo-classes. :hostcounts as a pseudo-class (0,1,0).:host(.featured)adds the argument's specificity: 0,2,0.::slotted(p)counts as a pseudo-element plus its argument: 0,0,2.::part(label)takes a name, not a selector, so it counts as one pseudo-element: 0,0,1.- Nested rules (article 21):
&has the specificity of the parent selector list, as if it were wrapped in:is(). @scope(article 22): the scope root adds no specificity to the rules inside it, unless they use:scope(0,1,0) or&.
Seeing specificity in DevTools
Chrome and Edge show a selector's specificity when you hover over it in the Styles pane (article 4). The Styles pane also lists rules from the winner down, and crosses out declarations that lost.
Keeping specificity under control
Most specificity problems come from rules that are more specific than they need to be. Each override then needs an even more specific selector, and a stylesheet turns into a specificity war. A few habits prevent it:
- Style with classes, one or two per selector. Keep IDs for links, labels and scripts (article 7).
- Avoid long descendant chains like
.sidebar .widget ul li a. A single class on the element, such as.widget-link, is shorter, faster to read, and easy to override. - Don't qualify classes with types (
a.button) unless the type matters. - Use
:where()for defaults that should lose to almost anything: resets, base styles, library styles. - Use cascade layers (article 20) to order whole groups of styles, so that a layer can win regardless of the specificity of its selectors.
- When you must raise specificity, do it deliberately. Repeating a class (
.button.button) adds 0,1,0 without needing a new class, and:is(#id)adds an ID's weight without a real ID in the markup. Use either sparingly, with a comment.
What's current
- The specificity rules for
:is(),:not(),:has(),:where()and:nth-child(of S)work the same in all current browsers. - DevTools in Chromium browsers show a selector's specificity when you hover over it.
- Cascade layers and
@scopereduce the need to manage specificity by hand (articles 20 and 22).
Common questions
Why doesn't my rule apply, although it comes later in the file? Source order only decides between equal specificities. If an earlier rule has a more specific selector, it wins. Check the Styles pane in DevTools: the losing declaration is crossed out, and the winning one is listed above it.
Do 11 classes beat an ID? No. Columns are compared from the left, and the ID column decides first. No number of classes adds up to an ID.
Does * have specificity?
It has zero specificity, 0,0,0. That's different from no specificity at all: a * rule still matches, so * { color: gray; } beats a color the element would otherwise inherit, because inherited values have no specificity and lose to any declaration that matches the element itself (article 16).
Is :is() the same as writing the selectors out as a list?
They match the same elements, but the specificity differs. A list gives each selector its own specificity. :is() gives every match the specificity of its most specific argument.
How do I override a rule I can't change, such as one from a library?
Write a selector at least as specific as the library's and load it later, or put the library in a lower cascade layer with @import url(lib.css) layer(vendor); so that your unlayered styles win regardless of specificity (article 20).
Summary
- Specificity has three columns: IDs (A), classes, attributes and pseudo-classes (B), and types and pseudo-elements (C).
*, combinators and:where()add nothing.- Columns are compared from the left and never carry over. Equal specificity means the later declaration wins.
:is(),:not()and:has()take the specificity of their most specific argument.:nth-child(of S)adds a pseudo-class to that.- Each selector in a list has its own specificity. The cascade compares specificity for each property separately.
- Inline styles and
!importantaren't specificity: they're separate steps in the cascade. - Keep specificity low and flat with classes,
:where()and cascade layers.
Practice questions
Calculate the specificity of each selector: ul li.active a, #menu :hover, :where(.nav) a[href], .card:not(#hero, .wide).
Answer
ul li.active a: 0,1,3#menu :hover: 1,1,0:where(.nav) a[href]: 0,1,1.card:not(#hero, .wide): 1,1,0, because:not()takes its most specific argument, the ID
Which color does the link get?
.footer a { color: gray; }
a.footer-link { color: navy; }
footer a:hover { color: teal; }
<footer class="footer"><a class="footer-link" href="/about">About</a></footer>
Answer
When it isn't hovered, the first two rules match, both with 0,1,1, so the later one wins: navy. When it's hovered, footer a:hover matches too. It has two types and a pseudo-class, 0,1,2, which beats both 0,1,1 rules, so the link turns teal.
A stylesheet contains the rule below. A new design needs some buttons in the sidebar to be green, using a class .button-primary. What are your options, from best to worst?
#sidebar .button { background: gray; }
Answer
Best: lower the original rule's specificity, for example by changing it to .sidebar .button or :where(#sidebar) .button, so that .button-primary can win with a similar selector. Next: put the old styles in an earlier cascade layer. Workable: match the specificity, as in #sidebar .button-primary, placed later. Worst: !important, which starts an escalation that the next change will have to beat.
Why does :is(p, #intro) make it harder to override styles for ordinary paragraphs?
Answer
:is() takes the specificity of its most specific argument, so the rule has an ID's specificity (1,0,0) for every paragraph it matches, not only for #intro. Overriding it for any paragraph then needs an ID-level selector. Writing p, #intro as a list, or using :where(), avoids that.
Previous: Styling Web Components: , ::slotted(), ::part() and ()

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.