For years, responsive design meant responding to the viewport width with media queries. Container queries change that — components can now respond to the size of their own container.
The problem with media queries
Imagine a card component used in a three-column sidebar and a full-width content area. With media queries, both instances respond to the same viewport width, making it impossible to style them independently.
/* This applies to ALL .card elements at this viewport width */
@media (min-width: 768px) {
.card { flex-direction: row; }
}
Container queries to the rescue
.card-wrapper {
container-type: inline-size;
}
@container (min-width: 400px) {
.card { flex-direction: row; }
}
Now the card layout responds to its wrapper's width — not the viewport. The same card in a narrow sidebar stays vertical while the same card in a wide content area goes horizontal.
Browser support
Container queries are now baseline available in all modern browsers (Chrome 105+, Firefox 110+, Safari 16+). You can use them in production without polyfills.
Named containers
For nested layouts, you can name containers:
.sidebar { container: sidebar / inline-size; }
.main { container: main / inline-size; }
@container sidebar (min-width: 200px) { ... }
@container main (min-width: 600px) { ... }
When to use them
Container queries are ideal for:
- Design system components that need to work in any context
- Card components used in varying grid widths
- Any component that is "layout-agnostic"