Sub topic 2
The & nesting selector
This chapter assumes you know class, type, and pseudo-class selectors, the descendant and child combinators, and basic specificity. It explains what & means inside a nested rule and how the browser uses it to build the final selector.
What & stands for
Inside a style rule, & is the nesting selector. It stands for the elements matched by the parent rule's selector. The browser does not paste the parent's text into the nested selector. It treats & like :is() wrapped around the parent selector.
.card {
padding: 1rem;
border: 1px solid #ccc;
& .title {
font-weight: 600;
}
&:hover {
border-color: #666;
}
}The two nested rules resolve like this:
| Nested selector | Behaves like |
|---|---|
| `& .title` | `.card .title` |
| `&:hover` | `.card:hover` |
The space matters, as it does in any selector. & .title selects a descendant of .card. &:hover has no space, so it selects the .card element itself while it is hovered.
Why & is a selector and not text substitution
Because & behaves like :is(parent), it always matches as a single unit. This is most visible when the parent is complex. Take this rule:
.sidebar .menu {
& a {
color: navy;
}
}The nested rule behaves like :is(.sidebar .menu) a. It does not become .sidebar .menu a by string joining, though here the results match. The difference shows up with selector lists and specificity, which later chapters cover.
Two common errors
The first error is a stray space. Compare these rules:
.button {
&.primary {
background: royalblue;
}
& .primary {
background: tomato;
}
}&.primary matches an element that is both .button and .primary. & .primary matches a .primary element inside a .button. If a style never applies, check the space first.
The second error is expecting string concatenation. In Sass you can write &-title to build .card-title. Native CSS cannot do this, because & is a selector and not a piece of text.
.card {
/* Invalid in native CSS. This does not produce .card-title. */
&-title {
font-weight: 600;
}
}The browser drops this rule. Write .card-title as its own rule, or nest a full selector such as & .card-title.
Using & more than once
You can place & several times in one selector. Each occurrence stands for the same parent match.
.item {
& + & {
margin-top: 0.5rem;
}
}This behaves like .item + .item. It adds space only between consecutive items, so the first item has no extra top margin.
When & is required and when it is optional
The browser has to know how a nested selector relates to its parent. A nested selector starting with & states the relationship explicitly. A nested selector without &, such as .title or a, is treated as if it began with & (a descendant of the parent). Current browsers accept this shorter form, and the next chapter covers it in detail.
For now, remember this rule: write & when you want to attach to the parent itself (&:hover, &.active), and be deliberate about the space when you want a descendant (& .title).
Summary of the model
&stands for the elements matched by the parent rule.- It works like
:is(), so the parent is treated as one unit. - Whitespace after
&changes the meaning, from "the same element" to "a descendant". - It can appear more than once in a selector.
- It cannot be used for string concatenation.
Exercise
Given the rule below, what plain selector does &.open resolve to, and what does & .open resolve to? Which one matches an element that has both the classes menu and open?
.menu {
&.open { display: block; }
& .open { display: grid; }
}
Show answer
&.open resolves to .menu.open, and & .open resolves to .menu .open. The first one matches an element that has both classes menu and open. The second matches an .open element nested inside a .menu, not the .menu element itself.