Sub topic 10
Declarations after nested rules
This chapter assumes you can write nested rules with &, know how specificity and source order decide the cascade, and have used @media. It covers what happens when you write a plain declaration after a nested rule inside the same parent.
The pattern
CSS lets you mix declarations and nested rules in one block:
.card {
padding: 1rem;
& .title {
font-weight: 600;
}
border: 1px solid #ccc;
}Here border comes after the nested & .title rule. Most of the time this looks harmless, because border and .title style different elements. Trouble appears only when a declaration and a nested rule compete for the same element and the same property.
Two behaviors
Browsers have handled this in two ways.
- Older behavior (hoisting). The browser treated every declaration as if it sat at the top of the parent block, whatever its position. Source order between declarations and nested rules was ignored.
- Current behavior (order preserved). A declaration that follows a nested rule is wrapped in an implicit rule, called a nested declarations rule. It takes part in the cascade at the position where you wrote it.
Current browsers use the order-preserving model. This applies to Chrome 130 and later, Firefox 132 and later, and Safari 18.2 and later. Browsers from before those releases still hoist.
Where the difference shows
A nested rule that matches the parent element with no added specificity ties with the parent's own declarations. Source order then decides the winner.
.box {
color: blue;
& {
color: green;
}
color: red;
}Every <div class="box"> is styled by the same three declarations, so the results differ by browser:
| Behavior | Result for `.box` | Why |
|---|---|---|
| Hoisting | green | `color: red` moves to the top, so `& { color: green }` comes later and wins. |
| Order preserved | red | `color: red` comes last in source order, so it wins. |
The same thing happens with nested conditional at-rules:
.panel {
display: block;
@media (min-width: 40rem) {
display: grid;
}
display: flex;
}With order preserved, .panel is flex at every width. The nested @media block has the same specificity as .panel, and display: flex comes after it. With hoisting, wide viewports got grid. In that case the media query was effectively overridden by a declaration the author expected to be a default.
When order does not matter
If the nested rule has higher specificity, source order is irrelevant, as in ordinary CSS:
.button {
background: white;
&:hover {
background: lightblue;
}
background: yellow;
}.button:hover has specificity (0,2,0) and .button has (0,1,0). A hovered button is light blue in both models. An unhovered one is yellow in both.
Writing it safely
- Put all of the parent's own declarations first, then the nested rules. This works the same in old and new browsers.
- If you must add a declaration after a nested rule, check whether any nested rule matches the parent element at equal specificity. That means
&alone, a nested@mediaor@supports, or&with a:where()pseudo-class. - Never rely on a later declaration "going back" to override a nested rule. Use specificity or a separate rule instead.
Exercise
In a browser that preserves order, what is the display value of .item at a viewport 50rem wide, given the CSS below? What would a hoisting browser give?
.item {
display: block;
display: flex;
@media (min-width: 40rem) {
display: grid;
}
}
Show answer
In a browser that preserves order, grid. The nested @media rule comes last and has the same specificity as .item, so it wins when the query matches. A hoisting browser also gives grid, because both models put the declarations first and the media rule last. The result only differs when a plain declaration is written after the nested rule.