Native CSS Nesting
Lesson 23 of 41 in the CSS3 learning path.
How do I nest selectors in plain CSS?
Native CSS nesting lets a child, a state, or a media query live inside the parent rule. The nesting selector & stands for the parent. & h2 is a descendant. &:hover and &.is-open are compounds on the parent itself, not children that happen to have those names.
A @media rule nested inside the component applies to that same parent when the query matches. Specificity of a nested selector includes the parent. If the parent list contains an ID, the nested rule is as strong as that ID, even for a branch that looks like a simple class.
Syntax & Parameters
.card { & h2 { margin: 0; } &:hover { background: #f8fafc; } }
Example
Interactive preview available.card {
display: flex;
flex-direction: row;
gap: 0.75rem;
padding: 1rem;
border: 1px solid #e2e8f0;
border-radius: 12px;
background: #ffffff;
& h2 {
margin: 0;
font-size: 1.05rem;
}
& p {
margin: 0.25rem 0 0;
}
& img {
width: 4.5rem;
height: 4.5rem;
object-fit: cover;
border-radius: 8px;
}
&:hover {
background: #f8fafc;
}
&:focus-within {
outline: 2px solid #0f766e;
outline-offset: 2px;
}
&.is-open {
border-color: #0f766e;
}
@media (max-width: 36rem) {
flex-direction: column;
}
}
Expected Output
A horizontal card whose heading, paragraph, and image rules apply only inside .card. Hover lightens the background, focus inside the card draws a teal outline, and .is-open turns the border teal. At 36rem or narrower the card stacks in a column.
Line-by-Line Walkthrough
- .card sets the flex row, gap, padding, border, and background for the whole component.
- & h2 nests a descendant heading. The expanded selector is .card h2, so other headings on the page keep their margins.
- & p does the same for the paragraph inside the card.
- & img sizes the thumbnail and crops it with object-fit: cover inside a fixed frame.
- &:hover is the compound .card:hover, not a descendant named hover. The background changes when the pointer is over the card.
- &:focus-within draws an outline when keyboard focus is anywhere inside the card.
- &.is-open matches a card that has the class is-open. A space before the class would have matched a child instead.
- The nested @media rule applies flex-direction: column to .card only when the viewport is at most 36rem wide.
- Each nested selector includes the parent specificity. A parent list of .card, #hero would make the nested rule as strong as #hero.
Common Pitfalls & Security
The ampersand is the parent selector, not a general prefix. Where you put a space changes a compound into a descendant, and the specificity of the parent is copied into every nested rule.
- &.is-open matches the card that has the class. & .is-open matches a descendant with that class. The space is the entire difference.
- Nesting under a selector list that includes an ID makes every nested rule carry that ID specificity, which utilities then struggle to override.
- Deep nesting recreates long descendant chains. Two levels is usually enough. A third level is a sign the component should be split.
- A nested media query styles the parent. It does not automatically wrap the nested child rules unless those children are inside the query too.
- &:hover on a large card can flash on touch devices. Pair it with a style that does not hide required information.
- Native nesting is available in current browsers. A build step is optional. Do not also run the same file through a nesting compiler or the ampersand can be rewritten twice.
Best Practices & Modern Standards
Keep each component in one rule. Use & with a descendant combinator for children, &:hover and &:focus-within for states, and &.modifier for a class on the same element. Put the narrow-viewport switch in a nested media query on the parent.
Prefer a class parent over an ID parent so nested rules stay easy to override. If a nested rule must be weaker than the parent list, wrap the parent with :where() so the nesting selector does not copy the strongest specificity in that list.
Real-World Use
The catalog card is one nested block: the row layout, the thumbnail crop, the heading reset, the hover fill, and the open border. Editors add is-open when the card is the current course. On a narrow phone the same block switches the flex direction to a column, without a second selector written elsewhere in the file.
Frequently Asked Questions
What does the ampersand stand for?
It stands for the parent selector. & h2 becomes a descendant, and &:hover becomes the parent in the hover state.
How is a hover state written with nesting?
Write &:hover inside the parent rule. That is .card:hover when the parent is .card. It is not a child element.
Does nesting change specificity?
The nested selector includes the parent. If the parent list contains an ID, the nested rule is as specific as that ID plus the rest of the selector.
Can a media query be nested?
Yes. Declarations inside a media query nested in .card apply to .card when the query matches. Child rules stay where you nest them.