Sub topic 11
How CSS nesting relates to :is()
This chapter assumes you know :is() and :where(), how specificity is calculated, and the basics of selector lists. It also assumes you have seen CSS nesting, where a rule is written inside another rule.
Nesting uses :is() semantics
When you nest a rule, the browser combines the parent selector and the nested selector into one complex selector. If the parent is a single selector, this is easy to picture. If the parent is a selector list, the browser does not expand it into separate copies. It treats the whole list as if it were wrapped in :is().
.card,
#hero {
.title {
color: crimson;
}
}
/* Behaves like :is(.card, #hero) .title, with specificity (1,1,0) */The nested .title rule matches a .title inside .card or inside #hero. The nested rule's .title has no &, so the parent is added as a descendant, and the rule behaves like :is(.card, #hero) .title.
The specificity consequence
Because the parent list behaves like :is(), it takes the specificity of its most specific item. Here that item is #hero, so the nested rule has specificity (1,1,0), written as (IDs, classes, types). This applies even to elements that match only .card.
That surprises many people. You might expect .card .title to have specificity (0,2,0). Inside this list, it counts as (1,1,0). A later rule such as .card .title { color: navy; } would lose to the nested rule even though it comes later.
.card,
#hero {
.title {
color: crimson;
}
}
.card .title {
color: navy; /* loses: (0,2,0) is lower than (1,1,0) */
}Using :where() to lower the specificity
You can control this by choosing the parent selector yourself. If the parent is wrapped in :where(), the parent part contributes nothing.
:where(.card, #hero) {
.title {
color: crimson;
}
}The parent contributes (0,0,0), so the nested rule has specificity (0,1,0), which comes only from .title. Use this when the nested styles are defaults that other rules should be able to override.
Where nesting differs from :is()
The & nesting selector refers to the parent in the same way as :is(), so these two rules are equivalent.
.card,
#hero {
& .title {
color: crimson;
}
}Two differences matter in practice:
- Nesting is not a full replacement for
:is(). You can only nest inside a rule that exists. You still need:is()to put a list in the middle of a selector, as inarticle :is(h1, h2, h3) a. &cannot represent a pseudo-element. If the parent selector ends in::before, a nested rule that uses&will not match anything.
Common mistakes
- Adding an ID to the parent list for one case and forgetting that it raises the specificity of every nested rule under that list.
- Assuming
.card .titleand the nested form have the same specificity when the parent is a list. - Putting a pseudo-element in the parent and expecting nested rules to style it.
Exercise
A stylesheet has .note, #alert { p { margin: 0; } }. Later, .note p { margin: 1rem; } appears. A paragraph inside a .note element (not #alert) is styled. Which margin applies, and how can you change the first rule so the later rule wins?
Show answer
The margin is 0. The nested rule behaves like :is(.note, #alert) p, which has specificity (1,0,1) because of #alert, and that beats .note p at (0,1,1). To let the later rule win, write the parent as :where(.note, #alert), which makes the nested rule (0,0,1). The later rule then wins by specificity, and source order is no longer needed.