CSS selectors and specificity
Quick reference for CSS selectors: common patterns, specificity, and the :is, :where, and :not rules, complementary to the CSS tutorial.
Specificity, :is, :where, and :not
Add the components of a compound selector to get its specificity. div.card is (0, 1, 1). #hero is (1, 0, 0) and beats div.card when both declarations have the same importance and layer.
Do not treat :is() as a zero-specificity grouping tool. In Selectors Level 4, the specificity of :is(), :not(), and :has() is the specificity of the most specific argument in the selector list. :where() is the same kind of grouping, but its specificity is always (0, 0, 0).
A common mix-up is to give :not() a class-level weight of its own and then add the argument, so :not(#b) would be wrongly listed as (1, 1, 0). :not(#b) is (1, 0, 0). :not(.a) is (0, 1, 0) only because .a itself is (0, 1, 0), not because :not always counts as a class. :hover still counts as (0, 1, 0) on its own.
| Syntax or property | Initial / default | Notes |
|---|
:is(…) | — | Matches any argument. Specificity is that of the most specific argument, even if a less specific argument is what actually matches. |
:where(…) | — | Matches any argument, like :is(), but the specificity of the pseudo-class is always (0, 0, 0). Useful for optional overrides that should not inflate specificity. |
:not(…) | — | Matches when none of the arguments match. Specificity is that of the most specific argument, with no extra weight for the :not name itself. |
:is, :where, and :not compared
:is(#hero, .promo)
:where(#hero, .promo)
:not(.a, #b)
.card:is(#hero, .promo)
Worked specificities: :is() and :not() take the most specific argument; :where() stays at zero.
- The numbers below are Selectors Level 4 calculations on the selector AST, not a browser cascade measurement.
<article id="hero" class="card promo">Featured card</article>
<article class="card">Ordinary card</article>
<style>
.card { color: #1B1814; }
.card:is(#hero, .promo) { background: #F3EFE4; }
.card:where(#hero, .promo) { outline: 1px dashed #0B5853; }
.card:not(.promo, #hero) { opacity: 0.85; }
</style>
What this markup means
.card is (0, 1, 0). .card:is(#hero, .promo) is (1, 1, 0) because :is() contributes (1, 0, 0) from #hero, plus .card. .card:where(#hero, .promo) stays (0, 1, 0) because :where() contributes (0, 0, 0). .card:not(.promo, #hero) is also (1, 1, 0), from #hero inside :not(), not from an extra class weight on :not itself. The ordinary card is the one :not() is written to match.
Related: CSS tutorial: selectors, Display
MDN documentation → · CSS specification →
Grouping with :is() versus a long list
:is(h1, h2, h3).title
h1.title, h2.title, h3.title
:is() groups alternatives without repeating a compound selector. Its specificity still follows the most specific argument.
- h1.title, h2.title, and h3.title are each (0, 1, 1). :is(h1, h2, h3).title is also (0, 1, 1), because the :is() arguments are type selectors.
<section>
<h1 class="title">Garden planner</h1>
<h2 class="title">Weekly tasks</h2>
<p class="title">Not a heading.</p>
</section>
<style>
:is(h1, h2, h3).title { font-family: sans-serif; letter-spacing: 0.01em; }
p.title { font-weight: normal; }
</style>
What this markup means
The heading rule matches h1.title and h2.title through :is(). The paragraph has class title too, but it is not an h1, h2, or h3, so only p.title applies to it. Because the :is() arguments are type selectors, this rule is not more specific than a simple h2.title rule would have been.
Related: CSS tutorial: selectors
MDN documentation → · CSS specification →