Sub topic 3
When you can leave out the &
This chapter assumes you know CSS selectors, combinators, the basics of the cascade and specificity, and how to write a basic nested rule with &. You will learn when the & is optional, when it is required, and what the browser does with a nested selector that has no &.
The relaxed nesting syntax
The first version of CSS nesting required every nested selector to start with a symbol or &. A bare type selector such as a was invalid. Current browsers accept the relaxed syntax, which lets you write a nested selector without a leading &.
.nav {
display: flex;
a {
padding: 0.5rem;
}
}The nested a rule means the same as .nav a. The browser treats a nested selector with no & as if it began with & (the & followed by a descendant combinator). The two forms below are equivalent.
.nav {
a {
padding: 0.5rem;
}
& a {
padding: 0.5rem;
}
}Leading combinators
A nested selector can also start with a combinator. The & is again implied, and the combinator links the parent to the selector that follows.
.nav {
> li {
list-style: none;
}
+ .nav {
margin-top: 1rem;
}
}These rules mean .nav > li and .nav + .nav. A leading combinator never needs an explicit &.
When the & is required
Leaving out the & always produces a descendant relationship (or the relationship named by a leading combinator). You must write & when you want something else.
| You want | Write | Resolves to |
|---|---|---|
| A descendant element | `a { }` or `& a { }` | `.nav a` |
| A child element | `> li { }` | `.nav > li` |
| The parent with an extra class | `&.is-open { }` | `.nav.is-open` |
| The parent in a state | `&:hover { }` | `.nav:hover` |
| The parent inside an ancestor | `.dark-theme & { }` | `.dark-theme .nav` |
Consider the difference between these two rules.
.nav {
.is-open {
background: #eee;
}
&.is-open {
background: #eee;
}
}The first rule matches an element with the class is-open inside .nav. The second matches the .nav element itself when it also has the class is-open. Leaving out the & here is a common mistake, because it still produces valid CSS that silently matches different elements.
Why you might still write the &
The & is optional in many places, but it can still make a rule easier to read. Some teams write & a so every nested rule starts with the same mark, which makes the parent relationship visible when scanning a long block. Others leave it out for type selectors and use it only where it changes the meaning. Both produce the same result. Pick one convention and apply it consistently.
Check for browser support
The relaxed syntax arrived later than the original nesting feature. A browser that only supports the first version drops a rule such as a { } nested in .nav, because it does not recognize the selector as a valid nested one. If you must support such browsers, start nested type selectors with &, as in & a, which is valid in both versions.
Exercise
Inside .card { }, you want to style the .card element itself when it also has the class featured. A teammate writes .featured { border-color: gold; }. What does that rule match, and how do you fix it?
Show answer
It matches elements with the class featured inside .card, because a nested selector with no & is a descendant selector (.card .featured). To match the card itself, write &.featured { border-color: gold; }, which resolves to .card.featured.