Sub topic 9
How specificity is calculated for nested rules
This chapter assumes you can read class, type, attribute, and pseudo-class selectors. It also assumes you know how specificity is written as three numbers, (IDs, classes, types), and that you know how the cascade breaks ties. Earlier chapters covered the & nesting selector, compound selectors, and parent-context selectors. This chapter uses them to answer one question: when a nested rule competes with other rules, which one wins?
The working model
A browser does not keep nested rules as a tree when it computes specificity. It resolves each nested selector into an ordinary selector in which & stands for the parent selector. That resolved selector then goes through the normal specificity calculation.
The key rule is that & behaves like :is() wrapped around the parent selector. Specificity of :is() equals the specificity of its most specific argument. So:
- Take the parent rule's selector (or selector list).
- Treat
&as:is(parent). - Substitute it into the nested selector.
- Add up IDs, classes, and types in the result, as usual.
Nesting adds no weight of its own. Neither the curly braces nor the leading & count. Only the selectors that result from resolution count.
Simple cases
For a parent with a single selector, :is() changes nothing. The result is the same as writing the full selector by hand.
.card {
& .title {
font-weight: 600;
}
&:hover {
border-color: #666;
}
}The first nested rule resolves to .card .title, which has specificity (0,2,0). The second resolves to .card:hover, also (0,2,0).
The table below shows more forms. The "relaxed" forms, where you leave out the leading &, resolve the same way as their explicit versions.
| Written inside the parent | Resolves to | Specificity |
|---|---|---|
| `.card { & .title }` | `.card .title` | (0,2,0) |
| `.card { .title }` | `.card .title` | (0,2,0) |
| `.nav { a }` | `.nav a` | (0,1,1) |
| `.nav { > li }` | `.nav > li` | (0,1,1) |
| `.nav { &.is-open }` | `.nav.is-open` | (0,2,0) |
| `.nav { .dark-theme & }` | `.dark-theme .nav` | (0,2,0) |
| `.card { &:hover }` | `.card:hover` | (0,2,0) |
| `.card { & + & }` | `.card + .card` | (0,2,0) |
One practical consequence is that a nested rule is usually heavier than a flat rule for the same element. A flat .title { color: black; } has specificity (0,1,0). The nested .card { .title { color: green; } } has (0,2,0), so it wins no matter which comes first in the stylesheet. This is the most common source of "why won't my override work" questions after teams adopt nesting.
Selector lists as parents
When the parent is a selector list, & takes the specificity of the most specific selector in the list. This applies even when the element matches only a less specific item.
#main, .note {
& p {
color: red;
}
}This behaves like :is(#main, .note) p. The specificity is (1,0,1): the ID from #main plus the type selector p. A paragraph inside .note is matched by the same rule, and it still carries the ID-level weight.
You can see the effect in a complete page:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Nested specificity</title>
<style>
#main, .note {
& p {
color: red;
}
}
.note p {
color: blue;
}
</style>
</head>
<body>
<div class="note">
<p>This text is red.</p>
</div>
</body>
</html>The paragraph has no #main ancestor, yet it renders red. The nested rule counts as (1,0,1) and .note p counts as (0,1,1). The later .note p rule loses on specificity, not on order.
Compare this with a hand-written list:
#main p,
.note p {
color: red;
}Here each selector in the list keeps its own specificity. A paragraph matched through .note p is weighted (0,1,1). The nested version is therefore heavier than the flat version for .note elements. Sass, which expands nesting into #main p, .note p, behaves like the flat version. Native nesting does not.
Avoiding the inflated weight
If you do not want the ID's weight to leak into .note, you have three options:
- Write the two parents as separate rules, each with its own nested block.
- Keep IDs out of selector lists that serve as nesting parents.
- Wrap the heavy item in
:where(), which always contributes zero specificity.
.note {
& p {
color: red;
}
}
#main {
& p {
color: red;
}
}Now .note p is (0,1,1) and #main p is (1,0,1), as expected.
Selector lists in the nested rule
The :is() behavior applies only to the & that stands for the parent. A selector list written in the nested rule is an ordinary list, and each item is counted on its own.
.card {
& a,
.dark-theme & {
color: navy;
}
}This is the same as .card a, .dark-theme .card. The first selector has specificity (0,1,1) and the second has (0,2,0). For a given element, the browser uses the specificity of the selector that actually matched it.
A parent that is a list and a nested rule that is also a list combine like this: the parent list is folded into :is(), and the nested list stays a list. For example, #a, .b { & i, & em {} } becomes :is(#a, .b) i, :is(#a, .b) em, and both items are (1,0,1).
Parents that are complex selectors
The parent does not have to be a single compound selector. If it contains descendant combinators, & stands for the whole thing and brings all its weight.
.page .sidebar {
& .link {
color: teal;
}
}This resolves to :is(.page .sidebar) .link, specificity (0,3,0). Each level of nested ancestry adds its weight. This is the main reason deep nesting produces rules that are hard to override.
Lowering specificity with :where()
Because & copies the specificity of its parent, you can reduce the weight of a whole block by making the parent weightless.
:where(.card) {
& .title {
font-weight: 600;
}
}This resolves to :is(:where(.card)) .title. The :where() part contributes (0,0,0), so the total is (0,1,0). A flat .title rule now ties with it, and source order decides. This pattern suits library or base styles that authors are expected to override.
Other things that do not change specificity
- Nested at-rules. A
@media,@supports,@container, or@layerblock nested in a rule does not alter the selector's specificity. The declarations inside apply to the parent selector and keep its weight. A condition only decides whether the rule applies. (Cascade layers do change who wins, but through layer order, which is decided before specificity.) - Declaration order. Where a declaration sits relative to nested rules does not change selector specificity. It can change source order, which matters only when specificity is tied.
!important. It is compared before specificity, and nesting does not change that.- Attribute and pseudo-class selectors. Each counts as a class-level item (0,1,0), whether or not it is nested.
Common errors
- Assuming nested equals flat. A nested
.titleunder.cardis (0,2,0), not (0,1,0). Overrides written with a flat selector lose. - Counting only the matching item in a parent list. The most specific item in the list sets the weight for every match.
- Reasoning from Sass. Sass expands lists into separate selectors. Native nesting uses
:is(), so the results differ for lists that mix specificities. - Forgetting that depth accumulates. Three nested class selectors give (0,3,0) before you add anything else.
- Reaching for
!importantto fix an override. It is usually better to flatten the nesting or use:where()on the parent.
Summary of the procedure
- Replace
&with:is(parent). If the leading&was omitted, add the parent in front as a descendant. - For a list parent, count only the most specific item inside
:is(). - Count the rest of the selector normally. List items written in the nested rule are counted separately.
- Ignore at-rule wrappers and declaration position.
- Compare with competing rules. If specificity is equal, the later rule wins.
Exercise
Given the CSS below, what color is <p> in <section class="intro"><p>Hello</p></section>? Give the specificity of each rule.
#page, .intro {
& p { color: red; }
}
section.intro p { color: blue; }
Show answer
Red. The nested rule resolves to :is(#page, .intro) p, which takes the weight of #page and so has specificity (1,0,1). The flat rule section.intro p has specificity (0,2,2). The nested rule is more specific, so it wins even though it comes first and the paragraph has no #page ancestor.
Exercise
Rewrite the rule below so that .title has specificity (0,1,0) and can be overridden by a flat .title rule that comes later. What is the specificity of the original?
.card {
& .title { font-weight: 600; }
}
Show answer
The original resolves to .card .title, specificity (0,2,0). Wrapping the parent in :where() removes its weight: :where(.card) { & .title { font-weight: 600; } } resolves to :is(:where(.card)) .title, specificity (0,1,0). A later flat .title rule then ties and wins by source order.