Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

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

Questions in this chapter (7)
  • Draft
  • medium
  • Askedoften
  • Verified October 2026

Answer

Native nesting lets a style rule hold other style rules and group rules (@media, @supports, @container, @layer, @scope):

.card {
  padding: 1rem;
  > h2 { margin: 0; }       /* .card > h2 */
  p { color: gray; }        /* .card p */
  &:hover { outline: 1px solid; }
  .theme-dark & { color: white; }
  @media (width > 40rem) { padding: 2rem; }
}

A nested selector is relative: without & it means "descendant of the parent", and it may start with a combinator. & places the parent anywhere in the selector. Starting with a type selector (p) works since Chrome 120, Firefox 117 and Safari 17.2.

& isn't text substitution. It represents the elements the parent rule matches, as if written :is(<parent selector>). Sass concatenates strings at build time instead, which gives three differences:

  1. Specificity. :is() takes its most specific argument. In #nav, .menu { & a {} }, the nested rule is (1, 0, 1) even for .menu a; Sass emits #nav a, .menu a, each with its own specificity.
  2. No suffixes. Sass's &__title builds .card__title. In CSS, &-bar is invalid and the nested rule is dropped.
  3. Matching. .a .b { .c & {} } is .c :is(.a .b): it matches a .b with .a and .c ancestors in either order. Sass writes .c .a .b, which requires that order.

& also can't represent pseudo-elements, the same restriction as :is().

Declarations after nested rules keep their place, wrapped in a nested declarations rule (Chrome 130, Firefox 132, Safari 18.2); earlier versions moved them to the top. The CSSOM keeps the structure: a nested p is a child rule with selectorText & p.

Example

<style>
  #nav, .menu {
    & a { color: red; }
  }
  .menu .link { color: green; }

  .a .b {
    .c & { color: blue; }
  }
</style>
<ul class="menu"><li><a class="link" href="#">Link</a></li></ul>
<div class="a"><div class="c"><p class="b">Text</p></div></div>
<script>
  console.log(getComputedStyle(document.querySelector(".link")).color);
  // → rgb(255, 0, 0)
  console.log(getComputedStyle(document.querySelector(".b")).color);
  // → rgb(0, 0, 255)
</script>

Chrome 156 and Firefox 159 agreed. Compiled by Sass 1.107, the same source gives #nav a, .menu a and .c .a .b: the link would be green, since .menu .link (0, 2, 0) beats .menu a (0, 1, 1), and the paragraph wouldn't be blue.

Pitfalls

  • Porting BEM-style &__element and &--modifier rules from Sass. CSS drops them without an error; write the full class.
  • An ID anywhere in the parent's selector list raising every nested rule to ID specificity.
  • Expecting .foo::before { &:hover {} } to mean .foo::before:hover, as in Sass. & can't represent the pseudo-element, so the nested rule matches nothing.
  • Nesting deep to mirror the DOM. Each level adds specificity and coupling; nesting doesn't scope anything, which is what @scope is for.

Follow-ups

What does & mean outside any style rule?
The same as :scope, which in a document stylesheet is the root element: a top-level & { --top: yes; } applied to <html>.
Why define & as :is() instead of expanding selectors like Sass?
Expansion multiplies: three levels of three-item lists give 27 selectors. :is() keeps one selector, and the spec notes that a single specificity per selector keeps matching fast.
Can an at-rule nested in a style rule hold bare declarations?
Yes. .box { display: block; @media (width > 100px) { display: flex; } } worked; the CSSOM showed the inner display in a CSSNestedDeclarations rule inside the CSSMediaRule.

Exercises

What colour is the article?

article {
  color: green;
  & { color: blue; }
  color: red;
}

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.