Sub topic 9
How specificity is calculated for nested rules
This chapter assumes you can read a specificity score written as (ids, classes, types), and that you know how & and relaxed nesting work from the earlier chapters.
The working model
A nested selector has the same specificity as its fully written-out equivalent. The browser replaces & with :is() wrapped around the parent selector. It then scores the result by the usual rules.
.card {
& .title {
font-weight: 600;
}
}This behaves like :is(.card) .title. :is() contributes the specificity of its most specific argument, so here it adds one class. The total is (0,2,0).
Nothing is added for the nesting itself. Nesting only changes how you write the selector.
| Written inside `.card` | Equivalent | Specificity |
|---|---|---|
| `& .title` | `.card .title` | (0,2,0) |
| `.title` (relaxed) | `.card .title` | (0,2,0) |
| `p` (relaxed) | `.card p` | (0,1,1) |
| `&:hover` | `.card:hover` | (0,2,0) |
| `&.is-open` | `.card.is-open` | (0,2,0) |
| `.dark-theme &` | `.dark-theme .card` | (0,2,0) |
Because the leading & is optional, p and & p score the same. Leaving it out has no effect on specificity.
When the parent is a selector list
The :is() rule has a consequence when the parent is a list. :is() uses the highest specificity among its arguments. The nested selector therefore takes that score, even if the element matches through a weaker item.
#main, .note {
& p {
color: red;
}
}
.note p {
color: blue;
}Take <div class="note"><p>Text</p></div>. The first rule is :is(#main, .note) p, which scores (1,0,1) because of #main. The second rule scores (0,1,1). The paragraph is red even though the blue rule comes later and the element has no #main ancestor.
This is a common source of surprise. Two ways to avoid it:
- Write the weaker selector as its own rule instead of putting it in a list with an id.
- Prefer classes over ids in parent lists, so every item scores the same.
A list in the nested rule is different. Each item is scored separately, and only & is wrapped in :is().
.panel {
.label,
#title {
margin: 0;
}
}Here .label becomes .panel .label, which is (0,2,0). #title becomes .panel #title, which is (1,1,0). The id does not raise the score of the .label item.
Checking your work
You can test a score by pairing the nested rule with an equivalent flat rule and seeing which wins.
.card {
& .title {
color: green;
}
}
.card .title {
color: orange;
}Both rules score (0,2,0). The later rule wins, so the title is orange. Swap their order and it turns green. If the nested version lost to a flat rule that you expected to tie, check for a list parent that raised its score.
Common errors
- Assuming nesting adds weight. It does not.
.card { .title {} }is no stronger than.card .title {}. - Expecting a list to be scored per item. When
&stands for a parent list, it takes the highest score. - Using an id in a parent list, then trying to override the nested rule with a class selector. The class loses. Fix the parent list, or avoid raising specificity with
!important. - Forgetting that
&adds the parent's full score each time it appears. In.a { & + & {} }, the result is:is(.a) + :is(.a), which is (0,2,0).
Exercise
A stylesheet has #app, .shell { & a { color: red; } } followed by .shell a { color: blue; }. A link sits inside <div class="shell">, with no #app anywhere. What color is the link, and why?
Show answer
Red. The nested rule is :is(#app, .shell) a, which takes the score of #app and is (1,0,1). The blue rule scores (0,1,1). The higher specificity wins, even though the blue rule comes later and the element matched through .shell.