Sub topic 8
Selector lists and nesting
This chapter assumes you know how selector lists work in plain CSS (h1, h2 { }), what & means in a nested rule, and the basics of specificity.
Lists inside a nested rule
A selector list is a comma-separated group of selectors that share one block of declarations. You can use a list in a nested rule exactly as you would at the top level. Each item in the list is resolved against the parent separately.
.card {
padding: 1rem;
h2, h3 {
margin: 0;
}
}This is equivalent to:
.card h2,
.card h3 {
margin: 0;
}You can mix forms in one list. Each item may start with &, with a combinator, or with a plain selector.
.menu {
& a, > li, &.open {
color: navy;
}
}The three items mean .menu a, .menu > li, and .menu.open.
Lists in the parent rule
The parent can be a list too. Here & has to stand for several selectors at once. The browser treats it like :is() wrapped around the whole parent list.
.alert, .notice {
padding: 1rem;
& p {
margin: 0;
}
}The nested rule behaves like :is(.alert, .notice) p. It matches a p inside either .alert or .notice, and it does not need two copies of the rule.
When both the parent and the nested rule are lists, you get every combination:
.alert, .notice {
& .title, & .body {
font-size: 1rem;
}
}This is equivalent to :is(.alert, .notice) .title, :is(.alert, .notice) .body.
The specificity consequence
Because & acts like :is(), a parent list contributes the specificity of its most specific item, not the item that actually matched.
#main, .note {
& p {
color: red;
}
}This rule has specificity (1,0,1), because #main is the most specific item in the list. The rule keeps that specificity even when the p sits inside a .note element that has no ID. A later rule such as .note p { color: blue; } loses to it, which can surprise you. The next chapter covers specificity calculations in full. For now, remember that mixing a high-specificity selector and a low-specificity one in a parent list raises the specificity of both.
If the two selectors need different specificity, write them as two separate rules.
Common errors
- One bad selector breaks the rule. A nested selector list is not forgiving. If one item is invalid, the browser drops the whole rule, as it does for a top-level list. Check each item when a nested rule seems to do nothing.
- Expecting suffix joining. Writing
&-titleinside.carddoes not produce.card-title. A list in the parent makes that even less meaningful, so write the full class name. - Surprising specificity from a mixed parent list. See the
#main, .noteexample above. Split the list if the specificity difference matters. - Forgetting the list expands. A parent list of three items and a nested list of three items give six combinations. Large lists make rules hard to debug in dev tools, so keep them short.
Exercise
What does the nested rule in this CSS match, and what is its specificity? .a, #b { & span { color: green; } }
Show answer
It matches any span inside an element with class a or ID b, as if written :is(.a, #b) span. Its specificity is (1,0,1), because :is() takes the most specific item in its list (#b), so even a span inside .a gets (1,0,1).
Exercise
Rewrite this without nesting: .box { h2, p { margin: 0; } }
Show answer
.box h2, .box p { margin: 0; }