Sub topic 11
How CSS nesting relates to :is()
This chapter assumes you can read CSS selectors and combinators, know how a selector list with commas works, and can compute specificity as an (ID, class, type) triple. It also assumes you have met :is() and :where() in the earlier chapters.
Native CSS nesting lets you write a rule inside another rule. It looks like Sass or Less, but it works differently. The browser does not paste selector text together. It treats the parent selector as one unit, and that unit behaves like :is(). Once you see this, the specificity and matching surprises in nested CSS are easy to predict.
The nesting selector `&`
The nesting selector & stands for "the elements matched by the parent rule". Here is a small nested stylesheet:
.card {
padding: 1rem;
& .title {
font-size: 1.25rem;
}
&:hover {
background: #f4f4f4;
}
.dark & {
background: #222;
}
}This matches the same elements as the following flat CSS:
.card {
padding: 1rem;
}
.card .title {
font-size: 1.25rem;
}
.card:hover {
background: #f4f4f4;
}
.dark .card {
background: #222;
}With a single-class parent the two forms look identical, so you cannot yet see the difference from Sass. The difference appears when the parent is a selector list.
A selector list in the parent behaves like :is()
Sass expands a nested rule into one flat rule per parent selector. Native CSS does not. It wraps the whole parent list in :is(). Take this rule:
.card,
#hero {
.title {
color: crimson;
}
}The browser reads the nested rule as if you had written this:
:is(.card, #hero) .title {
color: crimson;
}The :is() function takes the specificity of its most specific argument, and that is #hero. The nested rule therefore has specificity (1,1,0). This applies even to a .title inside a .card that has no ID anywhere. In Sass, the same source compiles to .card .title, #hero .title. The first selector has specificity (0,2,0), and only the second has an ID.
Here is how the two models compare for the same input:
| Model | Equivalent for `.card, #hero { .title {} }` | Specificity for `.card .title` |
|---|---|---|
| Sass-style expansion | `.card .title, #hero .title` | (0,2,0) |
| Native nesting | `:is(.card, #hero) .title` | (1,1,0) |
The same thing happens inside the nested rule's own selector list. If you write .card { h2, h3 { } }, each item is an implied & h2 or & h3. These are separate complex selectors, so each keeps its own specificity.
Controlling the effect with :where()
Because nesting resolves to :is(), you can switch to the zero-specificity behavior by wrapping the parent selector in :where():
:where(.card, #hero) {
.title {
color: crimson;
}
}This nested rule is equivalent to :where(.card, #hero) .title. The :where() part contributes nothing, so the specificity is (0,1,0), which is just the .title class. A single .title rule elsewhere in the stylesheet can now override it.
This is useful for library or base styles that sit inside a shared parent. Writing :where(.card, #hero) { ... } gives you the grouping and the compact nesting syntax without the ID's weight.
You can also use :where() at the nested level:
.card {
:where(h2, h3) {
margin-block: 0;
}
}The expanded form is .card :where(h2, h3). Its specificity is (0,1,0), because only .card counts. The nested rule is easy to override with something like .card h2.compact.
Where `&` appears and what it does
A nested selector can use & at the start, in the middle, or at the end of a compound. In each case the browser substitutes :is(parent) at that spot.
.btn {
&.primary {
background: royalblue;
}
.toolbar & {
margin-inline: 0.5rem;
}
& + & {
margin-inline-start: 0.5rem;
}
}These expand to .btn.primary, .toolbar .btn and .btn + .btn. You can use & more than once in a selector, and each occurrence refers to the same parent match.
When the parent has a single simple selector, wrapping it in :is() does not change the specificity. When the parent is a list, as in the earlier example, the wrap is what raises it.
A few details about the syntax follow from this model:
- A nested selector that starts with a type selector, such as
h2, is allowed. It is treated as& h2. Older browser versions required&or a symbol first, so check your support targets. - A relative selector can start with a combinator, as in
> li. That is treated as& > li. &can never be glued to a string to form a new name.&-titledoes not make.card-title, and the Sass idiom does not work in CSS. The parent is a selector, not text.
Limits that carry over from :is()
Since nesting uses the same matching model, the limits of :is() apply to it. Two of them matter most.
Pseudo-elements in the parent do not carry down. :is() cannot match a pseudo-element, so & cannot represent one. A nested rule under a pseudo-element parent never matches anything:
/* This nested rule matches nothing */
.note::before {
& strong {
color: red;
}
}To style something related to a pseudo-element, put the pseudo-element at the end of the nested selector instead:
.note {
& strong::before {
content: "* ";
}
}Invalid selectors are not forgiven in the same way. An :is() argument list is forgiving, so an unknown selector in it is ignored and the rest still match. The selector of a nested style rule is parsed like a normal selector list. One invalid item, such as an unsupported pseudo-class, can invalidate the whole nested rule. When you need a progressive-enhancement fallback, write it with an explicit :is() around the risky item:
.card {
:is(.title, :unsupported-pseudo) {
font-weight: 700;
}
}Here .card :is(.title, :unsupported-pseudo) keeps working for .title even in a browser that does not know the pseudo-class.
Choosing between a grouped parent and an explicit :is()
Both patterns can express the same thing, so choose by readability and by which specificity you want.
- Use a nested rule under a single parent for the ordinary case. The
:is()wrap changes nothing, and the code reads clearly. - Use a grouped parent such as
.card, .panel { ... }when the shared children genuinely belong to both parents and you accept the heaviest-argument specificity. - Wrap the parent in
:where()when the nested block provides defaults that other rules should override. - Write
:is()explicitly inside the nested selector when you need forgiving parsing for part of the selector.
Summary of the model
&stands for the parent selector, wrapped as:is(...).- The specificity of a nested rule is the specificity of the wrapped parent (the most specific item in a list) plus the rest of the nested selector.
- Wrapping the parent in
:where()removes its contribution. - Pseudo-elements cannot be matched through
&. - Forgiving parsing belongs to the
:is()argument list. It does not extend to the nested rule's selector as a whole.
Exercise
A stylesheet has the rule .card, #hero { .title { color: crimson; } } and another rule .page .card .title { color: navy; } that appears later. An element <h2 class="title"> sits inside <section class="page"><div class="card">. Which color does the heading get, and what one change to the first rule lets the later rule win?
Show answer
The heading is crimson. The nested rule resolves to :is(.card, #hero) .title, and the #hero argument gives it specificity (1,1,0), even though this element is not inside #hero. The later rule has specificity (0,3,0), which is lower, so source order does not matter. To let the later rule win, wrap the parent in :where(): :where(.card, #hero) { .title { color: crimson; } }. The nested rule is then (0,1,0), lower than (0,3,0), so navy applies.