CSS: nesting syntax
What is CSS nesting syntax
- CSS nesting is convenient syntax to let you nest CSS code, so the rules are grouped together for easier understanding and management.
- The nested code can always be written in a non-nested form. Nesting does not add anything new to CSS other than syntax.
Basic Nesting Example
/* nested syntax */ .card { background-color: pink; h2 { color: red; } p { color: blue; } } /* is equivalent to */ .card { background-color: pink; } .card h2 { color: red; } .card p { color: blue; }
Nesting syntax
by default, the relation of parent selector and its children have the same meaning as descendant selector.
.aa { .bb { color: red; } } /* is converted by CSS compiler to */ .aa .bb { color: red; }
you can override the relation
by prepending the combinator operator to the child selector, such as
>,
+,
~
.
.aa { > .bb { color: red; } } /* is converted by CSS compiler to */ .aa > .bb { color: red; }
nesting selector (ampersand &)
nested media query
browser support
supported by all major browsers since 2024.
It was introduced in 2019.
CSS nesting syntax
CSS. Selectors
Selector types
- CSS: types of selectors
- CSS: simple selectors
- CSS: compound selectors
- CSS: complex selector (combinator)
Simple selectors
- CSS: type selector (tag name)
- CSS: universal selector (* any tag)
- CSS: class selector (.x)
- CSS: ID selector (#)
- CSS: attribute selector ([x])
Combinators
- CSS: descendant selector (space)
- CSS: child selector (>)
- CSS: adjacent sibling selector (+)
- CSS: subsequent sibling selector (~)
Selector list
Nesting syntax
Special selector
- CSS: :root selector
- CSS: no child selector
- CSS: first child, sibling rank selector
- CSS: nth-child selector
- CSS: pseudo-class selector (:)
- CSS: pseudo-element selector (::)
- CSS: negation selector (:not)
- CSS: βis any ofβ selector (:is, :where)
- CSS: :has descendant selector
Misc
CSS. misc, advanced
- CSS: @media query (responsive design)
- CSS: Variable (Custom Property)
- CSS: calc
- CSS: Reset, Default Values
- CSS: global keywords (property values)
- CSS: nesting syntax
- CSS: Computed Style
- CSS: Browser Default Style Sheet (2025-12)