
Container Queries: Component-First Responsive Design
Viewport media queries cannot tell a reusable component how wide its slot is. Size container queries (Baseline 2023) finally let components answer that question themselves.
TL;DR: Viewport media queries answer the wrong question for any component placed in more than one layout slot. Size container queries — Baseline in Chrome 105+, Safari 16+, and Firefox 110+ since 2023 — let a component ask how wide its slot is and adapt accordingly. The migration follows a repeatable set of steps: wrap the component in a container-type: inline-size parent, swap @media (min-width: ...) for @container (inline-size >= ...), and keep @media for the global concerns it still owns (theme, motion, pointer, hover).
The problem viewport queries cannot solve
A card component lives in two slots on the same page. One is a 280 CSS px sidebar; the other is an 800 CSS px main column. Both slots render at the same viewport width, so a viewport-only @media query has no way to give them different layouts. The sidebar card needs to stack its meta and body vertically. The main column card has room to put them side by side. With viewport queries alone, the workarounds are a second component or slot-specific override selectors (for example .sidebar .card), and each has to be kept in step with every future change.
The mental shift is small but consequential. Viewport queries answer "how wide is the window?" Container queries answer "how wide is my slot?" The first question is correct for page-level layout decisions; the second is the question a reusable component placed in more than one slot needs to ask. Most existing stylesheets were written for the first question: the Web Almanac 2022 CSS chapter found max-width media queries on 83% of pages and min-width on 79%, measured before size container queries reached Baseline in 2023.
Declaring a container
Two declarations turn an element into a query container, and only the first is required.
.card-wrapper {
container-type: inline-size;
container-name: card;
}container-type: inline-size makes the element a query container along its inline axis (usually width in left-to-right scripts). This is the form to start with for components that adapt to their slot's width — it lets descendants query the wrapper's width without imposing block-size containment, which can interfere with intrinsic sizing in ways that surprise you.
container-type: size adds block-axis queries but requires full containment on both axes, which means the container ignores its intrinsic content height. Reach for it only when you genuinely need to ask "how tall is my slot?"
container-name is optional. Without a name, an @container query uses the nearest ancestor that is a query container for the features it tests — an inline-size query, for example, can use either a size or an inline-size container. Named containers let you target a specific level even when query containers nest, which is useful for design systems where a card might live inside a section that is also a container.
Browser support for size container queries reached Baseline 2023 — Chrome 105+, Safari 16+, and Firefox 110+. A container query polyfill from Google Chrome Labs exists, but its README states it has been in maintenance mode since November 2022 and lists real limits (no Shadow DOM support, no calc() in container conditions, only <style> and same-origin <link> stylesheets). The simpler fallback is to let the @container block not apply in older browsers, which is a graceful degradation if your base styles already work at the smallest slot size.
Writing @container queries
The query syntax mirrors @media but operates against the container.
.card-body {
display: block;
}
@container (inline-size > 30em) {
.card-body {
display: grid;
grid-template-columns: auto 1fr;
gap: 1rem;
}
}
@container card (inline-size > 48em) {
.card-body {
grid-template-columns: 1fr 2fr;
padding: 2rem;
}
}Three things to notice. First, range syntax works the same way it does in @media: @container (40em <= inline-size < 60em) is valid and clearer than the min-width / max-width pair it replaces. Second, you can stack multiple @container blocks for a single component, which is how the same card can have a stacked layout below 30em, a two-column layout between 30em and 48em, and a feature-row layout above 48em. Third, the named-container form @container card (...) targets only the container named card, which prevents accidental matches when components nest. The W3C CSS Conditional Rules Module Level 5 (the specification that defines container queries; a Working Draft as of Nov 2026) covers the full grammar, including style queries, and the MDN guide to container size queries walks through size queries, container names, and fallbacks.
Container query units
Container query units scale to the container, not the viewport.
| Unit | Meaning | Typical use |
|---|---|---|
| `cqi` | 1% of container inline size (usually width) | Font size, padding inside a component |
| `cqb` | 1% of container block size (usually height) | Vertical rhythm in size-typed containers |
| `cqmin` | The smaller of `cqi` and `cqb` | Square components where either axis can constrain |
| `cqmax` | The larger of `cqi` and `cqb` | Components that should track the dominant axis |
In an inline-size container, cqi tracks the slot's inline size (usually its width). Anchor a clamp() to cqi rather than vw and your typography scales with the card the user is reading, not with their monitor. The pattern looks like this:
.card-title {
font-size: clamp(1rem, 0.7rem + 2.5cqi, 1.6rem);
}In a 280 CSS px sidebar slot, the title comes out near the floor. In an 800 CSS px column slot, the same declaration produces a title near the ceiling — same component, two correct sizes, zero breakpoints written. The MDN container queries guide documents the unit set in full.
Migration pattern: from viewport to container
The migration from @media to @container follows a repeatable sequence of steps. The steps stay the same from one design system to the next.
Step one: identify components that live in variable-width slots. Cards, media objects, profile blocks, and form rows are the usual suspects. A component that always renders at the full viewport width — a top-level hero, a footer — does not need this migration.
Step two: wrap the component output in a container-type: inline-size parent. In a React or Vue codebase, this often lives in the component's root element; in a server-rendered CMS, it may need an extra wrapper around the slot.
Step three: convert component-local @media (min-width: ...) to @container (inline-size >= ...). Carry the old thresholds over as starting values, then re-tune them. A 480px viewport breakpoint converts to 30em (480 divided by the 16px default) only when the container's font-size is 16px: in @container conditions, em resolves against the query container's computed font-size, not the initial font-size that @media uses. The old value also measured the viewport, and the slot is narrower than the viewport by page gutters and neighboring columns. The width at which the component actually breaks inside its slot is therefore usually smaller than the old viewport value; drag-test each slot to find it. Verify the actual rendering in each layout slot, not just in the Storybook view that places the component at full width.
Step four: keep page-level @media for genuinely viewport-scoped concerns. The page grid, the layout of header and footer relative to the viewport, and any decision that depends on the user agent rather than the slot still belongs in @media.
Step five: document the rule for your design system. The shorthand is short: components ask the container, pages ask the viewport.
When @media still wins
Container queries do not deprecate media queries. The two answer different questions, and a healthy stylesheet uses both.
Media queries still own user-agent and global concerns. prefers-color-scheme for theme switching. prefers-reduced-motion for animation gating. pointer: fine versus pointer: coarse for tap target sizing. hover: hover for revealing hover-only affordances safely. orientation: portrait for viewport-relative layout decisions. None of these belong to a single slot — they describe the user agent or the user's preferences, which is exactly what @media is for.
Media queries also still own the page-level grid. A header, main, and footer arrangement that responds to the viewport's width is correctly written in @media (width >= 64em). Trying to express the page grid in container queries can introduce a wrapper that exists only to be queried, and it inverts the model — the page is not a component, and the viewport really is the right unit for it.
A simple heuristic: if the question is about the user, the agent, or the page, use @media. If the question is about a slot a reusable component is sitting in, use @container.
Phase 1 deliverables and acceptance criteria
Pick one reusable component and run the migration end to end so the rest of the design system has a worked example.
- Find one reusable component placed in two or more layout slots — card, media object, sidebar widget, or profile block are good first candidates.
- Wrap the component output in a
container-type: inline-sizeparent. Confirm no layout regression with@containerblocks empty. - Convert the component's local viewport
@mediaqueries to@containerqueries. Carry thresholds over as starting values, then re-tune them per slot. - Verify in every real layout slot — sidebar, modal, card grid, main column — not just the full-width Storybook view.
- Document the rule for the design system: components ask the container, pages ask the viewport.
The component you migrate first will look much like every component you migrate after. Container queries are not a different CSS — they are the question reusable components were always trying to ask.