Sub topic 11
What native nesting does not do that Sass does
This chapter assumes you can write nested rules with &, know how specificity works, and have seen Sass or SCSS. Many Sass habits carry over to native CSS nesting. A few do not, and they fail in ways that are easy to miss.
The core difference: text versus selector
Sass is a compiler. It runs before the browser sees anything, and it treats & as text to paste. Native nesting is part of the CSS engine. There, & stands for the parent selector as a whole, much like :is() wrapped around the parent. The browser never edits the selector text.
Most of the differences below follow from that one fact.
No suffix concatenation
In Sass you can build class names by appending to &:
.card {
&-title {
font-weight: 600;
}
&__body {
padding: 1rem;
}
}Sass compiles this to .card-title and .card__body. This is common in BEM-style code (a naming convention such as block__element).
Native CSS cannot do this. &-title is not a valid selector, so the browser drops the rule. Write the full selector yourself, either as a separate top-level rule or nested with a real selector:
.card {
padding: 1rem;
& .title {
font-weight: 600;
}
}
.card-footer {
border-top: 1px solid #ccc;
}Here .title is a descendant of .card. It is not a new class name built from .card.
Selector lists keep their specificity
Sass expands a parent list into separate selectors. Native nesting keeps the list together, so it behaves like :is() and takes the specificity of the most specific item.
#main, .note {
& p { color: red; }
}Sass outputs #main p, .note p. In that output, .note p has specificity (0,1,1). Native CSS treats the same source as :is(#main, .note) p. That makes .note p match at (1,0,1), because the #main item sets the specificity for the whole list.
A rule you moved from Sass to native CSS can therefore win cascade conflicts it used to lose.
Features that stay in the preprocessor
| Sass feature | Native CSS status |
|---|---|
| `&-suffix` concatenation | Invalid selector, rule is ignored |
| `$variables` | Use custom properties (`--gap`) instead |
| `@mixin` / `@include` | No equivalent |
| `@extend` | No equivalent |
| `@at-root` | No equivalent |
| Nested properties (`font: { family: x; }`) | Not supported |
| `//` line comments | Only `/* */` comments |
| Compile-time evaluation | Everything resolves in the browser |
Custom properties are the nearest replacement for variables, with one difference. They resolve at runtime, so they can change with the cascade, but you cannot use them to build selectors or media query conditions.
:root {
--gap: 1rem;
}
.card {
& .title {
margin-block: var(--gap);
}
}What both do
Conditional at-rules such as @media and @supports nest inside a style rule in both systems, and their declarations apply to the parent selector. Sass moves the at-rule outward during compilation. The browser does the same job natively. Leading combinators like > li and parent-context forms like .dark-theme & also work in both.
Fewer build steps, different failure modes
Native nesting needs no build step, so you can edit and reload. The cost is stricter error handling. Sass reports a bad selector as a compile error. A browser silently ignores an invalid nested rule and moves on, so check the DevTools Styles panel when a nested rule seems to do nothing.
Exercise
A Sass file contains .btn { &-primary { color: white; } }. You paste it into a native CSS file unchanged. What happens, and how do you fix it?
Show answer
The browser ignores the &-primary rule, because & cannot be joined to text. .btn-primary never gets color: white. Fix it by writing a separate rule, .btn-primary { color: white; }, or by nesting a selector the browser can parse, such as &.primary for a class on the same element.