← Back

CSS Nesting

This page explains what CSS nesting is, where it came from, how the ampersand works, and how I would use nesting in a real project.

Generative AI Research Summary

CSS nesting is a newer feature that allows developers to write CSS rules inside other CSS rules. This helps group related styles together and makes stylesheets easier to read and maintain, especially when working on larger projects.

The idea for CSS nesting mostly comes from preprocessors like Sass and Less. These tools let developers nest CSS rules long before browsers supported it directly. Since developers found that approach easier to organize, the CSS Working Group eventually worked toward bringing nesting into native CSS.

A key part of CSS nesting is the ampersand (&) selector. The ampersand represents the parent selector in a nested rule. It lets developers extend the parent selector instead of repeating it. For example, if a rule is nested inside a .card selector, using &:hover would translate to .card:hover.

Browser support for CSS nesting has improved in recent years. According to data from caniuse.com, modern versions of Chrome, Edge, Safari, and Firefox support CSS nesting. Still, it is smart to double-check compatibility when building for production websites.

“Research and Document the concept of CSS nesting...” prompt. ChatGPT, GPT-5.3, OpenAI, 08 Apr. 2026, https://chat.openai.com/.

Example HTML

<div class="card">
    <h2>CSS Nesting Example</h2>
    <p>This card demonstrates nested CSS rules.</p>
    <button class="btn">Click Me</button>
</div>

Example CSS Using Nesting

.card {
  background: #f4f4f4;
  padding: 20px;
  border-radius: 10px;
  width: 300px;

  h2 {
    color: #1f4e79;
  }

  p {
    font-size: 14px;
  }

  .btn {
    background: #1f4e79;
    color: white;
    padding: 8px 12px;
    border: none;
    border-radius: 6px;

    &:hover {
      background: #163a5c;
      cursor: pointer;
    }
  }
}

What on the Demo Page is Being Styled

The demo page contains a simple card layout with a heading, paragraph, and button. The CSS nesting keeps the styles for these elements grouped together under the .card selector.

The card container controls the main layout including the background color, padding, width, and rounded corners. Inside that rule, nested selectors style the elements within the card.

  • The h2 selector changes the heading color.
  • The p selector adjusts the paragraph font size.
  • The .btn selector styles the button.

Because these selectors are nested, they only apply to elements inside the card instead of affecting those elements across the entire page.

How the Ampersand Was Used as a Nesting Selector

The ampersand (&) acts as a reference to the parent selector. In the example code, it is used inside the button styling to define the hover state.

&:hover {
  background: #163a5c;
}

Since this rule is nested under the .btn selector, the browser interprets it as .card .btn:hover. Using the ampersand keeps the code shorter and avoids repeating the entire selector.

How I Would Use Nesting in a Project

In a real project I would use CSS nesting when styling components that contain multiple elements. Things like navigation bars, cards, sidebars, or sections of a layout often have several related styles that belong together.

Nesting allows those styles to stay grouped under one main selector instead of being spread throughout the stylesheet. That makes the CSS easier to read, easier to maintain, and clearer for other developers to understand later.

It also helps reduce repetition in selectors and keeps the structure of the CSS closer to the structure of the HTML.