Container queries
Overview
Media queries ask “how wide is the screen?”. But a component doesn’t care about the screen — it cares about the space it’s been given. The same course card might sit in a wide main column, a narrow sidebar and a three-column grid on the same page. With media queries, it would get the same layout in all three.
Container queries let an element respond to the size of its container. Write the component once; it adapts wherever you put it.
Size container queries have been supported in all major browsers since early 2023 (Chrome 105, Safari 16, Firefox 110).
Two steps
1. Make an element a container
.card-wrap {
container-type: inline-size;
}This tells the browser to track the element’s inline size (width, in English) so descendants can query it. It’s usually a wrapper around the component, or a layout region like the sidebar.
2. Query it
.card { display: grid; gap: 1rem; }
@container (width >= 30rem) {
.card { grid-template-columns: 2fr 3fr; }
}The query checks the nearest ancestor that’s a container. When that container is at least 30rem wide, the rules apply.
You can’t style the container itself
A container query styles the container’s descendants, never the container. Otherwise changing the container’s styles could change its size, which would change the query result — an infinite loop. That’s why you usually add a wrapper element, or make the layout region (the grid cell, the sidebar) the container.
container-type values
| Value | Tracks | Notes |
|---|---|---|
inline-size |
width (inline axis) | what you want 99% of the time |
size |
width and height | the element must have an explicit height — its contents can no longer size it |
normal |
nothing for size | default; still usable for style queries |
container-type: inline-size applies inline-size containment: the element’s width can no longer depend on its children. Block-level elements, grid items and flex items with a set basis work fine. But a container that would normally shrink to fit its content (a float, an inline-block, or a flex item with flex: none) will collapse to zero width, because its content can’t determine its size. If a container suddenly disappears, this is why.
Naming containers
With nested containers, a query checks the nearest one. To target a specific container, name it:
.sidebar { container: sidebar / inline-size; } /* shorthand: name / type */
.card-wrap { container: card / inline-size; }
@container sidebar (width < 20rem) {
.nav-label { display: none; }
}
@container card (width >= 30rem) {
.card { grid-template-columns: 2fr 3fr; }
}Query syntax
The syntax matches media queries, including range syntax and and, or and not:
@container (width >= 30rem) { … }
@container (20rem <= width < 40rem) { … }
@container (orientation: landscape) { … } /* needs container-type: size */
@container card (width >= 30rem) and (width < 50rem) { … }Container query units
Inside a container, these units are percentages of the container’s size:
| Unit | 1% of the container’s… |
|---|---|
cqi |
inline size |
cqb |
block size |
cqw / cqh |
width / height |
cqmin / cqmax |
smaller / larger dimension |
They’re perfect for fluid type inside components:
.card h3 {
font-size: clamp(1.1rem, 4cqi, 2rem); /* scales with the card, not the screen */
}If no ancestor is a container, the units fall back to the small viewport units (svw, svh).
Media queries or container queries?
| Use media queries for | Use container queries for |
|---|---|
| page-level layout (number of columns, sidebar or not) | components: cards, media objects, forms, widgets |
| navigation style (hamburger or full bar) | anything reused in different-sized places |
| user preferences (motion, colour scheme) | fluid type inside a component (cqi) |
| viewport height concerns | design systems and component libraries |
Most sites use both: media queries arrange the page; container queries make the pieces adapt to the space the page gives them.
Style queries
Container style queries test the value of a custom property on the container:
.card-wrap { --variant: featured; }
@container style(--variant: featured) {
.card { border: 2px solid gold; }
}They’re useful for theming and variants without extra classes. Support is newer than size queries: Chrome since 2023, Safari since 2024 and Firefox since 2026 — check your audience’s browsers before relying on them, and make the unstyled version acceptable.
The mindset shift: design components that adapt to their space, and let the page decide how much space each one gets. That’s what makes a component reusable.
One card, three places
The same .card markup appears in a narrow .sidebar (16rem) and a wide .main area. Make each .card-wrap a size container. By default the card stacks (image above text). When its container is at least 28rem, show the image and text side by side (image 40% wide). When it’s at least 45rem, also make the title bigger. Size the title with cqi units clamped between 1.1rem and 2rem. Check that the sidebar card stacks and the main card is side by side at the same viewport width.
<div class="page">
<aside class="sidebar">
<div class="card-wrap"><article class="card"><img alt="" /><div><h3>Container queries</h3><p>Components that adapt.</p></div></article></div>
</aside>
<main class="main">
<div class="card-wrap"><article class="card"><img alt="" /><div><h3>Container queries</h3><p>Components that adapt.</p></div></article></div>
</main>
</div>
<style>
* { box-sizing: border-box; }
.page { display: grid; grid-template-columns: 16rem 1fr; gap: 1rem; }
.card { border: 1px solid #ccc; }
.card img { display: block; inline-size: 100%; aspect-ratio: 16 / 9; background: #c9d8f8; }
.card h3 { margin: 0.5rem 0; }
/* your code here */
</style><div class="page">
<aside class="sidebar">
<div class="card-wrap"><article class="card"><img alt="" /><div><h3>Container queries</h3><p>Components that adapt.</p></div></article></div>
</aside>
<main class="main">
<div class="card-wrap"><article class="card"><img alt="" /><div><h3>Container queries</h3><p>Components that adapt.</p></div></article></div>
</main>
</div>
<style>
* { box-sizing: border-box; }
.page { display: grid; grid-template-columns: 16rem 1fr; gap: 1rem; }
.card { border: 1px solid #ccc; }
.card img { display: block; inline-size: 100%; aspect-ratio: 16 / 9; background: #c9d8f8; }
.card h3 { margin: 0.5rem 0; }
.card-wrap {
container: card / inline-size; /* name / type */
}
.card {
display: grid;
gap: 1rem;
}
.card h3 {
font-size: clamp(1.1rem, 4cqi, 2rem);
}
@container card (width >= 28rem) {
.card {
grid-template-columns: 40% 1fr;
align-items: start;
}
}
@container card (width >= 45rem) {
.card h3 { font-size: 2rem; }
}
</style>Summary
- Container queries let a component respond to its container’s size, so it works anywhere you place it.
- Mark the container with
container-type: inline-size(or thecontainershorthand with a name). @container (width >= 30rem) { … }styles the container’s descendants, never the container itself.- Container query units (
cqi,cqb,cqmin) are percentages of the container — great for fluid type in components. - Size queries are supported in all major browsers since 2023; style queries are newer.