:is() vs :where(): Similar Selectors, Different Specificity
CSS :is() and :where() can match the same elements and still produce different results. The difference appears when another rule tries to override them.
That makes the choice useful when writing defaults for a component, rather than just shortening a selector.
View code
<label><input id="override" type="checkbox" checked> Apply the .muted class to both headings</label>
<section class="where-article"><h3>:where(.where-article, .where-sidebar) h2</h3><h2 class="muted">Related reading</h2></section>
<section class="is-article"><h3>:is(.is-article, .is-sidebar) h2</h3><h2 class="muted">Related reading</h2></section>
<p class="note">The same .muted rule overrides the :where() default, but loses to the :is() selector. All declarations are normal and unlayered.</p>button, select { padding: .35rem .6rem; border: 1px solid var(--clg); background: var(--cw); color: var(--cb); }
input[type="range"] { width: min(100%, 18rem); vertical-align: middle; }
h2, h3 { font-size: 1rem; }
.note { color: var(--clg); }
section { margin-top: 1rem; padding: .75rem; border: 1px solid var(--clg); }
h3 { margin: 0 0 .5rem; overflow-wrap: anywhere; font-family: ui-monospace, monospace; font-weight: 400; }
h2 { margin: 0; }
:where(.where-article, .where-sidebar) h2 { color: var(--cl); }
:is(.is-article, .is-sidebar) h2 { color: var(--cl); }
.muted { color: var(--clg); }document.querySelector('#override').addEventListener('change', event => {
document.querySelectorAll('h2').forEach(heading => heading.classList.toggle('muted', event.target.checked));
});Keep defaults easy to override with :where()
<section class="article">
<h2 class="muted">Related reading</h2>
</section>
:where(.article, .sidebar) h2 {
color: navy;
}
.muted {
color: dimgray;
}
Here .muted wins. The selectors inside :where() add no specificity, so the first rule gets its specificity only from h2.
Replace :where() with :is() and the navy heading wins instead. Its selector includes the specificity of a class plus an element. This assumes ordinary declarations in the same cascade layer, with no competing inline or important styles.
:is() uses the strongest argument
An ID inside the list changes the priority of the whole selector:
:is(.article, #featured) h2 {
color: navy;
}
Even a heading matched through .article gets the specificity contributed by #featured. The browser uses the most specific argument, rather than the argument that happened to match. MDN documents this distinction.
That is an easy detail to miss when a tidy selector suddenly becomes difficult to override.
Choose how easy the rule should be to override
For component defaults and reset rules, :where() is a useful way to leave room for later customisation. For grouping selectors whose priority you want to retain, :is() is usually the more direct choice.
MDN’s specificity guide explains how these functions fit into selector priority. Neither removes the need to understand the cascade. They let you separate which elements a rule selects from how hard that rule should be to override.