Sub topic 8
Choosing between :is() and :where()
This chapter assumes you can read compound and complex selectors, and that you know how specificity decides which of two conflicting rules wins. You should also know that :is() and :where() match the same elements and differ only in the specificity they contribute. The goal here is to turn that one difference into a repeatable decision.
The one question that decides it
For any selector you are about to write, ask: should this rule be easy for other rules to override?
- If yes, wrap the alternatives in
:where(). The wrapped part adds nothing to the specificity, so almost any later or more specific rule can replace it. - If no, or if you don't care, use
:is(). The wrapped part adds the specificity of its most specific argument, so the rule behaves like the longhand selector list you would otherwise have written.
The rest of this chapter looks at what that means in practice and where each choice goes wrong.
| Situation | Better choice | Reason |
|---|---|---|
| Shortening a long list of component selectors | `:is()` | The result should have the same specificity as the expanded list. |
| Base styles for a reset, theme or library | `:where()` | Consumers can override the defaults with a plain class selector. |
| A rule that must beat other author rules | `:is()` | Its argument specificity counts toward the cascade. |
| Grouping selectors that contain an ID | Usually `:where()` | `:is(#id, .x)` gives every match the specificity of the ID. |
| Targeting only one element type | Neither | A type selector or a class is already short. |
Using :is() when specificity should count
Use :is() when you are compacting rules that already have the specificity you want. The expanded form and the :is() form below match the same elements.
/* Expanded */
.sidebar h2 a:hover,
.sidebar h3 a:hover,
.sidebar h4 a:hover {
color: darkred;
}
/* Compact */
.sidebar :is(h2, h3, h4) a:hover {
color: darkred;
}The specificity of each rule is the same. .sidebar contributes one class, a contributes one type, and :hover contributes one class. The :is() argument list contributes the most specific item inside it, which here is a type selector. Both rules come to (0,2,2).
Because the specificity is preserved, you can replace a selector list with :is() without changing which rules win. That is the main reason to prefer :is() for refactoring existing styles. If you used :where() instead, .sidebar :where(h2, h3, h4) a:hover would drop to (0,2,1), and a competing rule that used to lose might now win.
Using :where() when others should override
Library code, resets and design-system defaults are meant to be replaced. A default that is hard to override forces consumers into !important or longer selectors. :where() removes that problem.
:where(.prose) :where(h1, h2, h3, h4) {
margin-block: 1.5em 0.5em;
line-height: 1.2;
}
:where(.prose) :where(ul, ol) {
padding-inline-start: 1.5em;
}Both rules have specificity (0,0,0). A consumer can now write this and win:
h2 {
margin-block-start: 0;
}A plain h2 selector has specificity (0,0,1), which beats zero, so it overrides the margin no matter which stylesheet loads first. If the library had written .prose h2, the consumer would have needed at least .prose h2 or another class-based selector to win.
Edge case: zero is not "always loses"
A :where() rule with specificity (0,0,0) loses to any rule with a higher specificity. It only ties with rules that also have zero specificity, such as the universal selector *. When there is a tie, source order decides.
* {
margin: 0;
}
:where(p) {
margin: 1em;
}Both rules are (0,0,0). The second one is later in the source, so paragraphs get 1em margins. If you swap the two rules, paragraphs get 0. Don't assume a :where() rule always loses to *.
A :where() rule still beats the browser's built-in stylesheet. Author styles are in a higher origin than user-agent styles, and origin is compared before specificity. So :where(p) { margin: 1em; } replaces the default paragraph margin even with zero specificity.
A trap: IDs inside :is()
The most common mistake is putting an ID or other high-specificity selector in an :is() list and forgetting that it raises the whole rule.
:is(#app, .page) a {
color: navy;
}
.page a {
color: teal;
}For an a inside .page, you might expect the second rule to win, since it comes later. It doesn't. The first rule's specificity is (1,0,1), because #app is the most specific argument in the list. The second rule is (0,1,1). The first rule wins even for elements that matched only through .page. The ID's weight applies to every match of the rule, not only the matches that go through the ID.
There are three ways to fix it:
- Split the rule into two rules, so each one has the specificity it should.
- Remove the ID from the list.
- Use
:where(#app, .page) a, which gives (0,0,1), if the rule is a default that should be easy to override.
Mixing the two in one selector
You can use both pseudo-classes in the same selector to give different parts of it different weights. The specificity of the whole selector is the sum of its parts, so only the :is() part and any ordinary selectors count.
/* Specificity (0,1,0): .card counts, :where() does not */
.card :where(h2, h3) {
font-size: 1.25rem;
}
/* Specificity (0,0,1): only the trailing "a" counts */
:where(.card, .panel) a {
text-decoration: none;
}This is useful when you want the container to be part of a rule's identity but the list of descendants to be flexible. It also lets you build selectors with a specific, small specificity value, which is easier to reason about than a long compound selector.
Making the choice in practice
The diagram below summarizes the questions to ask.
Some practical guidelines follow from this flow:
- Default to
:where()for anything you ship as a base layer. A reset, theme default or component library benefits from predictable, overridable specificity. - Default to
:is()when you are tidying application styles. The rules should behave the same after the rewrite, and:is()is the form that preserves behavior. - Check the arguments, not just the function. The danger in
:is()is never the function itself. It is the most specific item in the list. A list of only type and class selectors is safe. - Test with a competing rule. If you are unsure which specificity a selector has, write a rule you expect to override it and look in the browser's developer tools to see which one applies.
Trade-offs to keep in mind
:where() makes overriding easy, and that cuts both ways. A zero-specificity rule can be overridden by accident, for example by an unrelated element selector that you did not mean to affect it. If a default must be hard to override, :where() is the wrong tool.
:is() keeps your rules strong, but a high-specificity argument spreads. The more rules in a project that contain IDs inside :is(), the more often you will need !important or longer selectors to win against them.
Summary of the decision
- Same matching, different weight:
:is()counts its most specific argument,:where()counts nothing. - Choose
:where()for defaults and overridable styles, and:is()for compacting rules without changing their weight. - Watch for IDs inside
:is()lists, and remember that zero specificity still ties with*and still beats the browser's own styles.
Exercise
A stylesheet has :is(#main, .note) p { color: blue; } followed later by .note p { color: green; }. A paragraph is inside an element with class note and no main ID anywhere. What color is it, and what is the smallest change that makes it green without touching the second rule?
Show answer
The paragraph is blue. The first rule's specificity is (1,0,1) because #main is the most specific argument, and that applies even when the match comes through .note. The second rule is (0,1,1), so it loses despite coming later. Changing :is() to :where() in the first rule lowers it to (0,0,1), so the later .note p rule wins and the paragraph is green.
Exercise
You are writing base styles for a component library, and you want :where(.btn, .link-button) to carry cursor: pointer. A consumer writes button { cursor: default; } in their own stylesheet, loaded before yours. Which rule wins, and why?
Show answer
The consumer's button rule wins. Your rule has specificity (0,0,0) because :where() contributes nothing, and the consumer's rule has (0,0,1), so specificity decides before source order is consulted. If you had used :is(), the class inside would have given your rule (0,1,0) and it would have won.