Sub topic 8
Choosing between :is() and :where()
This chapter assumes you know selector lists, combinators, and how specificity decides which rule wins. Earlier chapters showed that :is() and :where() match the same elements. They differ only in the specificity they contribute. Here you will turn that difference into a decision rule.
The one question to ask
Before you pick one, ask: should other developers, or my own later rules, be able to override this easily?
- If the rule is a default, such as a reset, a base style, or a library style, the answer is yes. Use
:where(). - If the rule is a real styling decision that should win against ordinary rules, the answer is no. Use
:is().
Side-by-side comparison
| Property | `:is()` | `:where()` |
|---|---|---|
| Matches | Any argument in the list | Any argument in the list |
| Specificity | Most specific argument | Always (0,0,0) |
| Forgiving parsing | Yes | Yes |
| Typical use | Shortening repeated selectors | Overridable defaults |
Use :is() to shorten selectors
When you want the rule to carry its normal weight, :is() removes repetition without changing how the rule competes. The specificity stays predictable, because it equals what the longest written-out version would have had.
article :is(h1, h2, h3) a {
text-decoration: underline;
}This has specificity (0,0,3): two type selectors from article and a, plus one from the most specific argument (h1, h2, or h3, all type selectors). It competes just like the three-rule version.
Use :where() for defaults
A default style should lose to almost anything. With :where(), a single class selector is enough to override it, whatever the source order.
:where(ul, ol) {
margin: 0;
padding-left: 1.5rem;
}
.menu {
padding-left: 0;
}The first rule has specificity (0,0,0), so .menu wins even if it appears earlier in the file. A stylesheet you ship to others benefits most from this, because users do not have to write selectors that fight yours.
A mixed approach
You can combine both in one selector. Put :where() around the parts that should not add weight, and leave the parts that should count outside it.
.card :where(h2, h3) {
margin-block: 0 0.5rem;
}Here .card gives (0,1,0), and the headings add nothing. Another stylesheet can override the margin with .card .title, which has the same specificity of (0,2,0) only if it is later. A rule like .card h2.title would win outright.
Common mistakes
- Using
:is()for defaults. An ID inside the list, such as:is(#main, .note), makes the whole rule (1,0,0) heavy. Later class-based rules then fail to override it, and you may end up reaching for!important. - Using
:where()for rules that must win. A zero-specificity rule loses to nearly any other rule that targets the same property, including a browser-independent reset you forgot about. - Mixing them up while debugging. If an override "does nothing", check whether the losing rule contains
:is()with an ID or class in its list.
Exercises
Exercise
You are writing a base stylesheet for a shared component library. It sets margin: 0 on h1, h2, and h3 inside .prose. Which pseudo-class fits best, and what is the rule's specificity?
Show answer
Use :where(), as in .prose :where(h1, h2, h3) { margin: 0; }. The specificity is (0,1,0), from .prose alone, so any single-class rule can override it.
Exercise
A rule :is(#main, .note) p { color: blue; } cannot be overridden by .note p { color: red; }. Why, and how do you fix it?
Show answer
:is() takes the specificity of its most specific argument, so the rule is (1,0,1) while .note p is (0,1,1). To fix it, change :is() to :where(), which makes the rule (0,0,1), so .note p wins.