Native CSS Nesting: Writing Cleaner Styles Without Sass
How native CSS nesting works: the & selector, relaxed syntax, nested media queries, specificity and BEM differences from Sass, and when not to nest.
& is a selector, not text, so BEM-style &__element concatenation does not work; a nested rule behaves like :is(), so a parent list containing an ID gives every nested rule ID-level specificity; and nesting happens in the browser, so there is no compiled output to inspect. Nest states, pseudo-elements and queries. Avoid nesting that mirrors the DOM tree.
Ask a team why they still compile Sass and the answer is often a single word: nesting. Variables moved to custom properties years ago, most mixins were replaced by utility classes or components, and color functions gave way to color-mix() and relative colors. What remains is the comfort of writing hover states and media queries inside the rule they belong to.
Native CSS nesting, defined in the CSS Nesting Module, lets you write style rules inside other style rules. The nesting selector & refers to the parent rule’s selector. Since December 2023, every major browser supports nesting with the relaxed syntax, which means you can start a nested rule with a type selector such as h2 without prefixing &.
My position: native nesting is good enough to drop Sass for most projects, but only if you learn the three differences. Teams that paste their Sass into plain .css files without changes will ship subtle specificity bugs.
The basics
.card {
padding: var(--space-3);
border-radius: var(--radius-card);
h2 { /* same as .card h2 */
font-size: var(--text-xl);
}
&:hover { /* same as .card:hover */
box-shadow: var(--shadow-raised);
}
&.is-featured { /* same as .card.is-featured */
border-color: var(--color-accent);
}
& + & { /* same as .card + .card */
margin-block-start: var(--space-3);
}
&::before { /* same as .card::before */
content: "";
}
.theme-dark & { /* same as .theme-dark .card */
background: var(--gray-800);
}
}
The rules for when you need & are simple. Without &, a nested selector is a descendant of the parent, as if a space came first. With &, the parent is placed exactly where the & appears. So &:hover attaches the pseudo-class to the parent, &.is-featured requires both classes on one element, and .theme-dark & puts the parent after an ancestor context.
Nesting at-rules
Conditional at-rules can sit inside a style rule. Declarations placed directly inside them apply to the parent selector. This is the feature I use most, because the responsive behavior of a component stays next to the component.
.product {
display: grid;
gap: 1rem;
@container (width > 32rem) {
grid-template-columns: 14rem minmax(0, 1fr);
}
@media (prefers-reduced-motion: no-preference) {
transition: box-shadow 150ms ease;
}
@supports (grid-template-rows: subgrid) {
grid-template-rows: subgrid;
}
@layer overrides {
border-radius: 0;
}
}
The container query pattern above comes straight from the container queries guide. With nesting, each component file reads top to bottom: base layout, then how it adapts, then how it respects user preferences.
Difference 1: & is a selector, not a string
In Sass, & is text substitution, so &__title becomes .card__title. In native CSS, & represents the parent’s matched elements, and &__title means “the parent element followed by a type selector named __title“, which matches nothing.
/* WRONG in native CSS: silently matches nothing */
.card {
&__title { font-weight: 600; }
&--featured { border-color: gold; }
}
/* RIGHT: write the full class name */
.card__title { font-weight: 600; }
.card--featured { border-color: gold; }
/* Or nest by relationship instead of by name */
.card {
&.card--featured { border-color: gold; }
.card__title { font-weight: 600; }
}
If your codebase uses BEM with Sass concatenation, this is the biggest migration task. You can keep a build step for those files, rewrite the names in full, or move to nesting by relationship. Many teams find that @scope, cross-browser since December 2025, removes the reason for long BEM names entirely, because it limits a component’s selectors to the component’s own subtree.
Difference 2: nesting behaves like :is(), and that changes specificity
A nested rule is equivalent to wrapping the parent selector in :is(). For a single simple parent, that changes nothing. For a selector list, it changes specificity for every member of the list.
#sidebar, .widget {
a { color: var(--color-link); }
}
/* Native CSS treats this as: :is(#sidebar, .widget) a
Specificity: (1, 0, 1) for ALL matches, even inside a plain .widget
Sass outputs: #sidebar a, .widget a
Specificity: (1, 0, 1) and (0, 1, 1) separately */
That is the rule from the cascade and specificity guide: :is() takes the specificity of its most specific argument. A link inside .widget now carries ID weight, and a later .widget-link rule cannot override it. Avoid mixing IDs and classes in a parent list that has nested rules. Better still, avoid IDs in selectors entirely.
The same equivalence has a second effect. Because the parent is matched as an :is() group, .a .b { .c & {} } means .c :is(.a .b), which can match DOM structures that Sass’s textual output would not. In practice this rarely matters, but it explains the occasional unexpected match.
Difference 3: there is no compiled output
With Sass, you can open the output and see exactly which flat selectors were produced. With native nesting, the browser resolves nesting itself. DevTools shows nested rules with their parent context, which is usually clearer, but there is no file to grep for a final selector. Code review becomes more important, and so do the depth limits below.
A note on declaration order
Early implementations moved all declarations of a rule above its nested rules, even if you wrote some declarations after them. The specification was changed in 2024 so that declarations after a nested rule keep their written position, and current browsers follow it. To stay safe in older engines and to keep files readable, put a rule’s own declarations first and nested rules after them.
How deep to nest
Nesting makes it effortless to mirror the DOM tree in CSS, and that is the main way it goes wrong. Each level adds a descendant selector, raises specificity and couples the styles to the markup structure. Change the HTML and the CSS silently stops matching.
/* WRONG: mirrors the DOM, specificity (0, 5, 2) */
.page {
.main {
.article {
.content {
p {
a { color: var(--color-link); }
}
}
}
}
}
/* RIGHT: nest only what belongs to the component */
.article-content {
a {
color: var(--color-link);
&:hover { text-decoration-thickness: 2px; }
}
}
My guidelines for a team:
- Nest freely: pseudo-classes (
&:hover,&:focus-visible), pseudo-elements (&::before), modifiers (&.is-active,&[aria-expanded="true"]), and@media,@containerand@supportsblocks. - Nest carefully: one level of descendant elements that belong to the component, such as a card’s heading.
- Do not nest: layout wrappers, page structure or anything more than three levels deep.
Moving from Sass to native CSS
| Sass feature | Native replacement |
|---|---|
| Nesting | Native nesting, minus & concatenation |
$variables |
Custom properties |
darken(), lighten(), mix() |
color-mix() and relative color syntax with oklch() |
| Math | calc(), min(), max(), clamp(), round() |
@import partials |
Bundler imports, or @import with layer() |
@extend |
Shared classes or :is() grouping |
@mixin, @each, @for |
No full native equivalent yet; keep a build step if you depend on them |
The color functions deserve their own guide, and they have one: modern color with oklch(). If you still need to support browsers older than late 2023, tools such as Lightning CSS and the PostCSS nesting plugin can flatten native nesting at build time, so you can write the native syntax today and drop the transform later.
Frequently Asked Questions
Is native CSS nesting supported in all browsers?
Yes. Chrome, Edge, Firefox and Safari all support native CSS nesting, including the relaxed syntax that allows nested type selectors without a leading ampersand, since December 2023.
Can I use BEM with native CSS nesting?
Not with concatenation. In native CSS, the ampersand represents the parent’s elements rather than its text, so &__title does not produce .card__title. Write BEM class names in full, nest by relationship instead, or keep a preprocessor for files that rely on concatenation.
Does CSS nesting change specificity?
Nested rules behave as if the parent selector were wrapped in :is(), so a nested rule takes the specificity of the most specific selector in the parent’s list. With a single simple parent the result matches Sass; with a list that mixes IDs and classes it is higher.
Do I still need Sass in 2026?
For many projects, no. Nesting, custom properties, color-mix(), relative colors and math functions cover most of what Sass was used for. Keep Sass, or another build step, only if you depend on mixins, loops or BEM name concatenation.
Nest for clarity, not for structure
Native nesting gives you the readability of Sass without the build step, as long as you remember that & is a selector and that nested rules weigh like :is(). Keep nesting shallow, keep states and queries next to their component, and let layers and scope handle the rest. Next, anchor positioning shows another place where native CSS now replaces a JavaScript dependency.
Rule of thumb: if a nested block is more than three levels deep, it is describing your HTML, not your component.
Originally posted on 5 February 2026 and post last modified on 5 February 2026