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

Container Query Breakpoints

Defining container-based breakpoints that allow components to adapt their layout at specific container widths.

Container Query Breakpoints

You'll learn how to set specific width thresholds where your components change their layout based on their container's size, not the whole viewport.

What Are Container Query Breakpoints?

Just like media queries have breakpoints (like 768px for tablets), container queries let you define breakpoints at the container level. Instead of asking "Is the browser window this wide?", you're asking "Is this specific container this wide?"

Think of it like a cardboard box that rearranges its contents differently depending on whether it's small, medium, or large — regardless of the room size it's sitting in.

Defining Container-Based Breakpoints

Once you've established a container context (with container-type: inline-size), you can write breakpoints using @container rules:

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

/* Mobile-style card when container is narrow */
.card {
  display: block;
}

/* Breakpoint at 400px container width */
@container card (min-width: 400px) {
  .card {
    display: flex;
    gap: 1rem;
  }
}

/* Another breakpoint at 600px container width */
@container card (min-width: 600px) {
  .card {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }
}

The .card component now adapts at 400px and 600px of its container's width, not the viewport. If that container appears in a sidebar (narrow) or main content area (wide), the card automatically uses the appropriate layout.

Why This Matters

This approach makes components truly modular and reusable. The same card component works perfectly whether it's in a tight space or spread across a full page — it responds to its environment, not the global viewport.

Key Takeaway: Container query breakpoints let you define width thresholds where components change layout based on their container's size, making truly adaptive, context-aware components.