
Container Queries: Component-Level Responsive Design
A card in a sidebar and the same card in a main column should look different — but the viewport is identical. Container queries, supported across browsers since 2023, solve this.
TL;DR: Container queries became Baseline Newly available in February 2023 and Widely available in August 2025, removing the need for the prop-drilling and viewport-as-proxy workarounds common in 2010s component work. A card placed in a 400-pixel sidebar and the same card placed in an 800-pixel main column can now decide their own layout without parent props, without media queries, and without JavaScript. This guide covers the @container syntax, the container query unit family (cqi, cqb, cqw, cqh, cqmin, cqmax), the patterns container queries unlock, the newer style queries, and the migration plan for an existing design-system codebase.
Why viewport-only responsive design falls short
For layout, media queries mostly answer one question — how wide is the viewport — but a reusable component does not own the viewport. A card component placed in a 400-pixel sidebar and the same card placed in an 800-pixel main column should look different, but the viewport is identical. The information the component actually needs is the size of its own container, not the page.
The 2010s workaround was prop drilling. Components took a variant or isCompact prop, parents passed the right value down based on knowledge of where the child was rendered, and the result was a brittle contract between component and consumer that broke the moment a component was reused in a new context. Some design-system teams shipped separate components — a <CardCompact>, a <CardExpanded>, a <CardInline> — rather than fight the prop drilling, multiplying the surface area to maintain.
A second workaround was viewport-as-proxy. The team would establish breakpoints (mobile / tablet / desktop) and assume the layout role each component played at each breakpoint. This worked passably for products with stable layouts and broke down for products where the same component appeared in multiple layout contexts.
Container queries solve this directly. A component declares its own containment context, writes @container rules against that context, and decides its layout based on the size of an ancestor container — not the viewport. The contract becomes "I respond to the size of the box you put me in," and the parent does not need to know or care.
The @container syntax
Container queries require two steps: declare a containment context, then write rules against it.
The first step is container-type on the ancestor that should be queryable:
.card-grid {
container-type: inline-size;
}container-type: inline-size measures the container's inline dimension only — width in horizontal writing modes, height in vertical writing modes. This is the common choice; it is less invasive on layout than full size containment because it does not affect block-axis sizing.
container-type: size measures both inline and block dimensions, and requires the container to have a defined size on both axes. Use it only when the rules genuinely depend on height as well as width.
Writing the queries uses @container:
@container (inline-size > 600px) {
.card {
display: grid;
grid-template-columns: 200px 1fr;
gap: 1rem;
}
}The query targets the nearest ancestor with container-type set. When more than one ancestor has containment, name the relevant one with container-name:
.app-shell {
container-type: inline-size;
container-name: shell;
}
.card-region {
container-type: inline-size;
container-name: region;
}
@container region (inline-size > 600px) {
.card { /* responds to .card-region, not .app-shell */ }
}The shorthand container: shell / inline-size declares both the name and the type in one property. Names are scoped to the document; pick names that read well in queries.
Container query units
The cq* unit family lets components size their own internals against the container.
| Unit | Definition |
|---|---|
| `cqi` | 1% of the container's inline size (width in horizontal writing modes) |
| `cqb` | 1% of the container's block size (height in horizontal writing modes) |
| `cqw` | 1% of the container's width (physical, ignores writing mode) |
| `cqh` | 1% of the container's height (physical, ignores writing mode) |
| `cqmin` | The smaller of `cqi` and `cqb` |
| `cqmax` | The larger of `cqi` and `cqb` |
cqi is the unit you reach for most often. It is writing-mode-aware (it follows the inline axis, so it stays correct in vertical writing modes such as vertical Japanese), and it tracks the dimension that drives most component layout decisions — inline size.
A practical pattern is fluid sizing without calc() nesting:
.card {
padding: 4cqi;
font-size: clamp(1rem, 2cqi + 0.5rem, 1.5rem);
gap: 2cqi;
}The card's padding scales with its container's width, the font size has a clamped fluid response to the container, and the gap follows the same axis. None of this requires a JavaScript ResizeObserver, viewport breakpoints, or magic numbers.
cqb is useful when block size genuinely matters — for example, a hero with a vertical fluid type scale. cqmin and cqmax matter when the component should respond to the smaller or larger of the two axes (square-aware sizing, aspect-ratio-driven layouts).
The writing-mode awareness of cqi and cqb is load-bearing for multilingual sites. A 20-locale design system that uses cqw for inline padding will behave incorrectly in vertical Japanese; the same design system using cqi handles all writing modes correctly.
Patterns that container queries unlock
Five common patterns show where container queries pay off.
A card component switches from horizontal to vertical layout at a card-width threshold, regardless of which page or column the card lives in:
.card-grid {
container-type: inline-size;
}
.card {
display: flex;
flex-direction: column;
}
@container (inline-size > 480px) {
.card {
flex-direction: row;
}
}The card no longer needs a variant prop or a size boolean. The card responds to its container.
A sidebar-aware navigation compacts when its container narrows:
.nav {
container-type: inline-size;
}
.nav-label {
display: inline;
}
@container (inline-size < 200px) {
.nav-label { display: none; }
.nav-icon { font-size: 1.5em; }
}When the navigation moves into a narrow sidebar, labels disappear and icons grow. Move the navigation back into a wide column on a different page, and the labels reappear. No application logic involved.
Dashboard tiles get different chart layouts driven by the tile container, not the viewport:
.tile { container-type: inline-size; }
@container (inline-size > 400px) {
.tile-chart { aspect-ratio: 16 / 9; }
.tile-summary { display: grid; grid-template-columns: 1fr 1fr; }
}A tile dragged from a small grid cell to a large one redraws its chart correctly without the dashboard runtime knowing anything about the change.
A search or filter bar reflows its own controls — an instance in a narrow sidebar stacks the input above its buttons, an instance in a wide page header keeps them on one line, with the same component code. The dropdown or autocomplete panel that opens from that input is a different problem: container queries report the size of an ancestor container, not the space available around the anchor in the viewport, and a popover renders in the top layer. Fitting that panel to the available space is the job of CSS anchor positioning and its position-try fallbacks, not container queries.
Article layouts switch pull quotes, figures, and asides between inline and floated based on column width. A blog post in a single-column mobile view shows pull quotes inline; the same post in a multi-column desktop layout floats the same pull quotes alongside the body text.
Style queries — the newest addition
Style queries — @container style(--token: value) — let a child element react to a custom property set on an ancestor. They are distinct from size queries: size queries are about dimensions, style queries are about state propagation.
.theme-zone {
--density: comfortable;
}
.theme-zone[data-mode="compact"] {
--density: compact;
}
@container style(--density: compact) {
.button {
padding: 0.25rem 0.5rem;
font-size: 0.875rem;
}
}The theme-zone sets a token; descendants reading from a style query react without prop drilling and without specific class names. A parent component setting --theme: dark lets descendants restyle without a data-theme="dark" attribute or a context provider.
As of October 2026, style queries against custom properties are Baseline Newly available: Chromium shipped them in Chrome 111 (2023), Safari in Safari 18 (2024), and Firefox completed the set in Firefox 151 (May 2026), and the feature is an Interop 2026 focus area. Newly available means users on older versions still lack support, so treat style queries as enhancement and provide a fallback until your audience data says otherwise. The size-query story is settled; the style-query story is settling.
Browser support and fallback
Container queries (size queries) became Baseline Newly available in February 2023 and Widely available in August 2025 (web-features). Container query units shipped at the same time. The container query polyfill that helped during the Newly-available phase is no longer needed for production audiences in 2026.
For the small remaining tail — audiences on engines older than Chrome 105, Firefox 110 or Safari 16, which predate the Baseline Newly available date — feature-detect with @supports:
@supports (container-type: inline-size) {
.card-grid { container-type: inline-size; }
}
@supports not (container-type: inline-size) {
.card { /* single-column fallback layout */ }
}The fallback is "one column, no responsive switching." This is correct degradation; the component is usable on the rare engine that lacks support, just without the layout adaptation.
Style queries (@container style(...)) split by what is being queried. As of October 2026, queries against custom properties are Newly available (since May 2026), while queries against standard CSS properties, such as style(font-weight: bold), are defined in the specification but not implemented in any browser. Check Baseline before relying on style queries in load-bearing paths.
Common pitfalls
Five mistakes are easy to make.
Using container-type: size on an element with no defined block size collapses the container — size containment lays the box out as if it had no content, so its height drops to zero and queries such as (block-size > 300px) never match. If you need block-size queries, give the container an explicit height or use aspect-ratio to derive one.
Forgetting that container-type creates a containment context. Inline-size containment isolates the container from some layout calculations — min-content and max-content sizing of children no longer affect the container's intrinsic size in the affected axis. For most components this is invisible; for components that previously relied on intrinsic sizing, it is a layout regression to test for.
Nesting containers with the same container-name and querying an unintended ancestor. Container-name resolution finds the nearest matching ancestor; if you accidentally name two ancestors card, the inner card wins and the outer is unreachable. Use unique names and pick names that describe the role, not the component type.
Mixing media queries and container queries on the same component for the same breakpoint axis is confusing — the cascade resolves both, but readers cannot tell which one is active. Pick one source of truth per axis: media queries for page-level layout (which actually depends on viewport), container queries for component-level responsiveness.
Treating container queries as a 1-to-1 replacement for media queries. They are not. Container queries replace component-level breakpoints; viewport-level page layout (the overall column structure of the page) is still a media-query job. Both belong in a modern stylesheet.
What to refactor first
A focused refactor migrates one design-system primitive at a time and demonstrates the value before the full rollout.
Pick one component that has historical isCompact or variant props purely for size-driven branching. A card, a navigation, or a media object are the typical candidates. Replace the prop with a container-type: inline-size context on the parent and @container rules on the component. Delete the prop from the public API once consumers migrate.
Add cqi-based fluid spacing and typography to the component to remove magic-number breakpoints. The component scales smoothly with its container instead of switching abruptly at fixed thresholds.
Document the container-type contract in the component's README so consumers know the parent must establish containment for the component to respond. The contract is small and stable; documenting it is one paragraph, not a chapter.
Add a Storybook story that places the component in three container widths (300px, 600px, 900px) so visual regression tests cover the responsive behavior. Without the story, the responsive behavior is invisible to QA — and silently drifts when consumers change parent widths.
Run a visual regression sweep after the refactor. Container queries change the component's behavior at the boundary widths; the sweep catches the rare case where the new behavior diverges from the old prop-driven behavior in a way the design system did not intend.
Migration priorities for the next planning cycle
- Pick one design-system component with
isCompactor similar size-driven props and migrate it to container queries; delete the prop after consumers update - Add
cqi-based fluid padding and typography to the migrated component to remove magic-number breakpoints - Document the
container-typecontract in the component README; add a Storybook story at three container widths - Audit the design system for other components with size-driven props — list them, prioritize by usage, schedule the migration across upcoming planning cycles
- Add
@supports (container-type: inline-size)fallback rules for the small audience tail; do not ship the container-query polyfill in 2026 - If you are exploring style queries (
@container style(...)), keep their use behind feature detection while they are only Newly available (as of October 2026) and limited to custom properties