Sub topic 8
Selector lists and nesting
This chapter covers what happens when a selector list, such as .a, .b, appears in a parent rule or a nested rule. It assumes you can read class, type, and pseudo-class selectors, know the descendant, child, and sibling combinators, and understand how specificity decides which declaration wins. It also assumes you have met the & nesting selector in earlier chapters.
A list is not expanded, it is wrapped
A selector list is a set of comma-separated selectors that share one block of declarations. In flat CSS, h1, h2 { margin: 0 } is shorthand for two separate rules.
Native nesting does not expand a parent list into separate copies of the nested rule. The & in a nested selector stands for the whole parent list, and it behaves as if the list were wrapped in :is().
.card, .panel {
& .title {
font-weight: 600;
}
}
/* Behaves like:
:is(.card, .panel) .title { font-weight: 600; }
*/For this example the result matches what you would get from writing .card .title, .panel .title. The difference appears in two places: specificity and selectors that use & more than once. Both are covered below.
Lists in the nested rule
A nested rule can also have its own list. Each comma-separated item is resolved against the parent separately, and every item is treated as a nested selector. You can mix items that start with &, items that start with a combinator, and items that start with neither.
.nav {
display: flex;
a, button {
padding: 0.5rem;
}
> li, > .item {
list-style: none;
}
&.is-open, &:focus-within {
background: #eee;
}
}These nested rules resolve like this:
| Nested selector | Resolved selector |
|---|---|
| `a, button` | `.nav a, .nav button` |
| `> li, > .item` | `.nav > li, .nav > .item` |
| `&.is-open, &:focus-within` | `.nav.is-open, .nav:focus-within` |
The first rule, a, button, depends on the relaxed syntax. Current browsers accept a nested selector that starts with a type selector. Older nesting implementations required & a, & button here. If you must support those older browsers, write the & explicitly. The chapter on browser support covers this.
A list is all or nothing
An invalid item inside a list affects the whole rule, not just that item. The :is() function is "forgiving". It ignores selectors it cannot parse and keeps the rest. A style rule is not forgiving. If one comma-separated item of the rule's own selector list is invalid, the browser drops the whole rule.
.card {
/* The second item is not a valid selector, so the whole rule is dropped,
including the valid "& .title" item. */
& .title, & ::not-a-real-pseudo {
color: rebeccapurple;
}
}If you need to tolerate selectors that only some browsers understand, split them into separate rules. Do not list them together.
.card {
& .title {
color: rebeccapurple;
}
& ::not-a-real-pseudo {
color: rebeccapurple;
}
}Now the first rule still applies even when the second one is dropped.
Specificity with a parent list
Specificity is a three-part value, written (ids, classes, types), that decides which rule wins when several match the same element. For a nested selector, & contributes the specificity of the most specific selector in the parent list. This is the rule for :is().
The browser makes this choice from the list as written, not from the item that matched the element. So the specificity is the same whichever item matched.
#main, .note {
& p {
color: red;
}
}
/* Behaves like: :is(#main, .note) p
Specificity is (1,0,1), because :is() takes the most specific item in its list. */Consider this markup:
<div class="note">
<p>Which colour am I?</p>
</div>#main, .note {
& p {
color: red;
}
}
.note p {
color: blue;
}The paragraph is red. It is inside .note, not #main, but the nested rule still has specificity (1,0,1). The flat rule .note p is only (0,1,1), so it loses even though it comes later in the stylesheet.
Sass would give the opposite result. It expands #main p, .note p and treats the two items separately, so the .note p item would have (0,1,1) and lose to a later equal-specificity rule. This is the most common surprise when people move from Sass to native nesting. The Sass chapter returns to this point.
A nested list does not get the "max" treatment
The max rule only applies to what & stands for. A nested selector list is an ordinary selector list. Each item has its own specificity, and the browser uses the specificity of the item that actually matched.
.card {
& .title, & #hero {
margin: 0;
}
}An element matching .card .title has specificity (0,2,0). An element matching .card #hero has (1,1,0). Neither is raised to match the other.
You can combine the two cases. For .a, #b { & .c, & .d { } }, the parent list gives (1,0,0) wherever & appears, and then the nested items add their own classes. Both items resolve to (1,1,0).
| Rule | Resolved as | Specificity |
|---|---|---|
| `.a, .b { & p {} }` | `:is(.a, .b) p` | (0,1,1) |
| `.a, #b { & p {} }` | `:is(.a, #b) p` | (1,0,1) |
| `.a, #b { & .c, & .d {} }` | `:is(.a, #b) .c, :is(.a, #b) .d` | (1,1,0) for each item |
| `.a { & p, & #x {} }` | `.a p, .a #x` | (0,1,1) and (1,1,0) |
Repeating `&` in one selector
When a parent list is wrapped in :is(), every & in a nested selector stands for the whole list. It does not stand for the same item twice. This matters for selectors such as "a sibling of the same kind".
.btn, .link {
& + & {
margin-left: 1rem;
}
}
/* Behaves like:
:is(.btn, .link) + :is(.btn, .link) { margin-left: 1rem; }
*/This matches .btn followed by .btn, and also .btn followed by .link, .link followed by .btn, and .link followed by .link. In Sass, & + & on the same list expands to pairs of the same item only, plus mixed pairs in some configurations, so check the output if you rely on it. With native nesting the behavior is simple: the two sides are independent.
If you need "same item twice" semantics, write separate rules:
.btn {
& + & {
margin-left: 1rem;
}
}
.link {
& + & {
margin-left: 1rem;
}
}Pseudo-elements in a parent list
:is() cannot match pseudo-elements, and so neither can &. If a parent list contains a pseudo-element, the nested selector does not match through that item.
p, p::first-line {
& span {
color: green;
}
}Here the p span part works. The p::first-line span part never matches anything. This is not an error. The rule is valid, and one list item is simply unreachable.
Put pseudo-elements in the nested selector instead, after &:
.note {
&::before {
content: "Note: ";
}
&::first-line {
font-weight: 600;
}
}Choosing between a list in the parent and separate rules
Both forms are valid. Pick by what you want the specificity to be.
- Use a parent list when the items really are interchangeable. Examples are
h1, h2, h3sharing heading styles, orbutton, [role="button"]sharing button styles. - Use separate rules when the items have very different specificity, for example an id and a class. A list holding
#mainmakes every nested rule under it carry the id's weight, which makes those rules hard to override. - Use a nested list when several descendants share declarations, as in
a, buttonunder.nav.
/* Risky: every nested rule inherits (1,0,0) from #main */
#main, .note {
& p { margin: 0; }
& a { color: inherit; }
}
/* Safer: each parent keeps its own weight */
#main {
& p { margin: 0; }
}
.note {
& p { margin: 0; }
}The second form repeats a declaration, but you can see and control the specificity of each rule.
Summary of rules
&stands for the whole parent list, as if wrapped in:is().- Each
&in a nested selector is independent of the others. - A parent list gives
&the specificity of its most specific item. - A nested list keeps per-item specificity.
- One invalid item in a rule's own list drops the whole rule.
- Pseudo-elements in a parent list never match through
&.
Exercise
Given the CSS below and the markup <section class="card"><p>Text</p></section>, what colour is the paragraph, and why?
#page, .card {
& p { color: red; }
}
.card p { color: blue; }
Show answer
The paragraph is red. The nested rule resolves to :is(#page, .card) p, and :is() takes the specificity of its most specific item. That item is #page, so the rule has specificity (1,0,1), even though the element matched through .card. The flat rule .card p is (0,1,1), so it loses despite coming later.
Exercise
Rewrite this rule so that .btn + .btn and .link + .link get the margin, but .btn + .link does not.
.btn, .link {
& + & { margin-left: 1rem; }
}
Show answer
Split the list into two rules. For example: .btn { & + & { margin-left: 1rem; } } and .link { & + & { margin-left: 1rem; } }. In the original, both & values stand for :is(.btn, .link), so mixed pairs such as .btn + .link also match.
Exercise
A nested rule reads & .title, & ::not-a-real-pseudo { color: teal; }. A browser does not recognise the second selector. What happens to the & .title item, and how do you avoid the problem?
Show answer
The browser drops the whole rule, so & .title is not styled either. A style rule's own selector list is not forgiving the way :is() is. To avoid it, put each selector in its own nested rule so the valid one survives.