← Back to HTML & CSS, page 6

html-css-0119 · Hard senior

How do :is() and :where() differ in specificity, and how can :where() make a stylesheet easier to override?

:is() takes the specificity of its strongest argument, while :where() always counts as zero. Use :where() for base styles so any later rule can override them, and fix strong legacy selectors step by step, with layers for larger changes.

Full answer

Both :is() and :where() take a list of selectors and match any one of them. This shortens long rules: :is(h1, h2, h3) a replaces three rules. The difference is in specificity.

  • :is() takes the specificity of its most specific argument. :is(#nav, .menu) a counts the id, so it is 1,0,1, even when the element matched the class.
  • :where() always has zero specificity. :where(#nav, .menu) a counts only the a, so it is 0,0,1.

Zero specificity is very useful for defaults, such as a reset or a base style in a library or a design system. Any normal rule written later or earlier can override it, with a plain class, and nobody needs a longer selector or !important. This is how many specificity fights start in old code: a base rule like #page .content ul li a is so strong that every override must be even stronger.

A way to clean up legacy CSS in small steps:

  1. Do not rewrite everything. Wrap new base styles in :where().
  2. For old strong selectors that you can edit, lower them to a class one at a time and test.
  3. Use @layer for larger changes, so layer order beats specificity.
  4. Reserve :is() for convenience, and remember that it can raise specificity.

Also note that :is() and :where() ignore invalid selectors in the list, instead of dropping the whole rule, which is called forgiving parsing.

The first link stays gray. The :where() rule has only the specificity of a class, which is lower than #site a. The second link is green. The :is() rule counts the id, so it beats the plain .link2 blue rule. The button is blue, because the :where(.btn) default has zero specificity, and .primary beats it easily.

Measure with the specificity shown in the browser tools before you restructure rules.

html
<style>
  #site a { color: gray; }
  :where(#site) .link { color: green; }
  :is(#site, .x) .link2 { color: green; }
  .link2 { color: blue; }
  :where(.btn) { background: #eee; padding: 4px; }
  .primary { background: #0a58ca; }
</style>
<div id="site">
  <a class="link" id="a" href="#">One</a>
  <a class="link2" id="b" href="#">Two</a>
</div>
<button class="btn primary" id="c" type="button">Go</button>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • :is() uses the highest specificity in its list.
  • :where() has zero specificity.
  • Use :where() for defaults and resets.
  • Lower strong legacy selectors one by one.
  • Layers can order large groups of rules.

Common mistakes

  • Using :is() in a base style and unintentionally raising its specificity.
  • Fighting a strong legacy selector with an even stronger selector or !important.
  • Rewriting all legacy CSS at once instead of making small safe steps.

Follow-ups

  • What is forgiving selector parsing?
  • How do cascade layers help with legacy overrides?

Tips

  • State both specificities in one line each.
  • Present :where() as the tool for easy-to-override defaults.