Sub topic 6
Cutting repetition in real stylesheets
This chapter assumes you know type, class, ID and attribute selectors, the descendant and child combinators, comma-separated selector lists, and how specificity decides which rule wins. You should also know the basic syntax of :is() and :where() from the earlier chapters. Here you apply them to real stylesheets and learn when a refactor changes behavior without anyone noticing.
Where repetition comes from
Repetition in selectors is multiplicative. Suppose a stylesheet needs to style links inside three kinds of containers, in three heading levels, in two interaction states. Written out with commas, that is 3 × 3 × 2 = 18 selectors. Each added container or state multiplies the list again.
article h2 a:hover,
article h2 a:focus-visible,
article h3 a:hover,
article h3 a:focus-visible,
aside h2 a:hover,
aside h2 a:focus-visible,
aside h3 a:hover,
aside h3 a:focus-visible {
text-decoration: underline;
}Those eight selectors share a skeleton: <container> <heading> a<state>. Only the parts in angle brackets vary. :is() lets you write the skeleton once and put each varying part in a list at the position where it varies.
:is(article, aside) :is(h2, h3) a:is(:hover, :focus-visible) {
text-decoration: underline;
}The browser treats this as the same eight combinations. You can add section to the container list by editing one place instead of adding four selectors.
A workflow for refactoring
Do not start by wrapping everything in :is(). Work through the selectors in order.
- Line up the selectors and find the positions where they differ. Each differing position becomes one
:is()list. - Check that the selectors differ in only those positions. If two selectors differ in their combinators or their structure, grouping them forces awkward lists.
- Compare the specificity of each original selector. This step catches the most common mistake, covered in the next section.
- Replace the selectors and check the result in your browser's developer tools.
The specificity trap
The browser gives an :is() selector the specificity of its most specific argument. That is the specificity of the whole list, not of the argument that happens to match. Merging selectors that had different specificities therefore raises the specificity of the weaker ones.
Consider this stylesheet.
article h2 a,
#main h2 a {
color: navy;
}
.intro h2 a {
color: darkgreen;
}For a link in <article class="intro">, the first rule matches with article h2 a, which has specificity (0,0,3). The .intro h2 a rule has (0,1,2), so it wins and the link is dark green. This is the intended result.
Now merge the first rule.
:is(article, #main) h2 a {
color: navy;
}
.intro h2 a {
color: darkgreen;
}The :is() list contains #main, so the selector has specificity (1,0,2) for every element it matches, including the one inside article. The link now turns navy. Nothing in the markup changed, and the refactored rule looks equivalent.
Two safe responses exist. If the article case must stay weak, leave the two selectors as separate rules. If the ID's weight does not matter for the structural part, use :where() for the list and let the other parts of the selector carry the specificity.
:where(article, #main) h2 a {
color: navy;
}This selector has specificity (0,0,2), which is lower than the original (0,0,3). It loses to rules it used to beat, so you have changed behavior in the other direction. Whichever you choose, compute the numbers first.
Practical patterns
Heading groups
Heading levels are the classic case. Six near-identical selectors become one.
:is(h1, h2, h3, h4, h5, h6) {
line-height: 1.2;
text-wrap: balance;
}
:is(h1, h2, h3) + p {
margin-block-start: 0.5em;
}The first rule has specificity (0,0,1), the same as a single h1 selector. The second is (0,0,2).
Form controls and states
Controls and states multiply naturally. Group each dimension separately.
:is(input, select, textarea):is(:hover, :focus-visible) {
border-color: royalblue;
}
:is(input, select, textarea):is(:disabled, [aria-disabled="true"]) {
opacity: 0.6;
cursor: not-allowed;
}Both rules have specificity (0,1,1). The type contributes (0,0,1) and the most specific item in the state list contributes (0,1,0), whether that item is a pseudo-class or an attribute selector.
Repeating a context
When many rules share a long ancestor, group the ancestors and keep the rest plain.
:is(.card, .panel, .dialog) :is(h2, h3) a:is(:hover, :focus-visible) {
color: var(--link-hover);
}Its specificity is (0,2,2): (0,1,0) from the container list, (0,0,1) from the heading list, (0,0,1) from a, and (0,1,0) from the state list. Specificity adds up across the compound selectors in the usual way.
Before and after
| Pattern | Selectors before | Selectors after |
|---|---|---|
| Three headings inside a link context | 3 | 1 |
| Two containers × two headings × two states | 8 | 1 |
| Three containers × two headings × two states | 12 | 1 |
| Six headings, one rule | 6 | 1 |
The saving grows with each position you group, because the number of expanded selectors is the product of the list lengths.
When not to group
Fewer lines does not always mean clearer code. Leave selectors alone in these cases.
- Only one item varies and there are two options.
h1, h2is no harder to read than:is(h1, h2). - The selectors have different specificity and that difference matters. See the trap above.
- The grouped lists are so deeply nested that readers cannot tell what the expansion is. If you need to write out the expansion in a comment to understand it, split the rule.
- You need pseudo-elements. Place them after the
:is()group, not inside its list.:is(p, li)::first-lineworks.:is(p::first-line, li::first-line)matches nothing.
Keeping the stylesheet maintainable
Group by meaning, not by accident. A list such as :is(h1, h2, h3) documents that those headings are treated alike. A list like :is(.btn, .nav-item, footer) that only exists because the declarations happened to match is harder to understand later and harder to split when the designs diverge.
Prefer one position per :is(). A group that holds complete selectors with their own combinators, such as :is(article h2, aside .title), is legal and sometimes useful, but it hides the structure that grouping is meant to expose.
Finally, remember that grouping does not make the browser faster in any way you should plan around. The benefit is in editing: one list to extend, one place to review.
Exercise
Rewrite these rules as one rule using :is(), then state the specificity of the new selector. Is the result safe to merge if another rule, .note h3 a, is meant to override it?
nav ul li a:hover,
nav ul li a:focus-visible,
footer ul li a:hover,
footer ul li a:focus-visible { color: teal; }
Show answer
Use :is(nav, footer) ul li a:is(:hover, :focus-visible) { color: teal; }. The specificity is (0,1,4): the state list contributes (0,1,0), and nav or footer, ul, li and a contribute one type each. Every original selector also had (0,1,4), so merging does not change specificity. The rule .note h3 a has (0,1,2), which is lower than (0,1,4), so it does not override the merged rule. That was already true before the merge.
Exercise
A stylesheet has article h2 a, #main h2 a { color: navy; } and .intro h2 a { color: darkgreen; }. After merging the first rule into :is(article, #main) h2 a, links inside <article class="intro"> turn navy. Explain why, and give a fix that keeps the original behavior.
Show answer
The merged selector takes the specificity of its most specific argument, #main, so it becomes (1,0,2) for every match. That beats .intro h2 a at (0,1,2). The original article h2 a was only (0,0,3) and lost to .intro h2 a. To keep the original behavior, leave the two selectors as separate rules, or raise the specificity of the .intro rule deliberately.