Component styles can become hard to scan when every descendant, state, and modifier repeats the same class prefix. Native CSS nesting keeps related selectors inside the component rule, so a card title, link, and featured state stay near the base card styles without a Sass or PostCSS build step.

The & nesting selector represents the parent selector at the current nesting level. A nested h2 rule styles headings inside the component, while &.is-featured attaches a class to the component itself. Leaving out & in a modifier rule would target a descendant instead.

Native nesting is parsed directly by current browsers. It does not flatten specificity differences between selectors, and a parent list containing an ID can raise the weight of every nested rule in that list. Class-based component selectors keep that weight predictable; older browsers need flat CSS rules for the styles they must receive.

Steps to nest CSS selectors in a component stylesheet:

  1. Create a file named index.html in your text editor with two plan cards and a link to styles.css.
    index.html
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Nested plan cards</title>
      <link rel="stylesheet" href="styles.css">
    </head>
    <body>
      <article class="plan-card" id="starter">
        <h2>Starter plan</h2>
        <p>Nested rules style this card's contents.</p>
        <a href="#starter">View Starter</a>
      </article>
      <article class="plan-card is-featured" id="team">
        <h2>Team plan</h2>
        <p>The modifier styles the card itself.</p>
        <a href="#team">View Team</a>
      </article>
    </body>
    </html>

    Both cards use .plan-card; only the Team card also has .is-featured. The link targets keep navigation within the same page.

  2. Create styles.css in the same folder with the page defaults and base card rule.
    styles.css
    body {
      margin: 24px;
      background: #f1f5f9;
      color: #334155;
      font: 18px/1.5 system-ui, sans-serif;
    }
     
    .plan-card {
      max-width: 440px;
      margin-bottom: 24px;
      padding: 20px;
      border: 2px solid #94a3b8;
      border-radius: 12px;
      background: white;
    }

    The base declarations come before the nested rules. Browsers without native nesting support need equivalent flat selectors; a feature query alone does not provide those fallback styles.
    Related: How to add CSS feature-query fallbacks

  3. Insert the descendant rules immediately before the closing brace of .plan-card in styles.css.
      h2 {
        margin: 0 0 8px;
        color: #1d4ed8;
        font-size: 24px;
      }
     
      p {
        margin: 0 0 12px;
      }
     
      a {
        color: #1d4ed8;
      }

    The nested h2 selector matches .plan-card h2, including headings deeper inside the card. An explicit & > h2 would restrict it to direct children.

  4. Insert a nested &:focus-visible rule after the color declaration inside the a block in styles.css.
        &:focus-visible {
          outline: 3px solid #1d4ed8;
          outline-offset: 3px;
        }

    At this nesting level, & refers to the card's link selector. The rule matches .plan-card a:focus-visible, so keyboard focus outlines the link itself.

  5. Add the card-level state rules after the a block but before the closing brace of .plan-card in styles.css.
      &.is-featured {
        border-color: #2563eb;
        background: #dbeafe;
      }
     
      &:focus-within {
        box-shadow: 0 0 0 5px #93c5fd;
      }

    &.is-featured matches .plan-card.is-featured on the same element. &:focus-within highlights a card containing a focused link. Native CSS cannot concatenate class-name fragments like Sass's &__title; a full descendant class name is needed instead.
    Related: How to debug CSS specificity

  6. Save index.html and styles.css in the same folder.
  7. Open the saved index.html file in your browser.

    The address starts with file:///. Its folder path depends on where you saved the file; no web server is needed for this HTML and CSS.

  8. Press Tab to focus the View Starter link in the page.

    If focus is still in the browser toolbar, further Tab presses reach the first link.

  9. Confirm that the nested rules style both cards and outline the focused Starter link and its card.

    Both headings are blue, the Team card has a blue border and light blue background, and Starter has a pale blue outer ring around its white card. The focused link has its own dark blue outline. Missing heading colors suggest a stylesheet or nesting-support problem; a Team card with the default gray border suggests that the modifier rule is outside .plan-card or lacks &.

Share this guide