For the longest time, media queries were the only tool we had for responsive design. And honestly? They worked. We built entire careers mastering @media (min-width: 768px) and memorizing breakpoint values. But recently, I stumbled into container queries, and I can't help but feel like I've been doing responsive design with one hand tied behind my back.
Let me take you through what I learned—and why I think container queries are the future of responsive components.
The Media Query Problem I Didn't Know I Had
Media queries have served us well. They let us adapt layouts based on viewport size, and for page-level responsiveness, they're still great. But here's the thing I started noticing: I was writing responsive styles based on where I thought a component would live, not based on how much space it actually had.
Consider a card component. With media queries, you might write something like this:
.card {
display: block;
}
@media (min-width: 768px) {
.card {
display: flex;
}
}
This works perfectly... until you put that card in a narrow sidebar on a wide screen. Suddenly your horizontal card layout is crammed into a 300px container on a 1920px viewport. The viewport is "large," but your component doesn't have the space it needs.
I used to solve this with additional modifier classes like .card--sidebar or wrapper-specific overrides. But every time I needed to reuse the component in a new context, I'd have to audit all the media queries and add more exceptions. It felt wrong, but I didn't know there was a better way.
Enter Container Queries
Container queries flip the whole model on its head. Instead of asking "how wide is the viewport?", you ask "how wide is my container?"
Here's the same card component with container queries:
.card-wrapper {
container-type: inline-size;
}
.card {
display: block;
}
@container (min-width: 500px) {
.card {
display: flex;
}
}
Now the card adapts based on its actual available space, not the viewport width. Put it in a wide main column? It flexes horizontally. Put it in a narrow sidebar? It stacks vertically. Same component, zero modifications, no conditional classes.
The first time I saw this work, I actually said "wait, what?" out loud at my desk.
The Mental Shift: Components That Own Their Responsiveness
The breakthrough moment for me was realizing that container queries make components truly encapsulated and reusable.
With media queries, responsive behavior is global—every component on the page responds to the same viewport breakpoints. This creates an invisible coupling between your layout and your components. You have to know where a component will be used to style it correctly.
With container queries, components own their responsive behavior. A card component doesn't need to know if it's in a sidebar, a grid, or a full-width layout. It just needs to know: "I have X pixels of space, so I'll render myself accordingly."
This is huge for design systems and component libraries. I can build a component once, and it adapts intelligently regardless of context. No more "mobile-card", "tablet-card", "desktop-card" variants. Just one card that responds to its container.
When I Still Reach for Media Queries
Here's the thing though—media queries aren't obsolete. After playing with container queries for a few months, I've learned they solve different problems.
Use media queries for:
- Page-level layout shifts (sidebar appearing/disappearing, navigation changes)
- Typography scaling across the entire site
- Global spacing and padding adjustments
- Anything that truly depends on screen size (like touch vs. mouse interactions)
Use container queries for:
- Component-level responsive behavior
- Cards, widgets, or modules that appear in varying contexts
- Design system components that need to be truly reusable
- Any time you've written CSS like
.component--in-sidebar
In practice, I often use both in the same project. Media queries handle the macro layout, and container queries handle how components adapt within that layout.
The Code That Made It Click
Let me show you the example that made this all crystallize for me. Imagine a product grid where items can be 1, 2, or 3 columns wide depending on the layout:
/* Container setup */
.product-item {
container-type: inline-size;
}
/* Stacked layout (narrow containers) */
.product {
display: flex;
flex-direction: column;
gap: 1rem;
}
.product__image {
aspect-ratio: 1;
}
/* Horizontal layout (medium containers) */
@container (min-width: 400px) {
.product {
flex-direction: row;
}
.product__image {
aspect-ratio: 4/3;
width: 40%;
}
}
/* Complex layout (large containers) */
@container (min-width: 600px) {
.product {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-rows: auto 1fr auto;
}
.product__image {
grid-row: 1 / -1;
}
}
The same product component now works perfectly whether it's:
- In a single-column mobile layout (stacked)
- In a 2-column tablet grid (horizontal)
- In a 3-column desktop grid (still horizontal)
- Spanning 2 columns in the desktop grid (complex layout)
All without a single media query or modifier class. The component just... adapts.
Browser Support and Using Them Today
The best part? Browser support is actually really good now. All major browsers support container queries as of 2023. I was worried about fallbacks, but the progressive enhancement story is simple:
/* Default mobile-first styles (work everywhere) */
.card {
display: block;
}
/* Container queries for browsers that support them */
@container (min-width: 500px) {
.card {
display: flex;
}
}
Older browsers get the stacked layout. Modern browsers get the adaptive layout. No polyfills needed, no build-tool complexity—just CSS.
Why This Feels Like the Future
After working with container queries for a few months, going back to pure media queries feels limiting. It's like the difference between absolute and relative positioning—once you understand the contextual approach, you can't unsee how much more flexible it is.
Container queries enable:
- True component reusability without context-specific overrides
- Cleaner, more maintainable CSS with fewer breakpoint-specific rules
- Better design system architecture where components are genuinely portable
- More intuitive responsive behavior that matches how we think about layout
Media queries taught us to think "mobile, tablet, desktop." Container queries teach us to think "narrow space, medium space, wide space"—regardless of device. That's a fundamental shift, and I believe it's the right direction.
My Recommendation
If you're building new components today, start with container queries. Think about how your component should adapt to its available space, not the viewport size. You'll write less CSS, create more reusable components, and build interfaces that adapt more intelligently.
Keep media queries around for page-level concerns, but let container queries handle your component responsiveness. After a few months of working this way, I can't imagine going back.
The future of responsive design isn't about viewport breakpoints—it's about components that adapt to their context. And that future is already here.