Skip to main content
← 🎨 CSS as a design systemΒ·Module B8 Β· Lesson 2
TaskBuild a card using nesting. .card { padding: 16px; background: white; border-radius: 8px; & h2 { margin: 0; color: #3B82F6; } & p { color: #71717A; margin: 8px 0 0; } &:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.1); } }.

CSS nesting: SCSS-like but native (2023+)

75 XP6 min
Theory

Native nesting shipped in browsers

.card {
  padding: 16px;
  background: white;

  & h2 {
    margin: 0;
    color: #3B82F6;
  }

  & p {
    color: #71717A;
  }

  &:hover {
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  }

  & .cta {
    background: #3B82F6;

    &:hover {
      background: #2563EB;
    }
  }
}

Looks like SCSS / Less. But it's now NATIVE CSS β€” no preprocessor needed.

The & is required (mostly)

.card {
  & h2 { ... }           /* descendant β€” & is required for clarity */
  &:hover { ... }        /* same-element state β€” & is required */
  & + .sibling { ... }   /* sibling combinator β€” & required */
  &.featured { ... }     /* same-element class β€” & required */
}

In some cases the & is implicit, but always-write-it is the safer rule.

When to nest

  • Container + its own state (.card + &:hover)
  • Container + its DIRECT children where the parent context matters

When NOT to nest

  • Deep nesting (>2 levels). Read like a hostile maze.
  • Crossing component boundaries. .page-header .card should NOT live inside .page-header β€” define .card once at the top level.

Browser support

Chrome / Edge 112+, Safari 16.5+, Firefox 117+. Shipped 2023. Safe everywhere in 2026.

πŸ”’

Sign up to start coding

Theory is open to everyone. The interactive editor, live preview, and check are unlocked with a 7-day free trial β€” card required, cancel anytime.

Sign up β€” free trial β†’

First 10 lessons in each track are free. No card needed for those.

← PreviousNext lesson β†’

Get one Python or web tip a day β€” by email

Short, hand-written, no spam. Unsubscribe in one click.