Sub topic 6
Cutting repetition in real stylesheets
This chapter assumes you know type, class, ID and attribute selectors, the descendant, child and sibling combinators, comma-separated selector lists, and how specificity decides which rule wins. Here you apply :is() to the repetition that builds up in real stylesheets.
Where repetition comes from
A selector list repeats every shared part for each alternative. The repetition grows quickly when two or more positions in a selector vary, because a plain list needs one entry for each combination.
/* Before: 2 x 3 = 6 selectors */
main h1 a,
main h2 a,
main h3 a,
aside h1 a,
aside h2 a,
aside h3 a {
text-decoration: underline;
}Each new alternative multiplies the list. Adding footer as a third container would add three more lines. The rule is also easy to get wrong, because you have to keep every line in sync when you edit it.
Collapse one position
Wrap the alternatives for one position in :is(). The shared parts stay outside it.
article :is(h1, h2, h3) a {
text-decoration: underline;
}This matches the same elements as the three-line version: any a inside an h1, h2 or h3 that is inside an article.
Collapse several positions
You can use :is() more than once in the same selector. The six selectors above shrink to one.
:is(main, aside) :is(h1, h2, h3) a {
text-decoration: underline;
}Adding footer is now a one-word change.
Common patterns
The same technique works on states, child elements and form controls.
/* Hover and keyboard focus styled together */
:is(a, button):is(:hover, :focus-visible) {
outline: 2px solid currentColor;
}
/* Every text-like input inside a form */
form :is(input[type="text"], input[type="email"], textarea) {
border: 1px solid #888;
}
/* List items at any list depth */
:is(ul, ol) :is(ul, ol) li {
margin-block: 0.25rem;
}Notice that :is(a, button):is(:hover, :focus-visible) has no space between the two parts. Both conditions apply to the same element, which is a compound selector.
Check the specificity
Compacting a selector can change its specificity. The value of :is() comes from its most specific argument, so one ID in the list raises the whole rule.
/* Specificity (1,0,1), even for elements matched by .note */
:is(#main, .note) p {
color: blue;
}Before you merge a list, compare the specificity of each entry. If the entries differ, the merged rule behaves as if every alternative had the highest value. This can override rules that used to win. When you only want to reduce repetition and keep the specificity low, :where() is the alternative, and a later chapter covers it.
Keep a refactor honest
- Expand the
:is()selector by hand into a full list, and check that it matches your original. - Compare the specificity of the longest original selector with the new one.
- Look for rules elsewhere that depended on the old, lower specificity.
Exercise
Rewrite this rule with :is() so that it uses one selector. Then say what changes if #sidebar replaces aside.
nav a:hover,
nav a:focus-visible,
footer a:hover,
footer a:focus-visible {
color: teal;
}
Show answer
Use :is(nav, footer) a:is(:hover, :focus-visible) { color: teal; }. If #sidebar replaces aside in a list such as :is(#sidebar, footer), the specificity of the whole rule becomes (1,1,1), because :is() takes the value of its most specific argument. The rule then also wins against other rules when the element is in the footer.