Container Queries: Size Containers, Container Names and Container Units
Components that adapt to their space: container-type and size containment, @container rules, named containers, the units cqi and cqb, why an element can't query itself, the zero-width trap in flex and grid, and container versus media queries.
kvi5hnuCode Sprout team member7 min readOn this page
Media queries ask about the viewport. But a component doesn't know where it will be placed: the same card might sit in a narrow sidebar, in a three-column grid, or across a wide main column, all on the same screen. Container queries let a component ask about the size of its container instead, so it can adapt wherever it's used.

The two parts
- Make an element a query container with
container-type. - Style its descendants with an
@containerrule.
.card-slot { container-type: inline-size; }
.card { display: grid; gap: 0.75rem; }
@container (width >= 25rem) {
.card { grid-template-columns: 9rem 1fr; }
}
Every .card inside a .card-slot that's at least 25rem wide gets two columns; the others stay stacked. The syntax inside @container is the same as for media queries: features in parentheses, the range syntax, and, or and not (article 76).
container-type
| Value | Queries allowed | Containment applied |
|---|---|---|
normal (initial) | style queries only (article 79) | none |
inline-size | width / inline-size | size containment on the inline axis |
size | width, height, aspect-ratio, orientation | size containment on both axes |
Use inline-size almost always. The reason is size containment: to avoid a loop where a query changes the content, which changes the container's size, which changes the query, the container's size along the contained axis is calculated as if it had no content.
- With
inline-size, the width can't depend on the content. That's fine for block-level elements, whose width comes from their parent. - With
size, the height can't either, so a container without an explicit height collapses to 0. Tested in Chrome: adivwithcontainer-type: sizeand a paragraph inside is 0px tall.
The zero-width trap
Size containment on the inline axis also affects elements whose width normally comes from their content: floats, inline-blocks, absolutely positioned elements, and flex or grid items with an auto size. Tested in Chrome: a flex item with container-type: inline-size and some text is 0px wide.
The fix is to give such containers a width that doesn't depend on content: flex: 1, a grid track like 1fr, or an explicit inline-size. Or put container-type on a wrapper whose width is determined from outside, such as a grid cell's child that stretches.
An element can't query itself
A container query styles the container's descendants, never the container itself. Tested in Chrome: an element that is a 400px container, with a matching @container (width > 300px) rule for itself and for its child, only changes the child.

The measurements are from the render: size containment ignores the content, so containers whose size normally comes from their content collapse, and a matching query restyles only what's inside the container.
So the usual structure is a wrapper and a component:
<div class="card-slot"> <!-- container -->
<article class="card">…</article> <!-- styled by the query -->
</div>
Named containers
An unnamed @container rule queries the nearest ancestor container of a suitable type. Tested in Chrome: inside a 200px container nested in a 500px one, @container (width > 400px) doesn't match, because the 200px one is nearest.
To query a specific ancestor, name it:
.layout { container: layout / inline-size; } /* shorthand: name / type */
.sidebar { container-name: sidebar; container-type: inline-size; }
@container layout (width >= 60rem) { .card { … } }
@container sidebar (width < 20rem) { .card { … } }
container-name can hold several names separated by spaces. The container shorthand takes name / type; tested in Chrome, container: outer / inline-size sets both.
Container query units
Inside a container, these units are relative to the nearest query container's size:
| Unit | 1% of the container's |
|---|---|
cqi, cqb | inline size, block size |
cqw, cqh | width, height |
cqmin, cqmax | smaller or larger of the inline and block sizes |
Tested in Chrome: 10cqi in a 300px container is 30px. If there's no container, they fall back to the small viewport units (svi, svb, article 25): 2cqi with no container in an 800px viewport is 16px. cqb needs a container with block-axis containment: inside an inline-size container, it uses the nearest size container further up, or the small viewport. Tested in Chrome: 10cqb in an inline-size container inside a 200px-tall size container is 20px.
They're useful for fluid type and spacing inside a component (article 77):
.card h2 { font-size: clamp(1.1rem, 0.8rem + 3cqi, 1.75rem); }
.card { padding: clamp(0.75rem, 4cqi, 1.5rem); }
body { font: 0.8rem system-ui, sans-serif; margin: 1rem; display: grid; gap: 0.6rem; justify-items: start; }
.slot { inline-size: var(--w); container-type: inline-size; outline: 2px dashed #d8cfc0; }
.card { display: grid; gap: 0.5rem; padding: 0.5rem; border-radius: 8px; background: #f4efe6; }
.img { aspect-ratio: 16 / 9; border-radius: 6px; background: linear-gradient(135deg, #1e3a5f, #e07a5f); }
h3 { margin: 0; color: #8a3b12; font-size: clamp(0.85rem, 0.6rem + 3cqi, 1.1rem); }
p { margin: 0.2rem 0 0; }
@container (width >= 12rem) {
.card { grid-template-columns: 5rem 1fr; align-items: center; }
}
@container (width < 10rem) {
p { display: none; } /* the narrowest version drops the detail */
}
The three cards share the same HTML and CSS. The 9rem one hides its detail, the 14rem and 20rem ones put the image beside the text, and the heading grows with cqi.
Container queries or media queries?
| Use media queries for | Use container queries for |
|---|---|
| the overall page layout: columns, navigation, showing a sidebar | reusable components: cards, media objects, forms, widgets |
| user preferences: dark mode, reduced motion | anything that appears in containers of different widths |
| print styles | design systems, where components don't know their context |
They work together: media queries arrange the page, and container queries let each component fit the space the page gives it.
Performance and practice
- Containers are cheap, but not free of side effects:
container-typealso applies style containment, so CSS counters incremented inside a container don't carry on outside it (article 96 covers counters). - Put containers on layout regions (
main,aside, grid cells, slots), not on every element. - Write queries in
rem. Unlike media queries, container conditions use the container's computed values:remis the real root font size, andemis the container's own font size. Tested in Chrome: withhtml { font-size: 10px },(width > 25rem)means 250px, and in a container with a 40px font,(width > 20em)means 800px. - A component should look acceptable with no container query matching, as its base style.
What's current
- Size container queries and container units: all browsers since 2023 (Chrome 105, Firefox 110, Safari 16).
- Style queries for custom properties: Chrome 111, Safari 18 and Firefox 151 (article 79).
- Scroll-state queries: Chrome 133 (article 79).
Common questions
Why does my container have zero width?
It's a flex item, grid item, float or inline-block sized by its content, and size containment ignores the content. Give it flex: 1, a fixed track, or an explicit width, or move container-type to a wrapper.
Why does my container have zero height?
It uses container-type: size. Use inline-size unless you need to query height and can set one.
Why doesn't my @container rule style the container itself?
Container queries only apply to descendants. Add a wrapper.
Which container does an unnamed query use?
The nearest ancestor with a suitable container-type. Name containers to query a specific one.
Summary
container-type: inline-sizemakes a query container;@container (width >= …)styles its descendants.- Size containment means the container's size can't depend on its content: watch for zero-width flex items and zero-height
sizecontainers. - An element can't query itself; use a wrapper.
- Unnamed queries use the nearest container;
container: name / inline-sizeand@container name (…)target a specific one. cqi,cqband the other container units size things relative to the container, falling back to small viewport units.- Media queries for the page and preferences, container queries for components.
Practice questions
Write CSS so that .profile uses a row layout when its .slot wrapper is at least 30rem wide.
Answer
.slot { container-type: inline-size; }
.profile { display: flex; flex-direction: column; }
@container (width >= 30rem) {
.profile { flex-direction: row; }
}
A sidebar is a flex item with no flex value, and has container-type: inline-size. It now has no width. Why, and what's one fix?
Answer
Size containment makes its content-based width zero, and a flex item's auto basis is its content's width. Give it a width that doesn't come from its content, such as flex: 0 0 18rem or inline-size: 18rem.
Inside a 360px container, how wide is inline-size: 25cqi?
Answer
90px: 25% of the container's inline size.
Why doesn't this make .card change color when it is wider than 30rem?
.card { container-type: inline-size; }
@container (width > 30rem) { .card { background: gold; } }
Answer
An element can't be styled by a query on itself; the query only affects the container's descendants, and .card's nearest container is an ancestor. Make a wrapper the container, or style a child of .card.

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.