Sub topic 1
The Repetition Problem with Selector Lists
CSS lets you attach one block of declarations to several selectors by separating them with commas. This works well for short lists. When the selectors share most of their text, the list gets long and hard to maintain. This chapter shows where that repetition comes from and previews the feature that removes it.
Prerequisites
This chapter assumes you know:
- Basic selectors: type, class, ID, and attribute.
- Combinators: descendant (space), child (
>), and sibling (+,~). - Selector lists written with commas.
- The basic idea of the cascade, including that specificity decides conflicts between rules.
What a selector list does
A selector list is shorthand for writing the same declaration block several times. Each selector in the list is evaluated on its own. An element matches the rule if at least one selector matches it.
h1,
h2,
h3 {
margin-block: 1rem;
}This is equivalent to three separate rules with the same declarations. Each selector also keeps its own specificity.
Where the repetition comes from
The list is short here because the selectors differ only in one word. Real selectors usually have a shared parent and a shared ending, and only the middle part varies:
article h1 a,
article h2 a,
article h3 a {
text-decoration: underline;
}Here article and a appear three times each. Now add a second variable part, such as interaction states:
header a:hover,
header a:focus,
nav a:hover,
nav a:focus,
main a:hover,
main a:focus {
color: darkred;
}Three containers and two states produce six selectors. The count is the product of the options. Adding a fourth container, such as footer, adds two more lines. Adding a third state, such as :active, adds four more.
Why this is a problem
- Editing is error-prone. Changing
atoa[href]means editing every line, and a missed line leaves the rule inconsistent. - Intent is hidden. A reader has to compare the lines to see which parts vary and which stay the same.
- Mistakes are easy to miss. A typo in one of the six lines silently stops that case from matching.
- A single invalid selector can break everything. In a normal selector list, one invalid selector causes the browser to drop the whole rule. Later chapters cover this.
A preview of the fix
The :is() pseudo-class takes a selector list as its argument and matches any element that matches one of the items. You write the shared parts once and put only the varying part inside the parentheses:
/* Before */
article h1 a,
article h2 a,
article h3 a {
text-decoration: underline;
}
/* After */
article :is(h1, h2, h3) a {
text-decoration: underline;
}Both rules match the same elements. The second states the intent directly: links inside headings inside an article.
A second pseudo-class, :where(), has the same syntax and matching behavior. It differs only in how it affects specificity. The next chapters cover that difference, which determines which one you should choose.
Exercise
Exercise
Rewrite the six-selector rule from this chapter (header, nav, and main, each with a:hover and a:focus) as one selector using :is() twice. Which parts of the original lines stay outside the parentheses?
Show answer
:is(header, nav, main) a:is(:hover, :focus) { color: darkred; }. Only the descendant combinator (the space) and the type selector a stay outside the parentheses. The three containers and the two states move into the two :is() lists.