Skip to content
Tutorials

CSS Nesting and Cascade Layers: What Changes When You Leave Sass

AIDToolStack EditorialNovember 7, 20267 min read
CSSSassCascade LayersTailwind CSS
Share:Twitter

If you still run Sass mostly for nesting, and fight specificity with ever-longer selectors and the odd !important, two plain-CSS features now cover both jobs. Native nesting has been Baseline since December 2023, and cascade layers since March 2022. The trouble is that nesting looks like Sass and behaves differently in a few places that matter, and layers flip some cascade rules you've relied on for years. This is the migration guide I wish I'd had: what changes, what breaks, and the order to do it in.

Nesting: the rules that matter

The CSS Nesting draft defines & by desugaring: it's replaced with the parent rule's selector "wrapped in an :is() selector". Everything else follows from that.

.card {
  padding: 1rem;

  h2 { margin-top: 0; }            /* .card h2       (descendant, & optional) */
  &.is-active { outline: 2px solid; } /* .card.is-active (compound, & required) */
  &:hover { background: #f9fafb; }  /* .card:hover */
  .sidebar & { padding: 0.5rem; }   /* .sidebar .card (reversed context) */

  @media (width >= 40em) {
    padding: 2rem;                  /* nested at-rules work too */
  }
}

MDN's guide to using nesting puts the key rule plainly: without &, the browser adds whitespace for you, so .b inside .a means .a .b. For a compound selector you have to write &.b.

Diagram showing nested CSS on the left and the flat selectors the browser matches on the right: #a, b with & c becomes :is(#a, b) c with specificity 1,0,1, and .card with &.active, .icon & and h2 becomes .card.active, .icon .card and .card h2
Diagram showing nested CSS on the left and the flat selectors the browser matches on the right: #a, b with & c becomes :is(#a, b) c with specificity 1,0,1, and .card with &.active, .icon & and h2 becomes .card.active, .icon .card and .card h2

One historical note if you support older browsers: MDN's compat data marks Chrome 112 to 119 and Safari 16.5 to 17.1 as partial, because they didn't accept nested rules starting with a type selector. Full support is Chrome 120, Firefox 117 and Safari 17.2. If your analytics still show those versions, write & h2 instead of h2 and it works in both.

Where native nesting differs from Sass

PatternSassNative CSS
&__title inside .cardcompiles to .card__titleinvalid; the nested rule is dropped
#a, b { & c {} }two selectors, #a c (1,0,1) and b c (0,0,2)one selector, :is(#a, b) c, (1,0,1) for both
Declarations after a nested rulein written order since Dart Sass 1.92.0in written order (with CSSNestedDeclarations)
Runs whereat build timein the browser

No string concatenation. This is the one that breaks BEM codebases. Sass lets you add suffixes like &__copy and &--open. The nesting spec explains why CSS can't: nesting "is not a syntax transformation, but rather matches on the actual elements the parent selector matches," and &Bar would be a type selector in the wrong position anyway. I checked .card6 { &__title { color: red } } in Chrome 152: the nested rule is discarded and .card6__title stays unstyled. No error, no warning.

Specificity flattens upward. The spec's own example:

#a, b {
  & c { color: blue; }
}
.foo c { color: red; }

On <b class="foo"><c>, the text is blue. The & takes the specificity of #a, the heaviest item in the parent list, so & c is (1,0,1) and beats .foo c at (0,1,1). Sass nests each item of a list separately, so it would have emitted #a c, b c; the <b> match scores (0,0,2) there, and red would win. Chrome 152 rendered it blue, as the spec says. If you nest under selector lists that mix IDs and types, expect some overrides to stop working after the switch.

Declaration order. Older browsers hoisted declarations that came after a nested rule. MDN notes that support for keeping them in order arrived with the CSSNestedDeclarations interface, which the compat data lists for Chrome 130, Firefox 132 and Safari 18.2. Dart Sass made the same change the default in 1.92.0. In Chrome 152, this ended up green, the last declaration:

.t9 {
  color: blue;
  @media (min-width: 1px) { color: red; }
  color: green;
}

The practical advice: put a rule's own declarations first and nested rules after them. Then it doesn't matter which behaviour a browser has.

Cascade layers: order beats specificity

Layers let you decide up front which group of styles wins, regardless of how specific the selectors inside are. From MDN's @layer reference: "once the layer order has been established, specificity and order of appearance are ignored."

/* 1. Declare the order once, first thing in your CSS */
@layer reset, vendor, components, utilities;

/* 2. Pull third-party CSS into a layer */
@import url("vendor/datepicker.css") layer(vendor);

/* 3. Add to layers from anywhere; the order above doesn't change */
@layer components {
  #app .btn.primary { background: #2563eb; }
}
@layer utilities {
  .bg-none { background: none; }   /* wins over the rule above */
}

That @import must come before other rules, except @charset and @layer statements, so it sits right after the order line.

Two rules catch everyone the first time:

  1. Unlayered styles beat layered ones. MDN's cascade layers guide says normal unlayered declarations win "even if the unlayered styles have a lower specificity and come first in the order of appearance." In Chrome 152, a plain .btn outside any layer beat #t7 .btn.primary inside components.
  2. !important reverses the order. For important declarations, the first-declared layer wins and unlayered styles lose. A !important in reset beat a !important in components in the same test page.
Diagram of two stacks of cascade layers: for normal declarations unlayered styles win, then components, then reset; for important declarations the order flips so reset wins, then components, then unlayered styles
Diagram of two stacks of cascade layers: for normal declarations unlayered styles win, then components, then reset; for important declarations the order flips so reset wins, then components, then unlayered styles

That reversal is deliberate: it lets a low layer like reset mark something as non-negotiable. It also means a stray !important in old unlayered code loses to any !important inside a layer, which is the opposite of what people expect.

How Tailwind v4 uses layers

If you use Tailwind, you already depend on this. The Preflight docs show what @import "tailwindcss" breaks down into:

@layer theme, base, components, utilities;
@import "tailwindcss/theme.css" layer(theme);
@import "tailwindcss/preflight.css" layer(base);
@import "tailwindcss/utilities.css" layer(utilities);

That's why a utility class can override a .card you defined in @layer components, as the custom styles docs show with class="card rounded-none". It also explains a common v4 surprise: any CSS you write outside a layer beats every utility, because unlayered wins. Put your own component CSS in @layer components. Our Tailwind v4 setup guide covers the install side, and if you're still choosing a framework, the Tailwind vs Bootstrap comparison is the place to start.

Migrating from Sass, in order

  1. Add the layer order line first. One @layer reset, vendor, components, utilities; at the top of your entry file changes nothing until rules go into layers, so it's a safe first commit.
  2. Move third-party CSS into vendor with @import ... layer(vendor). Your own unlayered code now beats it without specificity hacks. Delete the overrides that only existed to outrank the vendor file.
  3. Search for &- and &_ in your .scss files. Every hit is a concatenated class name. Write the full class (.card__title) as its own top-level rule. That's more typing, and it also makes the class greppable again.
  4. Check nested rules under selector lists. Any parent like #header, .nav now lends ID weight to every child. Split those rules or wrap the heavy part in :where().
  5. Rename the file and let the browser do the work. Plain .css with nesting needs no build step, though you can keep Sass for mixins and functions.
  6. Audit !important. Inside layers it flips. Grep for it and decide case by case.

Two tools help with the grunt work. Our CSS to SCSS converter goes the other direction, nesting flat CSS for you; be aware that for BEM names it writes &__element suffixes, which only Sass understands. For reading the result, the CSS formatter beautify mode indents nested rules and nested @media blocks correctly in my test.

A quick checklist

  • Order line declared once, before any @import that uses layer().
  • No &__ or &- left in files the browser reads directly.
  • Own declarations before nested rules in every block.
  • Nothing important left unlayered by accident: unlayered normal styles beat all layers.
  • Old !important reviewed now that layers reverse it.
  • Your browser targets are at least Chrome 120, Firefox 117 and Safari 17.2 for type-selector nesting, or you prefix those rules with &.

For the theming side, our post on custom properties and light-dark() builds on the same layer setup.

AE

Written by AIDToolStack Editorial

Researched from official documentation, specifications and vendor pages, listed under Sources below. Last checked November 2026.

Sources

  1. drafts.csswg.org/css-nesting-1/
  2. developer.mozilla.org/en-US/docs/Web/CSS/Guides/Nesting/Using
  3. developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/Nesting_selector
  4. github.com/mdn/browser-compat-data/blob/main/css/selectors/nesting.json
  5. github.com/mdn/browser-compat-data/blob/main/api/CSSNestedDeclarations.json
  6. github.com/mdn/browser-compat-data/blob/main/css/at-rules/layer.json
  7. developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@layer
  8. developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Cascade_layers
  9. sass-lang.com/documentation/style-rules/parent-selector/
  10. sass-lang.com/documentation/style-rules/
  11. sass-lang.com/documentation/breaking-changes/mixed-decls/
  12. tailwindcss.com/docs/preflight
  13. tailwindcss.com/docs/adding-custom-styles

Related Posts

Explore on AIDToolStack

Discover tools and servers featured in our directory.

MCP Servers

Trigger.dev Jobs MCP ServerManage Trigger.dev background jobs from your AI coding assistant. Monitor job runs, trigger tasks manually, inspect execution logs, manage schedules, and debug failed runs. The server provides operational visibility into your background job infrastructure — see which tasks are running, queued, or failed, and take action. Your AI can help diagnose job failures from execution logs, retry failed tasks, and optimize concurrency configurations for better throughput.Tailscale Network MCP ServerManage your Tailscale mesh VPN network from your AI coding assistant. View connected devices, manage ACLs, configure exit nodes, check device status, and manage DNS settings. The server provides visibility into your Tailscale network — see which machines are online, troubleshoot connectivity issues, manage network access policies, and configure routes. Useful for developers managing distributed infrastructure where Tailscale provides secure networking between services.Portainer Docker MCP ServerManage Docker environments through Portainer's API from your AI coding assistant. Control containers, images, networks, and volumes across multiple Docker hosts and Kubernetes clusters. The server provides tools for container lifecycle management, image pulling and building, stack deployment, and resource monitoring. Portainer's multi-environment management makes this server particularly valuable for teams managing containers across development, staging, and production environments.

Reviews

Sign in to leave a review.

No reviews yet. Be the first!

Comments

Sign in to leave a comment.

No comments yet. Be the first!

Sponsor Our Newsletter

Reach thousands of developers who are actively evaluating AI tools, MCP servers, and dev infrastructure. Our weekly newsletter goes to engaged technical decision-makers.

Growing subscriber baseHigh open rates

All sponsored content is clearly labeled per our editorial policy.