Course contentsShow
← Web Development
Lesson 438 of 2,9827. Responsive DesignFree lesson

Nested Container Queries

Implementing container queries within container queries for complex, multi-level responsive component behavior.

Nested Container Queries

You'll learn how to apply container queries within other container-queried components for sophisticated, context-aware layouts.

The Core Idea

Nested container queries let you place one container-query-responsive component inside another one. Each container responds to its own size independently, creating a hierarchy of adaptive behavior. Think of Russian nesting dolls: each doll (container) knows its own size and transforms accordingly, regardless of what's happening in the outer or inner dolls.

This is powerful because real interfaces aren't flat. A card component might live inside a sidebar, which itself responds to page width. With nested container queries, the card adapts to the sidebar's size, while the sidebar adapts to the page—all without global media queries.

How It Works

You set up multiple container-type contexts at different levels:

.sidebar {
  container-type: inline-size;
  container-name: sidebar;
}

.card {
  container-type: inline-size;
  container-name: card;
}

@container sidebar (min-width: 300px) {
  .sidebar {
    padding: 2rem;
  }
}

@container card (min-width: 200px) {
  .card-title {
    font-size: 1.5rem;
  }
}

The .card inside .sidebar responds to its width (the card's container), while the sidebar responds to its width. They operate independently but hierarchically—the card doesn't "see" the outer page, only its immediate container.

When to Use This

Anytime you have reusable components that might appear in varying contexts—a card in a main column versus a narrow sidebar, or a button group in different-sized panels.

Key Takeaway: Nested container queries enable each component layer to respond to its own size independently, creating truly modular, context-aware responsive design.