On this page9 sections
  1. Learning Objectives
  2. 1. Before and After
  3. 2. &: the Parent Selector
  4. 3. Nesting Media Queries
  5. 4. Don't Nest Too Deep
  6. Common Beginner Mistakes
  7. Practice Questions
  8. Practice Answers
  9. Key Takeaways

Lecture 028: CSS Nesting

Category6: CSS in Depth
Lecture028 of 057
Estimated video length8 min
TargetCSS3 · any modern browser
LevelIntermediate

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:

css
.card { padding: 16px; border: 1px solid #dee2e6; }
.card h3 { margin-top: 0; }
.card p { color: #495057; }

With nesting:

css
.card {
  padding: 16px;
  border: 1px solid #dee2e6;

  h3 { margin-top: 0; }
  p  { color: #495057; }
}

Both produce exactly the same result.

Example

html
<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

html
<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 writeIt 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:

html
<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

css
/* 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

  1. Rewrite nav a { color: white; } using nesting inside nav { }.
  2. What does & mean inside a nested rule?
  3. Inside .link { }, how do you write the hover state?
  4. Inside .card { }, how do you target .card.featured?
  5. Why avoid nesting five levels deep?

Practice Answers

  1. nav { a { color: white; } }
  2. The selector of the rule it's nested inside.
  3. &:hover { … }
  4. &.featured { … }
  5. 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.