Sub topic 1
The repetition problem with selector lists
A comma-separated selector list lets one rule style many elements. It works well until the selectors share most of their text. Then you copy the same fragments again and again, and the stylesheet gets long, easy to break, and hard to read. This chapter describes that problem so the next chapters on :is() and :where() have something concrete to solve.
Prerequisites
This chapter assumes you already know:
- Basic selectors: type (
a), class (.card), ID (#main), and attribute ([href]). - Combinators: descendant (space), child (
>), and sibling (+,~). - How a comma-separated selector list works in a rule.
- That the cascade uses specificity and source order to resolve conflicts. You don't need to compute specificity by hand yet. A later chapter reviews it.
What a selector list does
A selector list is two or more selectors separated by commas. The browser treats each selector independently. An element matches the rule if at least one selector in the list matches it.
h1,
h2,
h3 {
margin-block: 1em 0.5em;
}This is equivalent to three separate rules that share one declaration block. A list saves you from repeating the declarations. It does not save you from repeating the selectors themselves, and that is where the trouble starts.
Where repetition appears
Repetition shows up when the alternatives you want to match sit in the middle of a longer selector. The comma can only separate whole selectors. So any shared prefix or suffix has to be written out for every alternative.
Suppose you want to underline links inside headings, but only inside article:
article h1 a,
article h2 a,
article h3 a {
text-decoration: underline;
}Each line repeats article and a. The only part that changes is the heading level. Only the middle of the selector varies, and the list syntax gives no way to say that.
Shared ancestor
When several elements live inside the same container, the container name repeats on every line:
.sidebar p,
.sidebar li,
.sidebar blockquote {
font-size: 0.9rem;
}Shared descendant or state
The same happens when the end of the selector is shared, for example a link state:
button:hover,
a:hover,
summary:hover {
cursor: pointer;
}Multiple axes at once
The real cost appears when there are two or more places that vary. The number of selectors you must write is the product of the choices at each place, not the sum.
nav a:hover,
nav a:focus,
aside a:hover,
aside a:focus,
footer a:hover,
footer a:focus {
color: darkred;
}This rule has three containers and two states, so it needs 3 × 2 = 6 selectors. Add a fourth container, header, and you need 8. Add a third state, :focus-visible, and you need 12. Each new alternative multiplies the length of the list.
| Containers | States | Selectors you must write |
|---|---|---|
| 2 | 2 | 4 |
| 3 | 2 | 6 |
| 3 | 3 | 9 |
| 4 | 3 | 12 |
| 5 | 4 | 20 |
Why this is a real problem
Repeated selector fragments cause practical trouble, not just extra typing.
- Edits must be made in many places. If
articleis renamed to.post, you change every line. Missing one leaves a rule that quietly stops matching. - Omissions are hard to see. In a list of 12 selectors, it is easy to leave out one combination. The page then behaves inconsistently, for example links in the footer get a hover color but no focus color.
- The intent is buried. A reader has to scan every line to confirm that only one part varies. The sentence "links in these three containers change color on hover or focus" turns into a block of text you must compare line by line.
- The list grows with every addition. Adding one more alternative can mean adding several lines. Developers often avoid that work and write a separate rule instead, which spreads one idea across the file.
- One mistake can break the whole rule. In a normal selector list, a single invalid selector invalidates the entire rule, and the browser drops all of it. A typo in one of twelve repeated lines silently disables the other eleven. A later chapter explains this behavior and how it differs inside
:is()and:where().
Common workarounds and their limits
Before :is() and :where() were available, developers used several approaches. Each has a cost.
- Add a shared class. You could put
class="link-hover"on every element and write.link-hover:hover. This works, but it moves styling decisions into the HTML. It is not practical when you don't control the markup, such as content from a CMS or Markdown. - Use a preprocessor. Tools like Sass can generate the expanded list from a compact source. The repetition is gone from your source, but the compiled CSS still contains every expanded selector, so file size and the debugging view are unchanged.
- Split into several rules. You can write one rule per container. This avoids a huge list but repeats the declarations, which brings back the original problem of keeping duplicates in sync.
- Use broader selectors. You could drop
nav,aside, andfooterand style everya:hover. This is shorter, but it changes what the rule matches, which is a different behavior and not a refactor.
None of these lets you write the shared parts once and vary only the part that differs, inside the selector itself. That is the gap that :is() fills.
A first look at the fix
The pseudo-class :is() takes a selector list as its argument and matches any element that matches at least one item in it. You can put it anywhere in a compound or complex selector, so the shared parts stay outside and only the varying part goes inside.
/* Before */
article h1 a,
article h2 a,
article h3 a {
text-decoration: underline;
}
/* After */
article :is(h1, h2, h3) a {
text-decoration: underline;
}The two rules match the same elements. The multi-axis example shrinks the same way:
:is(nav, aside, footer) a:is(:hover, :focus) {
color: darkred;
}This one rule replaces the six selectors above. Adding header or :focus-visible now means adding a word to a list. It does not mean writing a new line for every combination. The cost grows with the sum of the alternatives, not their product.
What to carry forward
- A comma-separated list repeats every shared fragment of the selector, because the comma only separates complete selectors.
- When more than one part of a selector varies, the list length is the product of the number of choices.
- Repetition causes missed edits, inconsistent rules, hard-to-read intent, and a larger chance that one invalid selector disables the whole rule.
- Classes, preprocessors, and splitting rules help in some cases, but none lets you vary one part of a selector in place.
:is()accepts a selector list as an argument, so shared parts are written once.:where()does the same but with a different effect on specificity. The next chapters build the working model for both.
Exercise
The selector :is(header, main, footer) :is(h2, h3) a is written with :is(). If you wrote the same rule as a plain comma-separated list with no :is(), how many selectors would you need, and why?
Show answer
You would need 3 × 2 = 6 selectors. The first :is() offers 3 choices (header, main, footer) and the second offers 2 (h2, h3). A plain list must spell out every combination, so the count is the product of the choices at each varying position. The trailing a is shared and does not add to the count.
Exercise
A stylesheet has .card h2, .card h3, .panel h2, .panel h3 { margin: 0; }. Which parts of the selectors are repeated, and how would you rewrite the rule using :is() so each alternative is written once?
Show answer
Both the container (.card and .panel) and the heading level (h2 and h3) vary, and every combination is written out, giving 2 × 2 = 4 selectors. The compact version is :is(.card, .panel) :is(h2, h3) { margin: 0; }.
Exercise
Why is a long repetitive selector list more risky than a short one when a single selector in it is invalid?
Show answer
In a plain comma-separated list, one invalid selector makes the browser drop the entire rule, so every other selector in the list stops working too. A longer list contains more selectors that could contain a typo or an unsupported feature, and the failure is silent.