Sub topic 11
What Native Nesting Does Not Do That Sass Does
This chapter assumes you know the & nesting selector, relaxed nesting syntax, and how specificity is calculated. It also assumes you have seen Sass (SCSS syntax) nesting. If you are moving a stylesheet from Sass to native CSS, this chapter covers what you can keep, what you must rewrite, and where the same-looking code behaves differently.
Compile time versus run time
Sass nesting is a text transformation. The Sass compiler reads your nested source, expands every selector, and writes flat CSS. The browser never sees the nesting.
Native nesting is a CSS feature. The browser receives the nested rules as written, keeps them nested in the CSSOM, and resolves & when it matches selectors. A nested & behaves like :is() wrapped around the parent selector.
Nearly every difference in this chapter follows from that split. Sass can build selectors by editing strings, because it does so before the browser runs. Native CSS can only use selectors that parse as valid selectors.
No suffix concatenation with &
In Sass, & is a string that you can extend. This is the usual way to write BEM class names (a naming convention such as block__element--modifier):
.card {
padding: 1rem;
&__title {
font-weight: 600;
}
&--featured {
border-color: gold;
}
}Sass compiles this to .card__title and .card--featured. Native CSS does not allow it. A nested selector is a real selector, and & stands for the parent's matched elements, not for its text. &__title parses as & followed by a type selector named __title, which is invalid. The browser drops the whole rule without reporting an error.
Write the full class name instead. You can still nest the rule under the block:
.card {
padding: 1rem;
& .card__title {
font-weight: 600;
}
&.card--featured {
border-color: gold;
}
}The two rules do different things. & .card__title selects a descendant. &.card--featured is a compound selector, so it selects the .card element that also has the modifier class. Sass &--featured produces the same compound result, but only because it compiles to .card--featured as a string.
Sass users sometimes lose the descendant versus compound distinction here, because &__title never made them write it. Native CSS makes the relationship explicit every time.
Specificity differs when the parent is a list
Sass expands a selector list into separate selectors. Native CSS treats & as :is() over the whole list, and :is() takes the specificity of its most specific argument.
#main, .note {
& p {
color: red;
}
}
.note p {
color: blue;
}Native CSS gives & p a specificity of (1,0,1), even when it matches a paragraph inside .note. That is higher than .note p at (0,1,1), so the paragraph is red.
Sass compiles the first rule to #main p, .note p { color: red; }. There, .note p has specificity (0,1,1) and ties with the later rule. The later rule wins, so the paragraph is blue.
The same source produces different colors. A stylesheet that depends on list-parent specificity in either direction will change behavior when you remove the Sass step. When you migrate, find nested rules whose parent is a selector list and check them against other rules in the cascade.
Features that need a preprocessor
Nesting is one feature of Sass. Others are often bundled in with it, and none of the following exists in native nesting:
| Sass feature | Native status | What to do instead |
|---|---|---|
| `&-suffix`, `&__suffix` | Invalid selector, rule dropped | Write the full class name |
| `@at-root` | Not available | Move the rule outside the parent |
| `$variable` | Not available | Custom properties with `var(--name)` |
| `@mixin` and `@include` | Not available | Shared classes, custom properties, or a build step |
| `@extend` and `%placeholder` | Not available | Selector lists, or multiple classes in HTML |
| Property nesting (`font: { size: 1rem; }`) | Not available | Write `font-size` in full |
| `#{...}` interpolation | Not available | Not possible in plain CSS |
| `//` line comments | Not available | Use `/* ... */` |
Custom properties are close to Sass variables but not the same. They are resolved at run time and inherit through the DOM. They cannot be used inside a media query condition, so a Sass pattern like @media (min-width: $tablet) has no direct equivalent. Write the length out.
Nested at-rules: similar, but not Sass bubbling
Sass lets you nest @media inside a rule and bubbles it to the top level. Native CSS supports the same authoring pattern for conditional at-rules: @media, @supports, @container, @layer, and @scope. Declarations inside them apply to the parent selector.
.panel {
display: block;
@media (min-width: 40rem) {
display: grid;
}
}The difference is that the browser stores this structure as written. Sass would output a separate @media block after .panel. The cascade result is usually identical, but tools that read document.styleSheets will see a nested rule, not a flattened one.
Sass also has its own at-rules that CSS does not, such as @include, @if, and @each. Those cannot be nested natively because they do not exist.
Declarations after nested rules
Sass moves declarations that follow a nested rule up to the parent's first block, because it emits the parent rule before the nested rules. Recent Dart Sass versions warn about this with a mixed-decls deprecation.
Current browsers keep source order. A declaration after a nested rule is treated as a separate block at that position in the cascade.
.box {
color: red;
& {
color: blue;
}
color: green;
}Specificity is equal for all three declarations, so the last one wins, and the text is green. In older Sass output, the green declaration would be hoisted next to the red one and blue would win. Avoid depending on either behavior. Put declarations first and nested rules after them.
Silent failures when migrating
Native nesting fails quietly. An invalid nested selector drops that nested rule, and in a selector list, one invalid selector drops the whole rule. Sass reports the same mistakes at compile time.
You can find them with a search:
grep -rnE '^\s*&[-_]' src/stylesAlso check browser support. Relaxed nesting, where a nested type selector like a { } needs no &, arrived later than the first nesting implementations. If you must support older browsers, keep using & before type selectors, or keep Sass as a build step.
A migration checklist
- Replace every
&-suffixand&__suffixwith a full class name, using&.classfor compound and& .classfor descendant. - Replace
@at-rootblocks by moving the rule out of its parent. - Convert
$variablesto custom properties, except where they appear in media conditions. - Check nested rules under selector lists for specificity changes.
- Move declarations above nested rules.
- Replace
//comments with/* */.
Exercise
This Sass compiles to .btn and .btn-primary. Rewrite the nested part as valid native CSS that selects the same elements.
.btn {
padding: 0.5rem 1rem;
&-primary {
background: royalblue;
}
}
Show answer
Native CSS cannot extend & with a suffix, so write the full class name as a compound selector:
.btn {
padding: 0.5rem 1rem;
&.btn-primary {
background: royalblue;
}
}
A less nested option is a separate top-level .btn-primary rule. Note that &.btn-primary only matches elements that have both classes. Sass &-primary produced .btn-primary, which matches elements with only that class. Add btn to the markup, or use a top-level rule, if elements carry only the modifier.
Exercise
A stylesheet has #app, .widget { & a { color: red; } } followed by .widget a { color: blue; }. In native CSS, what color is a link inside .widget (and not inside #app), and why? How does Sass output differ?
Show answer
The link is red. In native CSS, & a behaves like :is(#app, .widget) a, whose specificity is (1,0,1) because :is() takes its most specific argument. That beats .widget a at (0,1,1), regardless of order. Sass expands the list to #app a, .widget a. There .widget a has specificity (0,1,1) and ties with the later rule, so the later blue rule wins and the link is blue.