CSS Nesting & @scope
Native CSS Nesting lets rules nest with & (desugaring under the hood to :is()) without Sass. @scope adds true DOM boundaries ("donut scope") with upper roots and lower to (...) exclusions, plus proximity-based tie-breaking in the cascade.
BEM Naming Conventions vs Native Nesting + @scope
Compare verbose BEM class chains against native & nesting and @scope.
.card__title { font-weight: 600; }
.card__body .card__title { color: #333; }
/* Leaks into nested sub-cards unless renamed */Requires unique class prefixes to prevent nested theme leaks.
@scope (.card) to (.card-slot) {
.title {
font-weight: 600;
&:hover { text-decoration: underline; }
}
}Matches .title inside .card, stops at .card-slot, and requires no BEM prefix.
Under the Hood
Sass nesting is just string concatenation at build time; native CSS Nesting and @scope are live structural rules inside Specificity & Inheritance.
:is()Desugaring:.a, #b { & .c {} }evaluates as:is(.a, #b) .c, giving every match#b's specificity.- Donut Scope:
@scope (.card) to (.slot)matches elements inside.cardand stops before entering.slot, preventing parent styles from bleeding into slotted children. - Proximity Tie-Breaker: Between two equally specific
@scoperules, the one whose scope root is fewer DOM hops away wins, sitting right below Cascade Layers in precedence.
@scope (.theme-dark) to (.theme-light) {
a { color: #93c5fd; }
& .badge { background: #1e293b; }
}Problem Genealogy
- 2006–2022: Sass/PostCSS provided nesting, while BEM and CSS Modules simulated scoping via mangled class names.
- 2023–2026: Native CSS Nesting and
@scopemoved nesting and DOM-bounded scoping directly into the browser's CSS parser.
Production Pitfalls
- BEM concatenation trap: Unlike Sass, native
&__itemdoes not concatenate class names into.card__item; write& .itemor separate classes. :is()specificity inflation: Nesting under a comma-separated list containing an ID or class inflates specificity for all branches.
- When not to use
@scope: Avoid@scopewhen you want strict shadow-boundary encapsulation of every inherited property; use Shadow DOM instead.
Connections & Logical Next Steps
Native nesting wraps parent selectors in :is(), inheriting their highest specificity.
@scope adds proximity-based cascade resolution below specificity.
@layer orders global stylesheet precedence, whereas @scope bounds selector reach and proximity within a subtree.