Native CSS Nesting: It's Not Sass
The migration looks like a formality. The project has used Sass for years mostly for a single feature – nesting – and browsers have supported it natively since 2023. So you rename components.scss to components.css, remove sass from package.json, and start the project. Everything looks the same. Almost everything: the red "Delete" button in the order card is now blue, like every other button. The console is empty, the file parses, the .card .btn--danger rule is where it belongs and sits below the rule that beats it.
That's not a browser bug. It's the first of several differences between a syntax that looks identical and two completely different mechanisms underneath.
Same syntax, different model
Sass is a text compiler. A nested rule is an instruction to it: take the parent selector, paste it in place of & (or in front of the child selector) and output the result as flat CSS. The browser never sees the nesting – it gets ready-made, expanded selectors.
Native nesting works at the matching level. The browser doesn't concatenate any text – & in a nested rule means "an element matching the parent selector", and formally it behaves exactly like :is() with the parent's selector list inside. As long as the parent has one simple selector, both models produce the same result. The differences show up in three places: selector lists, name concatenation and pseudo-elements.
& is :is(), and :is() takes the highest specificity
Here's the rule that broke the button from the intro:
css
#sidebar, .card {& .btn {background: #2563eb;}}.card .btn--danger {background: #dc2626;}
Sass expands it to #sidebar .btn, .card .btn – two independent selectors, each with its own specificity. For a button in a card, .card .btn is what counts, at (0,2,0). The .card .btn--danger rule has the same, comes later in the file, so it wins – the button is red.
The browser reads the same thing as :is(#sidebar, .card) .btn. And :is() takes the specificity of its most specific argument – regardless of which argument actually matched the element. The button in the card has nothing to do with #sidebar, and yet the rule gets ID-level specificity: (1,1,0). It beats .card .btn--danger no matter the order in the file.
css
/* Sass: #sidebar .btn, .card .btn → (1,1,0) and (0,2,0) separately *//* Native: :is(#sidebar, .card) .btn → (1,1,0) in both cases */
The same happens the other way round: .card { #sidebar & { … } } gives #sidebar :is(.card). The practical takeaway: a selector list in the parent where one item is clearly heavier than the rest raises the specificity of every nested rule – including the ones that never involve that heavier item. The simplest remedy is to split such a parent into two separate rules or to drop IDs from it – which is good practice anyway now that we have @layer.
No more name concatenation: &__title doesn't exist
The second difference hits every project written in BEM. In Sass, & is text, so you can glue it to anything:
scss
// Sass – works, generates .card__title and .card--featured.card {&__title { font-size: 1.25rem; }&--featured { border-color: gold; }}
In native CSS, & isn't text, it's a selector. You can combine it with other compound selectors – &.is-active, &:hover, &[aria-expanded="true"], & + & – but you can't glue a chunk of a class name onto it. &__title doesn't mean .card__title; it's an attempt to combine & with something the parser reads as a type selector – neither .card__title nor anything meaningful, so the rule quietly stops styling anything. No warning, because from the browser's point of view nothing unusual happened.
This is often the most laborious part of the migration: every &__element and &--modifier has to be written out as a full class name. Or – and this is often the better idea – you can ask whether you still need BEM at all. Its main job was to simulate scope through a naming convention, and that's what @scope, covered in the previous article, does today.
The pitfall: declarations after a nested rule
The third difference was the sneakiest for the first year, because it was about order, and in CSS order is meaning:
css
.card {color: black;@media (width >= 600px) {color: blue;}color: red;}
Reading top to bottom, the text should be red on a wide screen – color: red comes after the @media block. In the first version of native nesting (Chrome 112–129, Safari up to 18.1, Firefox up to 131) it was different: all of a rule's plain declarations were hoisted to its top, before the nested rules. color: red ended up next to color: black, and the @media with color: blue won. The same code produced blue text. Sass, incidentally, did exactly the same – hence the habit of always writing nested rules last.
The spec fixed this: declarations that come after a nested rule go into a separate, invisible block (CSSNestedDeclarations in the CSSOM) that keeps its place in the order. Chrome 130, Firefox 132 and Safari 18.2 now behave the way the code suggests – and newer versions of Dart Sass have adopted the same behavior, warning about the change with a mixed-decls message. Even so, the Sass habit remains sound: declarations first, nested rules after. Code like that reads unambiguously and works the same in every browser, old or new.
& can't see pseudo-elements
The last difference is minor, but it can catch you off guard. & can only represent elements, and a pseudo-element isn't an element as far as selector matching goes. So this:
css
.link,.link::after {&:hover {color: #2563eb;}}
gives .link:hover, .link::after:hover in Sass, while in the browser it becomes :is(.link, .link::after):hover – and :is() doesn't accept pseudo-elements, so the ::after part simply disappears. Nest pseudo-elements the other way round: .link { &::after { … } &:hover::after { … } }. That works everywhere and is more readable.
What native nesting does better
So as not to leave the impression that this is purely a list of losses: native nesting also has capabilities Sass doesn't have, or only has as a compile-time feature.
- Nested at-rules work in the browser, not in the compiler.
@media,@supports,@container,@layerand@scopecan go inside a rule – the container query from the container queries article stays with the component it's about. - Nested selectors show up in devtools exactly as you wrote them. You're not debugging generated CSS through source maps, but your own code.
- You don't need a build step. A stylesheet in a
<link>or a<style>works immediately – which pairs nicely with selector-less@scopein a component. - Elements without
&. Since Chrome 120, Safari 17.2 and Firefox 117 you can write.card { p { … } }– the earlier version of the spec required& pbefore a type selector.
css
@layer components {.card {container-type: inline-size;padding: 16px;h3 {font-size: 1.125rem;}@container (width >= 480px) {display: grid;grid-template-columns: 160px 1fr;gap: 16px;}&:has(img) {padding-top: 0;}}}
This single block combines three things I've written about in earlier articles – a layer, a container query and :has() – and also shows that a nested at-rule can contain declarations directly, without repeating the selector.
Pitfalls and best practices
- Don't nest deeper than two or three levels. Every level is another part of the selector and more specificity that isn't visible at first glance. Deep nesting in CSS has always been a sign that a rule knows too much about the HTML structure – native syntax doesn't change that.
- Watch out for selector lists in the parent. The heaviest item in the list sets the specificity of every nested rule. Avoid mixing IDs and classes in one parent.
- When migrating from SCSS, look for
&glued to text. The regular expression&[\w-]will find most of the&__elementand&--modifiercases that stop working after migration. - Declarations before nested rules. That works the same in old and new browsers and in every version of Sass.
- Check what your build tool does. If Lightning CSS, PostCSS or another bundler flattens nesting for older browsers in your
browserslist, the output can have different specificity than native:is()– and behave differently in dev and in production. Look at the generated file at least once. - Browser support. Basic nesting works since Chrome 112, Safari 16.5 and Firefox 117 (2023), the version without a mandatory
&before an element selector since Chrome 120 and Safari 17.2, and the correct order of declarations after nested rules since Chrome 130, Firefox 132 and Safari 18.2 (2024).
Summary
Native nesting looks like Sass, but it isn't a built-in version of it. Sass concatenates text and produces flat selectors; the browser treats & like :is() with the parent selector. Every migration pitfall follows from that one difference: a selector list in the parent raises specificity to the level of its heaviest argument, &__title stops meaning anything, and pseudo-elements drop out of &. On top of that comes the declaration order that worked differently in older browsers than the code would suggest. Know these differences, and you can safely remove Sass from your project – and along the way replace concatenated BEM names with something that actually solves the scope problem: @scope.