Native nesting
Definition and purpose
Native CSS nesting lets a style rule contain other style rules. The browser parses nested selectors relative to the parent selector, rather than using a text substitution preprocessor.
Nested descendant
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } .card { .label { color: green; } } </style> </head> <body> <section class="card"> <p id="a" class="label">Label</p> <p id="b">Detail</p> </section> <p id="c" class="label">Outside</p> </body></html>Only the descendant matches.
Syntax and mental model
Inside .card, a nested .label rule selects descendants, while &.active selects the card itself when active. A parent selector list gives & the maximum specificity of the list, like :is(). A flat comma-separated expansion can therefore match the same elements but produce different cascade results. Interleaved declarations retain their order in current nesting implementations.
List specificity
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } .card, #unused { & .label { color: navy; } } .card .label { color: red; } </style> </head> <body> <section class="card"> <p id="a" class="label">Label</p> <p id="b">Detail</p> </section> <p id="c" class="label">Outside</p> </body></html>The unused ID still contributes to nesting specificity.
Common mistakes
Do not use &-label to concatenate a class name as in some preprocessors. Use an explicit class selector. Be careful with an ID in a parent selector list.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Scope and proximity. Next lesson: Value resolution stages.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Native nesting — knowledge check
Five questions on native nesting.
What does &.active match inside .card?
What does nested .label mean?
Which specificity does & take for a parent list?
Can &-label concatenate a class in native CSS?
Does a flat list always preserve nesting specificity?