On this page9 sections
Lecture 028: CSS Nesting
| Category | 6: CSS in Depth |
| Lecture | 028 of 057 |
| Estimated video length | 8 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
Nesting lets you write a rule inside another rule, so a component's styles live together in one block, just like its HTML. It used to need a preprocessor such as Sass; now browsers understand it directly.
Learning Objectives
- Nest child selectors inside a parent rule
- Use
&for states, modifiers and parent references - Nest media queries
- Avoid nesting too deeply
1. Before and After
Without nesting:
.card { padding: 16px; border: 1px solid #dee2e6; }
.card h3 { margin-top: 0; }
.card p { color: #495057; }With nesting:
.card {
padding: 16px;
border: 1px solid #dee2e6;
h3 { margin-top: 0; }
p { color: #495057; }
}Both produce exactly the same result.
Example
<style>
.card {
max-width: 280px;
padding: 16px;
border: 1px solid #dee2e6;
border-radius: 10px;
h3 { margin: 0 0 6px; color: #1971c2; }
p { margin: 0; color: #495057; }
}
</style>
<div class="card">
<h3>Nested CSS</h3>
<p>The heading and paragraph styles live inside the .card rule.</p>
</div>2. &: the Parent Selector
& stands for "the selector I'm nested in".
Example
<style>
.btn {
padding: 10px 18px; border: 0; border-radius: 6px; background: #1971c2; color: white; cursor: pointer;
&:hover { background: #1864ab; } /* .btn:hover */
&.danger { background: #e03131; } /* .btn.danger */
&.danger:hover { background: #c92a2a; }
.toolbar & { font-size: 13px; } /* .toolbar .btn */
}
</style>
<button class="btn">Save</button>
<button class="btn danger">Delete</button>
<div class="toolbar"><button class="btn">Small in a toolbar</button></div>| You write | It means |
|---|---|
&:hover | .btn:hover (no space: the same element) |
& span / span | .btn span (a descendant) |
&.danger | .btn.danger (both classes) |
.toolbar & | .toolbar .btn (.btn inside .toolbar) |
3. Nesting Media Queries
Example: drag the preview corner:
<style>
.banner {
padding: 14px;
background: #fff3bf;
font-size: 16px;
@media (min-width: 500px) {
background: #d3f9d8;
font-size: 22px;
}
}
</style>
<div class="banner">Resize me: wider screens get green and bigger text.</div>The responsive rules sit right next to the styles they change.
4. Don't Nest Too Deep
/* Too deep: very specific, and tied tightly to the HTML structure */
.page { .main { .content { .card { .title { span { color: red; } } } } } }Keep to two or three levels. Deep nesting creates long, very specific selectors that are hard to override.
Common Beginner Mistakes
1. Forgetting & for pseudo-classes. :hover { } nested inside .btn means .btn :hover (any hovered element inside it). Write &:hover.
2. Nesting everything. Shorter selectors are easier to reuse and override.
3. Old browsers. Nesting works in all current browsers; very old ones ignore the nested rules.
Practice Questions
- Rewrite
nav a { color: white; }using nesting insidenav { }. - What does
&mean inside a nested rule? - Inside
.link { }, how do you write the hover state? - Inside
.card { }, how do you target.card.featured? - Why avoid nesting five levels deep?
Practice Answers
nav { a { color: white; } }- The selector of the rule it's nested inside.
&:hover { … }&.featured { … }- It creates long, overly specific selectors that are hard to override and tied to the HTML structure.
Key Takeaways
- Nest child rules inside a parent to keep a component's styles together.
&= the parent selector:&:hover,&.modifier,.context &.- Media queries can be nested too.
- Keep nesting shallow.