What is CSS nesting?
CSS nesting lets you write style rules inside other style rules, so the styles for a component and everything inside it live together in one block. It mirrors the structure of your HTML, cuts down on repeated selectors, and makes stylesheets easier to read.
Nesting was one of the main reasons people used preprocessors like Sass. Now it's built into CSS itself, with no build tools needed, and it works in all modern browsers.
How nesting works
Imagine a simple block of HTML:
<div class="content">
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
</div>
Traditionally, you'd style it with a separate rule for each part, repeating .content each time:
.content {
padding: 16px;
}
.content h2 {
font-size: 1.5rem;
}
.content p {
color: #4b5563;
}
With nesting, the rules for the heading and paragraph go inside the .content rule:
.content {
padding: 16px;
h2 {
font-size: 1.5rem;
}
p {
color: #4b5563;
}
}
Both versions do exactly the same thing. A nested selector like h2 matches h2 elements anywhere inside a .content element, just like the .content h2 selector. In CSS terms, they're descendants: children, grandchildren and so on.
Tip: put a rule's own declarations (like padding above) first, and the nested rules after them. It's the clearest way to read, and it avoids confusion about which declarations come first.
The & nesting selector
Inside a nested rule, the & symbol stands for the parent selector. It's essential whenever you want to attach something directly to the parent itself, rather than look for elements inside it.
Pseudo-classes and pseudo-elements
The most common use is for states like :hover:
.button {
background: #1d4ed8;
&:hover { /* .button:hover */
background: #1e40af;
}
&::after { /* .button::after */
content: " →";
}
}
Without the &, :hover on its own would mean "any hovered element inside .button", which isn't what you want.
Combining classes
.button {
&.is-large { /* .button.is-large: one element with both classes */
font-size: 1.25rem;
}
}
Placing & later in the selector
The & doesn't have to come first. Putting it at the end lets you style an element based on where it sits, such as inside a dark section, while keeping all of its styles in one place:
.note {
background: #ffffff;
color: #1f2937;
.theme-dark & { /* .theme-dark .note */
background: #1f2937;
color: #f3f4f6;
}
}
Not supported: in Sass, &__title joins text onto the parent to create .card__title. Native CSS can't do this, because & always represents the whole parent selector. Write the full class name instead.
Nesting with combinators
You can start a nested selector with any combinator (the symbols that describe how elements are related):
.menu {
> li { … } /* only direct children of .menu */
+ p { … } /* a p immediately after .menu */
~ p { … } /* any p that follows .menu */
}
Here's the difference between a descendant selector and the child combinator. Both lists contain a nested list:
.list-a {
li { color: #be185d; } /* every li inside, at any depth */
}
.list-b {
> li { color: #be185d; } /* only the top-level li elements */
}
- Fruit
- Apples
- Pears
- Vegetables
- Carrots
- Peas
- Fruit
- Apples
- Pears
- Vegetables
- Carrots
- Peas
Nesting media and container queries
You can also nest at-rules such as @media, @container and @supports straight inside a rule. The declarations inside apply to the parent selector, so responsive styles sit right next to the styles they change:
.sidebar {
display: none;
@media (min-width: 900px) {
display: block; /* .sidebar, on wider screens */
}
@supports (position: sticky) {
position: sticky;
top: 16px;
}
}
This is one of the biggest everyday benefits of nesting. Instead of hunting through a stylesheet for the media query that affects .sidebar, you'll find it inside the .sidebar rule.
Specificity and other gotchas
Nested selectors act like :is()
When the parent rule has a list of selectors, the browser treats it like :is(). That means the nested rule takes the specificity of the most specific selector in the list, even for elements matched by a less specific one:
#banner, .box {
p { color: #b91c1c; } /* behaves like :is(#banner, .box) p */
}
.box p {
color: #15803d; /* comes later, but loses! */
}
The paragraph is inside .box, not #banner, but the nested rule still carries the ID's specificity, so it beats the later .box p rule. Avoid mixing IDs and classes in a parent selector list, so you don't get surprises like this.
Other things to watch for
- Don't nest too deeply. Every level adds to the selector. Three or more levels creates long, highly specific selectors that are hard to override. One or two levels is usually plenty.
- Nesting doesn't scope styles. A nested
pstill matches everypinside the parent, including those inside other components placed within it. Use direct-child selectors or more specific class names where that matters. - A mistake can hide a whole block. If the browser can't understand a nested selector, it ignores that nested rule and everything in it. Check your browser's developer tools if styles aren't applying.
Real-world example: a card component
Here's a complete component written with nesting: a card with an image, a heading, text and a link with hover and focus states, plus a container query that switches to a side-by-side layout when there's room. Everything about the card lives in one rule:
.card {
display: grid;
overflow: hidden;
border: 1px solid #e5e7eb;
border-radius: 12px;
img {
display: block;
width: 100%;
height: auto;
}
.card-body {
display: grid;
gap: 8px;
padding: 16px;
h3 { margin: 0; }
p { margin: 0; color: #4b5563; }
}
a {
justify-self: start;
padding: 10px 18px;
border-radius: 8px;
background: #1d4ed8;
color: #ffffff;
&:hover {
background: #1e40af;
}
&:focus-visible {
outline: 3px solid #1d4ed8;
outline-offset: 2px;
}
}
/* Side by side when the card's container is wide enough */
@container (min-width: 440px) {
grid-template-columns: 180px 1fr;
img {
height: 100%;
object-fit: cover;
}
}
}
Tip: the original real-world example styled the button with &:focus. Using &:focus-visible instead shows the outline to keyboard users without showing it every time someone clicks with a mouse.
Browser support and best practices
Native CSS nesting works in Chrome and Edge (120 onwards), Firefox (117 onwards) and Safari (17.2 onwards). Those versions date from late 2023, and nesting has been Baseline widely available since June 2026, so it's safe to use on everyday websites.
Out of date: the original version of this post (written in March 2024) recommended a preprocessor for reliable cross-browser support. That's no longer necessary. Also, the slightly older browsers it mentioned (Chrome 112–119) didn't need a flag: they supported nesting, but with a stricter rule that nested selectors had to begin with a symbol, such as &, . or >. Writing & h2 instead of h2 works in those versions too, if you still need to support them.
Best practices
- Nest states and close relationships, such as
&:hover,&::before, direct children and media queries, as that's where nesting helps most. - Keep it shallow: one or two levels deep.
- Put declarations before nested rules in each block.
- Prefer classes over long nested element chains.
.card-titleis clearer and easier to override than.card .body div h3. - Still using Sass? Most Sass nesting works the same natively, apart from building class names with
&. See our introduction to CSS preprocessors for how Sass and native CSS compare.
Wrapping up
Native CSS nesting lets you group a component's styles, states and media queries in one tidy block, with no build step. Remember that nested selectors match descendants, use & to attach states and context to the parent, use > for direct children, and keep nesting shallow, and your stylesheets will be shorter, clearer and easier to maintain.