CSS selectors and specificity

Quick reference for CSS selectors: common patterns, specificity, and the :is, :where, and :not rules, complementary to the CSS tutorial.

This page is a selector and specificity quick reference. It deepens the tutorial chapter “Connect styles and select elements” without replacing that lesson, its example, or the #selectors anchor.

Specificity is a triple (IDs, classes/attributes/pseudo-classes, types/pseudo-elements). It is not the whole cascade: origin, importance, and cascade layers can outweigh a higher specificity. Source order matters only after those comparisons, as the tutorial already notes.

Examples are complete HTML and CSS fragments. They were parsed with PostCSS and a selector AST parser for syntax and structure. They were not rendered in a browser here, and this is not a visual or accessibility test.

Official sources: MDN and the CSS specification. Fragments were checked with PostCSS 8 for syntax and structure. That is not a CSS conformance suite, and this is not a browser render or visual test.

CSS reference catalog → · CSS tutorial overview →

Common selector syntax

A rule is a selector list plus a declaration block. A comma-separated list is a union of selectors. Combinators join simple or compound selectors: a descendant space, a child >, an adjacent sibling +, and a general sibling ~.

Syntax or propertyInitial / defaultNotes
*—The universal selector. It has zero specificity in Selectors Level 4.
type (div, p)—Matches that element type. Specificity (0, 0, 1).
.class—Matches a class. Specificity (0, 1, 0). A class can be reused, as the tutorial shows with .card.
#id—Matches an ID. Specificity (1, 0, 0).
[attr] / [attr=value]—Attribute selectors have the same specificity as a class: (0, 1, 0).
:hover, :focus, :nth-child()—Most pseudo-classes count as (0, 1, 0). :is, :where, and :not are different; see the next sections. Pseudo-elements such as ::before count as (0, 0, 1).

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 propertyInitial / defaultNotes
: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.
html
<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.
html
<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 →