← Back to HTML & CSS, page 3

html-css-0043 · Hard senior

How does native CSS nesting work, and how does it differ from Sass nesting?

Native CSS nesting lets rules sit inside other rules, with & standing for the parent selector. Nested media and container rules apply to the parent. The parent behaves like :is(), so the nested rule takes the specificity of the parent. Unlike Sass, & cannot be glued to text such as &-title.

Full answer

Native CSS nesting lets you write a rule inside another rule. The inner selector is combined with the outer one, so related styles stay in one block.

The nesting selector & stands for the parent selector. You use it when the inner selector must attach to the parent directly, as in &:hover or &.active. You can place it elsewhere in the selector to wrap the parent, as in .dark &. Conditional group rules such as @media and @container can also be nested, and then they apply to the parent selector without repeating it.

Specificity: the parent in a nested rule behaves like :is() around the parent selector list, so it takes the specificity of the most specific selector in that list. A rule such as #panel { .title { ... } } has the specificity of #panel .title, which is (1,1,0), more than .page .title.

Main differences from Sass:

  • Native nesting runs in the browser, not at build time. The browser must support it.
  • You cannot build names by gluing text onto &, such as &-title or &__item. In native CSS, & is a selector, not a string.
  • Because of the :is() behaviour, a nested comma list can have specificity different from the Sass output.

The card gets a bold navy title and a yellow background. The card inside .dark takes the light text and dark background. The nested @media gives 24px padding from a 600px viewport. The note is red: #panel .note has a higher score than .page .note.

Keep nesting shallow, around two or three levels. Deep nesting creates long, high-specificity selectors that are hard to override and bring back the problems that flat CSS avoids. Nesting is also a newer feature, so check the support table for your audience, or compile it down with a build tool if you must support older browsers.

html
<style>
  .card {
    padding: 12px;
    border: 1px solid #999;
    color: #222;

    & .title { font-weight: bold; color: navy; }
    &:hover { border-color: crimson; }
    &.featured { background: #fff7d6; }
    .dark & { color: #eee; background: #222; }

    @media (min-width: 600px) { padding: 24px; }
  }

  #panel { .note { color: red; } }
  .page .note { color: blue; }
</style>
<div class="card featured" id="c"><span class="title" id="t">Notice</span></div>
<div class="dark"><div class="card" id="d">Dark card</div></div>
<div id="panel" class="page"><span class="note" id="n">Note</span></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • & represents the parent selector: &:hover, &.active, .dark &.
  • @media and @container can be nested inside a rule.
  • Nested rules take the specificity of the parent, like :is().
  • &-suffix name building from Sass is not valid native CSS.
  • Keep nesting shallow to avoid long, high-specificity selectors.

Common mistakes

  • writing &-title expecting BEM-style name building as in Sass. Native CSS does not support this.
  • forgetting that a nested rule's specificity includes the parent. Nesting inside an id selector makes every inner rule hard to override.

Follow-ups

  • when can you omit &?
  • Is native nesting handled by the browser or by a build step?
  • How does a nested rule's specificity compare to the same rule written flat?

Tips

  • Nest by relationship (state, modifier, context), not by markup depth. One level for hover and modifiers is usually enough.
  • Say that you nest by relationship such as state and context, not by markup depth.
  • Name the specificity effect of nesting under an id.