Is native CSS nesting enough to drop Sass?

With Firefox 117 out at the end of August, native CSS nesting works in all three major browsers.

With Firefox 117 out at the end of August, native CSS nesting works in all three major browsers. For a lot of projects, nesting was the main reason Sass was in the build at all. So can it go?

What already works like Sass

.card {
  padding: 1rem;
  border-radius: 8px;

  &:hover {
    box-shadow: 0 2px 8px rgb(0 0 0 / 0.15);
  }

  .title {
    font-weight: 600;
  }

  @media (min-width: 40rem) {
    padding: 1.5rem;
  }
}

Pseudo-classes with &, nested class selectors and media queries inside a rule all behave the way you’d expect coming from Sass.

The catch with element selectors

In Chrome and Safari, this doesn’t work yet:

.card {
  p {
    margin: 0;
  }
}

The first version of the spec didn’t allow a nested selector to start with a letter. The parser couldn’t tell p { apart from the start of a property like padding: without looking ahead. The spec has since been relaxed and Firefox 117 already ships the relaxed version, but until Chrome and Safari catch up, start element selectors with &:

.card {
  & p {
    margin: 0;
  }
}

I’d put & in front of every nested selector for now. It works everywhere and it’s obvious at a glance which lines are nested rules.

No gluing class names together

This is the big one if you use BEM. In Sass, & is text that gets pasted in:

.card {
  &__title { font-weight: 600; }     // .card__title
  &--featured { border-color: gold; } // .card--featured
}

In CSS, & stands for the parent selector as a whole, not as a string. &__title doesn’t turn into .card__title, and there’s no other way to build a class name out of pieces. Those rules have to be written out in full.

Specificity can change

Native nesting treats the parent as if it were wrapped in :is(). That’s normally invisible, but it matters when the parent is a list:

#sidebar, .panel {
  & a {
    color: teal;
  }
}

Sass compiles this to #sidebar a, .panel a, so links in .panel get class-level specificity. Native CSS reads it as :is(#sidebar, .panel) a, and :is() always takes the specificity of its most specific argument. So links in .panel suddenly have ID-level specificity and can override things they didn’t before. If you see odd overrides after switching, check for selector lists in parents.

What Sass still gives you

  • mixins and functions,
  • loops and maps for generating utility classes,
  • @extend,
  • variables that work in media queries.

That last one is easy to miss. Custom properties replace most Sass variables, but @media (min-width: var(—tablet)) doesn’t work. Media query conditions can’t use custom properties, so breakpoints are one place where a Sass variable still does something CSS can’t.

Color math is mostly covered now. color-mix() works in all three browsers, and calc() with custom properties handles the spacing math most stylesheets need.

Older browsers

A browser without nesting support doesn’t partly understand your stylesheet, it simply drops the nested rules. If you still support Safari before 16.5 or Chrome before 112, keep a build step that flattens nesting, like postcss-nesting or Lightning CSS. You get to write native syntax now and remove the build step later.

So, can it go?

If your Sass is mostly nesting and variables, probably yes. Write & in front of nested selectors, move variables to custom properties, keep breakpoints in a few repeated media queries, and spell out BEM class names. If you rely on mixins, loops or a big @extend setup, Sass still earns its place.