Tutorials CSS CSS Nesting
Ch 53 / 60
Chapter 53 Intermediate 8 min read CSS

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.

What you'll learn
Basic descendant nesting
The & nesting selector
Nesting pseudo-classes and combinators
Nesting @media and @container
CSS nesting vs Sass nesting
Specificity rules with nesting

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:

Without nesting — flat, repetitive
.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; }
With nesting — grouped, no repetition
.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:

.card { &:hover { … } /* .card:hover — no space */ &.featured { … } /* .card.featured — compound */ .title { … } /* .card .title — descendant, & optional */ }
&
explicit parent reference
:hover
attached directly — needs &
.featured
compound class — needs &
.title
new selector with space — & implied
Live demo — hover the card, compare normal vs featured
Standard Card
Hover me — uses &:hover
Learn More
CSS — the card component above, fully nested
.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):

CSS — combinators, pseudo-elements, and lists
.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:

CSS — nested @media and @container
/* ── 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
Specificity note
A nested rule has the same specificity as if you had written the fully-qualified selector by hand. .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
Free Resource
CSS3 Cheat Sheet — Nesting & Modern Syntax
Complete reference covering CSS nesting, the & selector, and nested media queries.
Download PDF

Try It Yourself

A fully nested component — including nested media queries — all in one rule block per component:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 53

Questions & Answers
& is required whenever the nested selector attaches directly to the parent without whitespace — pseudo-classes, pseudo-elements, and compound class/attribute selectors:
.card { &:hover {} } → .card:hover
.card { &.active {} } → .card.active
.card { &::before {} } → .card::before

Without &, 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 .title

Combinators (>, +, ~) also don't need & because the combinator itself implies the relationship to the parent.
No. Nesting is purely a writing convenience — the browser internally "flattens" nested rules into the equivalent fully-qualified selectors before computing specificity.

.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.
Yes. @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.
Native CSS nesting only handles selector nesting — it does not bring along the rest of Sass's programming features:

— 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.
Most nesting puts & at the start (&: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.
Multiple Choice Questions
Question 1 of 5
What does .card { &:hover {} } resolve to?
Question 2 of 5
Is the & required in .card { .title {} }?
Question 3 of 5
Does CSS nesting change the specificity of the resulting selector compared to writing it flat?
Question 4 of 5
Which CSS rule can be nested directly inside a selector block?
Question 5 of 5
What does .icon { .dark & {} } resolve to?
Chapter Summary
  • 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 .icon when 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.
~8 min read
Intermediate level
Chapter 53 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel