What Are CSS Container Queries?
For years, responsive web design relied almost entirely on media queries — rules that respond to the size of the browser viewport. But viewport-based styling has a fundamental limitation: it can't account for the size of a component's parent container. Enter CSS Container Queries, a game-changing addition to the CSS specification that lets elements respond to the size of their containing element rather than the whole screen.
Why Media Queries Aren't Always Enough
Imagine you have a card component used in both a narrow sidebar and a wide main content area. With media queries alone, you'd have to write complex overrides based on viewport breakpoints — even though what you really care about is how much space the card's parent gives it. Container queries solve this elegantly.
- Reusable components can adapt to their own context, not global screen size.
- Design systems become more portable and predictable.
- Nested layouts no longer require fragile viewport hacks.
How to Use Container Queries
Using container queries involves two steps: defining a containment context and then writing query rules against it.
Step 1: Define a Container
.card-wrapper {
container-type: inline-size;
container-name: card;
}
The container-type: inline-size declaration tells the browser this element is a containment context for its children, measuring along the inline (horizontal) axis.
Step 2: Write the Container Query
@container card (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 1fr 2fr;
}
}
Now the card shifts to a two-column grid layout whenever its container is at least 400px wide — regardless of the viewport.
Container Query Units
Along with container queries comes a set of new CSS units relative to the container's dimensions:
| Unit | Relative To |
|---|---|
cqw | 1% of container width |
cqh | 1% of container height |
cqi | 1% of container inline size |
cqb | 1% of container block size |
Browser Support
Container queries now enjoy broad baseline support across all major browsers — Chrome, Firefox, Safari, and Edge all support them as of their recent versions. You can safely use them in production today, optionally providing a simple fallback layout for older environments.
Best Practices
- Name your containers explicitly with
container-nameto avoid ambiguity in nested layouts. - Use container queries for component-level responsiveness; keep media queries for page-level layout shifts.
- Avoid over-engineering — not every component needs container query logic.
- Test components in isolation using tools like Storybook to verify container-responsive behavior.
The Bigger Picture
Container queries represent a philosophical shift in how we think about responsive design. Instead of designing pages that respond to a screen, we're now designing components that respond to their environment. This brings CSS closer to the component-driven model that JavaScript frameworks like React and Vue have popularized for years. The web platform is catching up — and it's a great time to be a front-end developer.