CSS Nesting
For over a decade, nesting selectors inside one another — writing
.card { .title { … } } instead
of repeating
.card .title { … } — required
a preprocessor like Sass or Less. Native CSS nesting changes that.
It's now built directly into the language, supported by every
modern browser, with no build step required. It keeps related
styles visually grouped, reduces repetition, and introduces the
& nesting selector for
referencing the parent explicitly.
Flat CSS vs Nested CSS
Without nesting, every related selector is written flat and repeated. With nesting, child rules live inside their parent's curly braces — visually grouping everything that belongs to one component:
.card { background: #fff; } .card .title { font-size: 1.2rem; } .card .title { color: #0f172a; } .card .btn { background: blue; } .card .btn:hover { background: darkblue; } .card:hover { box-shadow: 0 4px 12px #0002; }
.card { background: #fff; &:hover { box-shadow: 0 4px 12px #0002; } .title { font-size: 1.2rem; color: #0f172a; } .btn { background: blue; &:hover { background: darkblue; } } }
Both compile to identical CSS rules — nesting is purely a writing convenience, not a new capability.
The & Nesting Selector
& explicitly represents
the parent selector. Plain nesting (a new selector on its own
line) implicitly means "descendant of the parent" and doesn't
require & — but combining
directly with a pseudo-class, modifier class, or compound selector
requires & to anchor it to
the parent without a space:
explicit parent reference
attached directly — needs &
compound class — needs &
new selector with space — & implied
.nest-demo-card {
background: #fff;
border: 2px solid #e2e8f0;
transition: border-color .2s, box-shadow .2s;
/* &:hover — attached directly to parent, NO space, needs & */
&:hover {
border-color: #264de4;
box-shadow: 0 8px 20px rgba(38,77,228,.15);
}
/* &.is-featured — compound modifier class, needs & */
&.is-featured {
border-color: #f97316;
background: rgba(249,115,22,.03);
/* nested again — .ndc-title INSIDE &.is-featured */
.ndc-title { color: #f97316; }
}
/* .ndc-title — plain descendant, & is implicit/optional */
.ndc-title { font-size: 13px; font-weight: 700; }
.ndc-sub { font-size: 10px; color: #64748b; }
.ndc-btn {
background: #264de4; color: #fff;
transition: background .2s;
&:hover { background: #1d3eb8; } /* nested hover, 3 levels deep */
}
}
/* ── & can also appear AFTER another selector ── */
.icon {
.dark & { filter: invert(1); } /* .dark .icon — & at the end */
}
Nesting Combinators & Pseudo-elements
Nesting works with all combinators (>, +,
~) and pseudo-elements (::before, ::after):
.nav {
/* Direct child combinator */
> li { list-style: none; }
/* Adjacent sibling */
li + li { margin-left: 1rem; }
/* Pseudo-element */
&::before { content: '📂'; }
/* Multiple selectors at once — comma-separated */
&.active, &.current { font-weight: 700; }
/* Attribute selector nesting */
&[data-state="open"] { display: block; }
/* :is() and :where() inside nesting */
&:is(:hover, :focus) { outline: 2px solid blue; }
}
/* ── :is() wrapping multiple ancestor contexts ── */
:is(.theme-dark, .dark-mode) {
.card { background: #1e1e2e; } /* nests under either parent class */
}
Nesting @media and @container
One of the most useful aspects:
@media and
@container can be nested
directly inside a selector block. This keeps all the responsive
variations of one component's styles physically together, instead
of scattering them across separate media query blocks at the
bottom of a file:
/* ── BEFORE: media queries scattered far from the rule they affect ── */
.sidebar { width: 280px; }
/* ... 200 lines of other CSS later ... */
@media (max-width: 768px) {
.sidebar { width: 100%; } /* easy to lose track of */
}
/* ── AFTER: nested — all .sidebar rules stay together ── */
.sidebar {
width: 280px;
@media (max-width: 768px) {
width: 100%; /* no selector repetition needed inside! */
}
@media (prefers-color-scheme: dark) {
background: #1e1e2e;
}
}
/* ── Nested @container works the same way ── */
.card-wrapper {
container-type: inline-size;
.card {
display: flex;
flex-direction: column;
@container (min-width: 400px) {
flex-direction: row; /* nested container query, no & needed here */
}
}
}
/* ── Nested @supports too ── */
.glass {
background: rgba(255,255,255,.9);
@supports (backdrop-filter: blur(1px)) {
background: rgba(255,255,255,.15);
backdrop-filter: blur(12px);
}
}
CSS Nesting vs Sass Nesting
Native CSS nesting was deliberately designed to closely mirror Sass syntax, but there are important differences in behaviour and a few syntax gaps:
| Aspect | Native CSS Nesting | Sass/SCSS Nesting |
|---|---|---|
| Build step | None — runs in the browser | Requires a compiler |
| & selector | Required for direct attachment (&:hover) | Same concept, same symbol |
| Plain nesting | .card { .title {} } valid, & optional | Same — & optional for descendants |
| String interpolation | Not supported (&.#{$var} invalid) | Supported via #{$variable} |
| Mixins/functions | Not part of nesting — use custom properties | @mixin, @include, @function available |
| Loops (@each, @for) | Not available natively | Available |
| Specificity | Matches the equivalent flattened selector | Matches the equivalent flattened selector |
| Nesting depth | No hard limit, but deep nesting hurts readability in both | Same caveat |
.card { .title {} } has
identical specificity to
.card .title {} written flat —
nesting changes nothing about how the cascade resolves conflicts,
only how the code is organised.
Nesting Quick Reference
| Pattern | Resolves to | & required? |
|---|---|---|
| .card { .title {} } | .card .title | No — descendant is implicit |
| .card { &:hover {} } | .card:hover | Yes — direct attachment |
| .card { &.active {} } | .card.active | Yes — compound class |
| .card { > .title {} } | .card > .title | No — combinator implies relation |
| .card { &::before {} } | .card::before | Yes — pseudo-element |
| .icon { .dark & {} } | .dark .icon | Yes — & placed mid/end of selector |
Try It Yourself
A fully nested component — including nested media queries — all in one rule block per component:
Quick Check — Chapter 53
.card { &:hover {} } →
.card:hover.card { &.active {} } →
.card.active.card { &::before {} } →
.card::beforeWithout &, the parser would otherwise interpret
:hover { } as a top-level
rule applying to all elements, not specifically
.card.& is optional for plain descendant selectors — a new selector that starts with a type, class, or ID and has implicit whitespace separation from the parent:
.card { .title {} } is
identical to
.card { & .title {} } →
both resolve to
.card .titleCombinators (
>,
+,
~) also don't need &
because the combinator itself implies the relationship to the
parent.
.card { .title { color: red; } }
has exactly the same specificity as
.card .title { color: red; }
written out flat — both are specificity (0,2,0): two class
selectors.This means all the normal cascade and specificity rules you already know apply unchanged. Nesting doesn't introduce a new priority system or override mechanism — it's syntax sugar that compiles (conceptually) to the same flat CSS rules a preprocessor would produce.
@media,
@container, and
@supports can all be
written directly inside a selector's curly braces:.sidebar { width: 280px; @media (max-width: 768px) { width:
100%; } }Without nesting, the media query would have to be written separately, often far away in the file:
@media (max-width: 768px) { .sidebar { width: 100%; }
}
— requiring you to repeat the selector and physically
separating related rules.Nesting media queries keeps every responsive variation of a component's styles co-located with the rest of that component's CSS. This makes components easier to maintain — when you change a component, all its breakpoint behaviour is visible in one place instead of scattered across dozens of media query blocks at different points in the file.
— No string interpolation: Sass's
&.#{$variant} has no
native CSS equivalent— No mixins or functions: Sass's
@mixin,
@include,
@function aren't part of
nesting — though CSS custom properties and functions like
calc() cover many of the same use cases— No loops: Sass's
@each and
@for have no native
equivalent— No variable arithmetic at compile time: Sass variables resolve at build time; CSS custom properties resolve at runtime with calc()
For pure selector organisation, native nesting fully replaces what most developers used Sass nesting for. For programmatic CSS generation (loops, functions, string building), Sass and other preprocessors remain useful — they can be combined with native nesting freely.
&:hover) because you're usually adding a state or modifier to the
current element. But & can be placed anywhere in the
nested selector string, which lets you express "ancestor
selects this element" relationships:.icon { .dark & { filter: invert(1); } }This resolves to
.dark .icon — meaning
".icon, when it appears inside a .dark ancestor." The &
sits at the end because the icon is the thing being styled,
but the condition (.dark) comes before it in the DOM
hierarchy.This pattern is useful for "context-aware" component styling — letting a component nested inside
.icon { } still react to
ancestor classes like theme wrappers, without writing a
separate top-level selector outside the component's nested
block.
.card { &:hover {} }
resolve to?
.card { .title {} }?
.icon { .dark & {} }
resolve to?
- Native CSS nesting requires no build tool — supported in all major browsers since 2023. Lets you write child selectors inside a parent's curly braces.
-
Plain descendant nesting:
.card { .title {} }→.card .title. & is optional here. -
& nesting selector: required for direct
attachment — pseudo-classes (
&:hover), compound classes (&.active), pseudo-elements (&::before), attribute selectors. -
Combinators (
>,+,~) don't need & — the combinator itself implies the parent relationship. -
& placement: usually at the start (
&:hover) but can appear anywhere —.dark &resolves to.dark .iconwhen nested inside.icon {}. - Nested @media/@container/@supports: keeps all responsive variations of a component physically together instead of scattered across the file.
- Specificity unchanged: nesting is a writing convenience only. A nested rule has identical specificity to the equivalent flattened selector.
- vs Sass: native nesting covers selector organisation but not string interpolation, mixins, functions, or loops — those still require a preprocessor if needed.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.