Sub topic 10
Declarations After Nested Rules
This chapter covers what happens when a style rule has plain declarations such as color: red; after a nested rule. It assumes you know the & nesting selector, nested @media rules, and how the cascade orders rules by specificity and then by source order.
Declarations and nested rules can be mixed
A style rule's block can hold declarations and nested rules in any order. This is valid:
.box {
color: red;
& p {
margin: 0;
}
background: white;
}The background: white; declaration comes after the nested & p rule. Most of the time you won't notice any difference from putting it first, because the declarations apply to .box and the nested rule applies to .box p. They target different elements and never compete.
The order matters only when a declaration and a nested rule can apply to the same element and set the same property with equal specificity. This chapter explains how browsers resolve that case, and why the answer changed in 2024.
Two behaviors: hoisting and source order
Early implementations of nesting (Chrome 112 through 129, Safari 16.5 through 18.1, Firefox 117 through 131) treated all the loose declarations in a rule as one group. That group sat at the position of the parent's opening, no matter where the declarations appeared in the block. This is called hoisting. A declaration written after a nested rule behaved as if it had been written before it.
The current CSS Nesting specification takes a different approach. Declarations that follow a nested rule are wrapped in an implicit rule, a nested declarations rule. The browser places that rule in the cascade at the position where you wrote it. Source order now means what it says.
| Behavior | Declaration after a nested rule | Browsers |
|---|---|---|
| Hoisting (older) | Moves up, so the nested rule always comes later in the cascade | Chrome before 130, Safari before 18.2, Firefox before 132 |
| Source order (current) | Stays where written, so it can override the nested rule | Chrome 130+, Safari 18.2+, Firefox 132+ |
Version numbers change, so check a compatibility table such as MDN's before relying on them.
A worked example
Here is a case where the two behaviors give different results:
.box {
color: red;
@media (min-width: 40rem) {
color: blue;
}
color: green;
}Take a viewport that is 50rem wide.
- With hoisting, the browser sees
color: red; color: green;on.box, and the last one wins, so.boxis green. Then the media rule appliescolor: blue. The nested rule comes later, so the text is blue. - With source order, the browser sees three pieces in sequence:
color: red, then the media block withcolor: blue, thencolor: green. The last piece wins, so the text is green at every viewport width. The media rule is effectively dead code.
The reason is that the media block adds a condition but no specificity. Inside it, the declarations apply to the parent selector .box, which has specificity (0,1,0). The trailing color: green also applies to .box at (0,1,0). With equal specificity, the later one wins.
How the browser represents the trailing declarations
The trailing declarations are not a hack in the parser. They become a real object in the CSS Object Model (CSSOM), the browser's in-memory model of stylesheets. That object is a CSSNestedDeclarations rule. It has a style property holding the declarations, like any other rule.
You can see this in a browser console or a script:
<!doctype html>
<html lang="en">
<body>
<script>
const sheet = new CSSStyleSheet();
sheet.replaceSync(`
.box {
color: red;
@media (min-width: 40rem) { color: blue; }
color: green;
}
`);
const box = sheet.cssRules[0];
console.log(box.style.color);
for (const rule of box.cssRules) {
console.log(rule.constructor.name);
}
</script>
</body>
</html>In a browser with source-order behavior, the first console.log prints red. The loop prints CSSMediaRule and then CSSNestedDeclarations. The leading color: red stays on the parent's own style, because no nested rule precedes it. Only declarations that come after a nested rule go into a CSSNestedDeclarations object.
In an older browser, the same code prints green for box.style.color, because all declarations were merged. It then lists only the media rule.
Specificity of the trailing declarations
The implicit rule is not the same as writing & { color: green; }. A literal & { ... } behaves like :is(parent), so with a selector list as the parent it takes the most specific item's specificity. The implicit nested declarations rule keeps the specificity of the parent rule as written, per matching selector.
This matters when the parent is a list:
#main, .note {
color: red;
& p {
color: blue;
}
color: green;
}The color: green declaration applies to #main and .note themselves, not to p. For a .note element it uses .note's own specificity of (0,1,0), not #main's (1,0,0). The nested & p rule targets paragraphs inside them and does not compete with these declarations.
When order does not matter
Order only breaks ties. If a nested rule has higher specificity, it wins whether the declaration comes before or after it:
.btn {
background: blue;
&:hover {
background: green;
}
background: red;
}Here &:hover has specificity (0,2,0) and .btn has (0,1,0). The hovered button is green in every browser. Without hover, the button is red, because the trailing background: red comes after background: blue.
A nested rule and a trailing declaration tie only when the nested rule adds no specificity. The most common such cases are:
- Nested conditional at-rules such as
@media,@supports,@container, and@layerblocks, which apply declarations straight to the parent. - A nested
& { ... }with nothing else attached to it.
Rules with pseudo-classes, extra classes, or descendant selectors usually add specificity or target different elements.
Supporting older browsers
If your audience includes browsers that support nesting but hoist declarations (roughly mid-2023 through late 2024 releases), a stylesheet that relies on trailing declarations can behave differently between them. You have three ways to avoid this.
- Write all declarations first, then all nested rules. Both behaviors give the same result.
- If a declaration must override a nested at-rule block, wrap it explicitly in
& { ... }placed after that block. This works in both behaviors, because the explicit nested rule is a real rule and keeps its position. - Raise the specificity of the one that must win, for example by repeating a class.
Option 2 looks like this:
.box {
color: red;
@media (min-width: 40rem) {
color: blue;
}
& {
color: green;
}
}Both old and new browsers render this one as green, because the & { ... } rule appears after the media block, has the same specificity, and wins by order.
Practical guidance
- Put the parent's own declarations at the top of the block, then nested rules. This is the most readable layout and the safest across browsers.
- Don't use trailing declarations as a technique to override a nested
@mediablock. If you want a rule that overrides the media condition, write it so the override is obvious, such as a separate rule after the block. - When debugging a nested rule that "doesn't apply", look for a later declaration in the same parent block that sets the same property at equal specificity.
- Linters and formatters may reorder nothing here, so review the source order yourself when mixing declarations and nested rules.
Common errors
- Assuming a trailing declaration is always overridden by earlier nested rules. That was true under hoisting but is not true now.
- Assuming a trailing declaration always overrides nested rules. A nested rule with higher specificity, like
&:hover, still wins. - Testing only in one browser. The two behaviors differ only when the cascade ties, so the bug may show up only in an older browser.
- Expecting the trailing declarations to behave like
:is(parent). They keep the parent's own specificity.
Exercise
Given the CSS below, what color is the heading on a 60rem-wide viewport in a browser that uses source-order behavior? What color would an older hoisting browser show? h2.title { color: black; @media (min-width: 40rem) { color: purple; } color: gray; }
Show answer
In a source-order browser the heading is gray. The media block adds no specificity, so the trailing color: gray ties with it at (0,1,1) and wins because it comes later. In a hoisting browser the heading is purple, because the declarations merge into black then gray on the parent and the nested media rule is placed after them.
Exercise
Rewrite the rule from the previous exercise so that the heading is purple at 40rem and wider in both kinds of browser, and gray otherwise.
Show answer
Put the base declarations first and the media block last: h2.title { color: gray; @media (min-width: 40rem) { color: purple; } } With no trailing declaration, there is no tie to resolve, so both behaviors produce the same result.