What Are Container Queries?
You'll learn: How container queries enable components to adapt based on their container's size instead of the entire viewport.
The Container Problem with Media Queries
Up until now, you've been using media queries to make your layouts responsive. But media queries have a limitation: they only respond to the viewport size (the browser window). This creates a problem when you want a component to look different based on how much space it has, not how big the screen is.
What Are Container Queries?
Container queries let you style an element based on the size of its parent container, not the viewport. Think of it like this: instead of asking "How wide is the browser window?", you're asking "How wide is this specific box I'm sitting inside?"
A Real-World Analogy
Imagine you're packing suitcases of different sizes. With media queries, you'd choose your outfits based on the size of your room. But what if you have three different suitcases to pack? Container queries let you choose outfits based on each individual suitcase's capacity instead.
Why This Matters
Container queries make truly reusable components possible. A card component can automatically adjust its layout whether it's in a narrow sidebar, a medium-width column, or stretched across a full-width hero section—all without knowing anything about the overall page layout.
This is component-based responsive design: your components respond to their own space, making them flexible and portable across different contexts.
Key Takeaway: Container queries shift responsive design from viewport-based to container-based, allowing components to adapt to their parent's size rather than the screen size.