Skip to main content

Modern CSS Authoring

Nesting & Scope

Written by Updated Nesting & Scope exercises

Keep related selectors together with native nesting and limit selectors to a component subtree with @scope. The lesson compares native selector nesting with @scope, shows the parent selector, nested conditions, scope limits, and scoping proximity.

You can now write CSS rules inside other rules, just like in Sass, with no build tool. And @scope lets you say "these styles only apply inside this component". They sound similar, but they solve different problems.

Nesting is about tidier source code: related rules sit together. @scope is about limits: it stops a selector from matching outside an area. This lesson builds on CSS Combinators and pairs well with Cascade Layers.

Nesting

Example

css

.card {
  padding: 1rem;

  & > h2 { margin-block-start: 0; }

  &:hover { border-color: #64748b; }

  @media (width >= 40rem) {
    padding: 1.5rem;
  }
}

& means "the parent selector", so &:hover becomes .card:hover, and & > h2 becomes .card > h2. You can even nest a media query inside the rule it changes, which keeps everything about the card in one place.

The trap is going too deep. Every level of nesting adds to the selector, and four levels down you have a long, strong selector that is hard to override. Two levels is plenty for most components.

@scope

Example

css

@scope (.article) to (.example) {
  a { text-decoration-thickness: 0.12em; }
  h2 { scroll-margin-block-start: 5rem; }
}

This styles links and headings inside .article, but stops at any .example box inside it, so the demo boxes in your article keep their own styles. Scoped rules still take part in the cascade as normal, with one extra tiebreaker: when two scopes match, the closer one wins.

Nesting and Scope Syntax

SyntaxPurposeExample
&Represents the nested rule's parent selector.&:hover
Nested selectorKeeps a related descendant rule inside its parent rule..card { .title { ... } }
@scopeLimits selectors to a subtree.@scope (.card) { ... }
Scope limitStops a scope before a nested boundary.@scope (.article) to (.comments)

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Nesting & Scope example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

.card {
  padding: 1rem;

  & > h2 { margin-block-start: 0; }

  &:hover { border-color: #64748b; }

  @media (width >= 40rem) {
    padding: 1.5rem;
  }
}

@scope (.article) to (.example) {
  a { text-decoration-thickness: 0.12em; }
  h2 { scroll-margin-block-start: 5rem; }
}
  </style>
</head>
<body>
  <main><article class="card"><h2>Nested card rules</h2><p>Hover the card to see its nested state.</p></article><section class="article"><h2>Scoped article</h2><a href="#">Scoped link</a><div class="example"><a href="#">Excluded example link</a></div></section></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
CSS nesting12012011717.2
@scope11811814617.4

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Nesting is supported everywhere current. @scope arrived in Firefox last, so if you need it in older browsers, keep a fallback such as plain class names.

Which one do you need?

  • Use nesting when your stylesheet repeats the same parent selector over and over.
  • Use @scope when styles from one part of the page are leaking into another.
  • You can use both together, or either one alone.

Nesting & Scope exercises

Nest a rule

In one rule for .menu, nest a rule for .link that makes the links teal.

The HTML your code works on
<nav class="menu">
  <a class="link" href="#">Home</a>
  <a class="link active" href="#">Shop</a>
</nav>
<div class="card">
  <h2 class="title">Tea</h2>
</div>

Result

Check your answer to see it here.