# CSS Nesting

> Write child rules directly inside their parent. Avoid repetition and make your stylesheets reflect your HTML structure.

Subject: CSS | Lesson 37 of 44 | 4 min read
Interactive lesson: https://codewithanimation.in/css/css-nesting
Previous: [Scroll Snap & Smooth Scroll](https://codewithanimation.in/css/scroll-snap-and-behavior.md) | Next: [:has() The Parent Selector](https://codewithanimation.in/css/has-parent-selector.md)

## Grouping related styles together

Native CSS nesting allows you to place CSS rules inside other CSS rules. Instead of repeating the parent selector multiple times, you write it once and nest the child selectors inside.

This mirrors the nested structure of HTML, making your stylesheet much easier to read and maintain.

For years, developers relied on CSS preprocessors like Sass or Less to get this feature. Today, it is built natively into all modern browsers.

## Less repetition, easier maintenance

Without nesting, a component's styles are scattered across independent rules: `.card`, `.card h2`, `.card .button`. If you rename the `.card` class, you have to update it in every selector.

With nesting, the parent is declared once. Everything related to the component lives neatly inside its block. This makes refactoring significantly safer and faster.

It also makes it immediately obvious which styles belong to a specific component, acting as natural encapsulation.

## Using the nesting selector (&)

To nest a descendant, you simply write the child selector inside the parent's curly braces. For example, `article { p { color: gray; } }` targets paragraphs inside articles.

If you need to chain a pseudo-class or pseudo-element directly to the parent, use the ampersand `&`. Writing `.btn { &:hover { } }` correctly targets the button itself when hovered.

The `&` represents the parent selector. You can also use it to invert the relationship: `.dark-theme & { }` means "when the parent is inside a dark-theme container".

## Common mistake: String concatenation and missing ampersands

If you used Sass in the past, you might be used to concatenating strings with the ampersand (e.g., writing `&-title` inside `.card` to target `.card-title`). Native CSS nesting strictly targets DOM structure; it cannot concatenate class names. `&-title` is invalid CSS.

A common mistake is omitting the `&` when targeting a pseudo-class. `.box { :hover { } }` actually targets any hovered *descendant* inside the box, not the box itself. Always write `&:hover`.

## Tip: Understanding specificity

In native CSS nesting, the `&` selector calculates its specificity as if it were wrapped in an `:is()` function. It takes the highest specificity from the comma-separated list of parent selectors.

For example, in `#nav, .menu { a { color: blue; } }`, the `a` tags inside both `#nav` and `.menu` get the high specificity of the ID selector.

## Try it: Try it

Watch how a deeply nested CSS rule expands into its flat, traditional CSS equivalent.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/css-nesting

## Check yourself

### Question 1

How do you apply a hover state to a `.button` from inside its nested block?

- A. &:hover { }
- B. :hover { }
- C. button:hover { }
- D. @nest :hover { }

Correct answer: A

The `&` explicitly refers to the parent selector. Writing `&:hover` attaches the pseudo-class directly to the parent element.

- Why not B: Without the `&`, this targets any hovered descendant inside the `.button`, not the button itself.
- Why not C: This ignores the parent context and redundantly re-specifies the tag name.
- Why not D: The `@nest` rule was dropped from the final CSS specification. It is no longer required or valid.

### Question 2

True or False: In native CSS, you can write `&-icon` inside `.card` to automatically generate the `.card-icon` class.

- A. False
- B. True

Correct answer: A

False. Native CSS nesting operates on full CSS selectors, not text strings. You cannot use it to dynamically build BEM-style class names like you could in Sass.

- Why not B: This works in preprocessors like Sass, but native CSS nesting cannot concatenate strings. It strictly maps to the DOM hierarchy.

### Question 3

How is the specificity of a nested rule calculated?

- A. It uses the highest specificity of the parent options, exactly like :is()
- B. It adds the parent and child specificity together, exactly like traditional CSS
- C. Nested rules always have zero specificity
- D. Nested rules always win over flat rules

Correct answer: A

Under the hood, native CSS nesting wraps the parent selector in `:is()`. If the parent is a list like `h1, #header`, the nested rule adopts the high specificity of the ID `#header` for all matches.

- Why not B: While traditional CSS adds specificity, native nesting treats the parent reference `&` as `:is(parent)`, which takes the highest specificity of the parent's options.
- Why not C: Nested rules definitely carry specificity, inherited from the parent selector via `&`.
- Why not D: Nesting does not grant automatic priority. Specificity and source order still dictate the winner.

## Remember this

- Native nesting groups related styles, making stylesheets mirror the HTML structure.
- Use the `&` selector to explicitly chain pseudo-classes like `&:hover` or pseudo-elements.
- Unlike Sass, native CSS nesting cannot concatenate strings to build class names.
- Nested rules calculate their specificity using the `:is()` function behavior.
