Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Selectors

The & nesting selector

Reference for CSS nesting and the & selector: write rules inside rules, refer to the parent selector, nest media queries and understand specificity.

CSS nesting lets you write a style rule inside another one. The ampersand (&) is the nesting selector: it stands for the parent rule's selector, so you can attach pseudo-classes, extra classes or a surrounding context to it.

Native nesting works in the browser without a preprocessor. Unlike Sass, & is not string concatenation: it behaves as if the parent were wrapped in :is().

CSS
.card {
  padding: 1rem;

  &:hover { background: #f1f3f5; }
  .title { font-weight: 700; }
}

Try it

Nested rules

Syntax and arguments

CSS
.parent {
  color: red;               /* declarations */
  &:hover { }               /* .parent:hover */
  &.active { }              /* .parent.active */
  & > li { }                /* .parent > li */
  > li { }                  /* same, relative form */
  .child { }                /* .parent .child */
  .ctx & { }                /* .ctx .parent */
  & + & { }                 /* .parent + .parent */
  @media (...) { }          /* conditional group rules */
}
Part Description
& Represents the parent selector as a whole. Can appear anywhere in a nested selector, even more than once.
No & A nested selector without & gets an implied & in front (descendant), and one starting with a combinator is relative to the parent.
Nested at-rules @media, @supports, @container, @layer and @scope can be nested; declarations inside apply to the parent.

Specificity: & takes the specificity of the most specific selector in the parent list, like :is().

Usage notes

  • No string joining. &-suffix (as in Sass for BEM) is not valid in CSS; & is a selector, not text. Write .card-title as its own rule.
  • Parent lists. With .a, .b { &:hover { } } the result is like :is(.a, .b):hover. Specificity is that of the strongest member.
  • Declarations after nested rules. Plain declarations that appear after a nested rule are still applied, but they are treated as if wrapped in a nested block; keep declarations first for clarity.
  • Element selectors. Older engines needed & p for nested type selectors. Current browsers accept a bare p { } inside a rule.
  • Depth. Deep nesting copies the problems of over-long selectors. Two or three levels are usually enough.
  • Fallback. Browsers without support drop nested rules and may ignore the nested parts. Compile with a tool if you target older browsers.

Browser support

Supported in all current browsers (Baseline since late 2023). The relaxed syntax that allows bare element selectors (p { }) without & needs recent versions of Chrome, Edge, Firefox and Safari; older ones need & p.