Queries that read the box
By the end you will know the one thing container queries can do that media queries cannot, why a query sometimes reads the wrong box or collapses it, and you will leave with a rule tuned to your own threshold and tested in your own browser.
The viewport is not the slot
Media queries ask about the viewport, which is the browser window or screen. Container queries ask about the size of an ancestor element that you have marked as a container. That lets a component change its layout to fit the space it actually gets. Use media queries for page layout and user preferences. Use container queries for components that appear in more than one place.
You only see the difference when the two disagree. In the demo below, both cards sit in boxes that you resize with the slider. The window stays the same size the whole time.
box: measuringUnder a 600px viewport the media card is locked to stacked at every slider position, and the readout says so. On a phone, watch the container card: it still follows its box.
Drag the slider to the left. Once its box drops below 360px, the container query card moves its image above the text. The media query card cannot see the box at all. On a screen 600px or wider it stays side by side however much you squeeze it, until the text is crushed into a thin column. On a narrower screen it stays stacked even at full width. Either way, the readout shows why: the media card's state is locked to the viewport width, and that number never moves.
</> Same styles, two different questions.Show code
.card { display: grid; gap: 1rem; }
/* Asks: how wide is the viewport? */
@media (min-width: 600px) {
.card { grid-template-columns: 120px 1fr; }
}
/* Asks: how wide is my nearest container? */
.slot { container-type: inline-size; }
@container (min-width: 360px) {
.card { grid-template-columns: 120px 1fr; }
}This is the one thing a media query cannot do. It answers a question about the page, and a component rarely knows where on the page it will end up.
Mark the box, then ask it
A container query has two parts. First, an ancestor opts in with container-type. Second, an @container rule sets a condition and styles the elements inside that ancestor. The value you will use most is inline-size. It lets you query the inline size, which is the width in horizontal writing modes.
You can name a container with container-name or with the container shorthand. A named rule then targets that container, not just the nearest one. Containers also give you new units: 1cqi is 1% of the container's inline size, just as 1vw is 1% of the viewport width.
Readable at any width
inline size: measuringThe heading uses font-size: clamp(1rem, 7cqi, 2.5rem). As you drag, the readout shows the width of the container's content box, the current value of one cqi, and the font size that results. The bar beside the heading shows which rule wins. It is gray when nothing matches, teal from 300px and peach from 460px. When both rules match, the one written later wins, as it would anywhere else in the cascade.
</> Name the container, size text with cqi.Show code
.slot {
container: slot / inline-size; /* name / type */
}
.card h3 {
font-size: clamp(1rem, 7cqi, 2.5rem);
}
@container slot (min-width: 460px) {
.card h3 { border-color: var(--accent); }
}One level deeper. Container queries could create a loop. Suppose the container's width depended on its children, and the children changed layout based on that width. The browser could then switch back and forth forever. container-type breaks the loop by applying size containment on the axis you query. The browser sizes the container as if it had no children, lays it out, and only then evaluates the queries for the elements inside. Every failure in sections 5 and 6 comes from this one rule.
If no ancestor is a container, cqi does not break. It falls back to the small viewport size, so 1cqi acts like 1svi: 1% of the viewport's inline size when the browser's retractable toolbars are showing.
One component, two slots
Here is the payoff on a real page. The same card markup appears in a narrow sidebar and in a wider main column. Each slot is a container, and the card has one rule: go side by side at 360px or wider.
Sidebar: measuringOn a narrow phone both slots are under 360px, so both cards stack. That is the container query doing its job.
Each card picks its layout from its own slot. Turn on Swap slot widths and the two cards trade layouts. No class names change, and no script touches the cards. The script only changes the grid columns and reports what it measures.
With media queries, you would need a modifier class such as card--sidebar, and you would have to keep every breakpoint in step with the page grid. Move the component to a new slot and those rules quietly stop fitting. A container query keeps the decision inside the component. Section 7 lets you tune this threshold yourself and copy the finished rule.
The change is visual only. Screen readers and the Tab key follow the DOM order, and that order is the same in both slots. Keep it that way. If a container rule reorders content with order or grid placement, keyboard focus will move in an order that no longer matches what people see.
Which container did it read?
Most reports of container queries not working come down to four silent rules. The browser shows no warning. The rule never applies, or it applies against a box you did not mean.
Each element looks for its container among its ancestors, nearest first. An unnamed @container rule takes the nearest ancestor whose container-type can answer the query. A named rule, such as @container card, skips every ancestor that is not named card. That gives four ways to miss. No ancestor declares a container. The name in the rule matches no ancestor. The element you style is the container itself, so it looks past itself. Or the container is a plain inline box, such as a default <span>, which cannot be a size container.
resolvingThe script does not guess. It writes three @container rules whose thresholds sit between the wrappers' widths, then reads which rule won. For the units, it measures a hidden 100cqi line and reports whichever wrapper's content width matches. The outlined wrapper is the one your browser chose.
Each button cycles its wrapper through no container, an unnamed container and a container named card. Try these in order. Turn on Query asks for name card. Nothing is named yet, so nothing matches: that is a mismatched name. Now set the outer wrapper to named card. The query skips two closer containers and reads the outer one. Turn the name switch off and set all three wrappers to none: that is a missing declaration.
Restore the wrappers and turn on Rule styles the inner wrapper itself. It does not fail. It reads the middle wrapper, which is worse, because the layout now changes at a width you never meant to test. Last, turn on Inner wrapper is display: inline. The spec still picks the inline wrapper as the container, finds that it cannot answer a size query, and treats the result as unknown. It does not fall back to the middle wrapper. The readout reports what your browser actually did, not what the spec says it should do.
Watch the end of the readout as well. Container units ignore names. While a named query reads the outer wrapper, 1cqi on the same pill still measures the nearest container on the inline axis. A rule and the units inside it can read two different boxes. With the inline switch on, the unit line shows which box your browser used for cqi once the nearest container is an inline box. That part is measured too, so it may differ from the rule's answer.
</> Query from inside a block container.Show code
/* Broken: .card looks above itself */
.card { container-type: inline-size; }
@container (min-width: 400px) {
.card { display: flex; }
}
/* Fix: the wrapper is the container */
.card-slot { container-type: inline-size; }
@container (min-width: 400px) {
.card { display: flex; }
}
/* Broken: a default span is inline */
span.slot-broken { container-type: inline-size; }
/* Fix: make it a block first */
span.slot-fixed { container-type: inline-size; display: block; }In Chromium DevTools, every query container gets a container badge in the Elements panel. Check for the badge on the wrapper you expect before you debug the rule itself.
inline-size versus size
container-type has three values you will meet. normal is the default and creates no size container. inline-size lets you query width. size lets you query both width and height. It is tempting to use size everywhere just in case, and that is the most common reason a container suddenly vanishes.
container-type: normalTry each value. With normal, neither pill fills, because there is no container. With inline-size, the width pill fills but the height pill does not, because a height query has no container to read. With size, the width query still works, but the box collapses to a thin outlined strip and the pills spill out below it. The readout shows its height drop to just its padding and border. Now turn on Give it a height. The box opens to 140px and the height query matches. Try the same switch with inline-size and you will see that a height alone is not enough. Only size can answer height queries.
One level deeper. Size containment tells the browser to size the box as if it were empty. Width rarely depends on content, because a block in normal flow takes the width of its parent. Height nearly always depends on content, because a block is as tall as what it holds. Contain the height and nothing is left to set it, so the content height becomes zero. That is why size only makes sense when something outside the content sets the height. That can be an explicit height, a flex or grid track with a fixed size, or a region that fills the viewport.
</> Use size only when the height comes from outside.Show code
/* Width queries: the usual choice */
.slot { container-type: inline-size; }
/* Height queries: something else must set the height */
.panel {
container-type: size;
height: 70vh; /* without this, the panel collapses */
}
@container (min-height: 400px) {
.panel .chart { aspect-ratio: auto; }
}When inline-size collapses too
inline-size is safe on an ordinary block because its parent sets its width. It is not safe on an element that takes its width from its content, which is called shrink-to-fit sizing. Containment makes the content count as nothing, so the box shrinks to its padding.
This happens with a flex item that has an automatic width in a row, a float, an inline-block, an absolutely positioned element with no width, an element with width: fit-content, and an item in a grid column sized to its content, such as max-content.
item width: measuringPress the button to measure all nine cases plus the size container.Turn on container-type: inline-size. The first flex item shrinks to a sliver about as wide as its padding, and its text spills over the next item. The text is fine. The browser simply measured the item as if it were empty. Now turn on flex: 1. The item takes its width from the free space in the row, not from its content, so it fills out again.
</> Let the layout, not the content, set the width.Show code
.toolbar { display: flex; }
/* Broken: its width came from its content */
.toolbar .search-broken { container-type: inline-size; }
/* Pick one of the two fixes below, not both. */
/* Fix A: let the row hand out the width */
.toolbar .search-grow { container-type: inline-size; flex: 1; }
/* Fix B: give it a width of its own */
.toolbar .search-fixed { container-type: inline-size; width: 20rem; }One level deeper. Shrink-to-fit sizing asks the content for its preferred width, called its max-content size, and its narrowest width, called its min-content size. Inline-size containment answers both questions with zero. A flex item's default min-width: auto normally stops it from shrinking below its content, but that floor is now zero too. So the only width left is the padding and border.
The Measure all nine cases button builds every case this section names in an invisible box, including the open dialog and a popover described below. It measures each one with getBoundingClientRect() before and after adding container-type: inline-size, then removes the box before the next frame is drawn. Every test box has 10px of padding and a 1px border on each side, so the arithmetic predicts 22px for each collapsed case. The plain block is the control, and it should not move. The popover is opened with showPopover(), measured, then hidden again. If your browser cannot open one, that line reads popover: not testable here rather than disappearing. The last entry does the same test for a block with container-type: size and no height. Press the button in each browser you ship to and compare the lines. A support table tells you a feature exists. This tells you what that browser measured on that machine today.
What building this turned up. Two cases are easy to miss because they come from the browser's default styles, not from your CSS. An open <dialog> collapses, because its default styles give it width: fit-content. A [popover] element gets the same default, so it collapses for the same reason, and the popover line in the readout shows whether it did in your browser. Give either one an explicit width, such as width: min(40rem, 90vw), before you make it or anything inside it a container. In a grid, a max-content column collapses the whole track, not only the item. The track asks the item for its max-content size and gets zero.
Which query to reach for
Two practical choices remain before you ship: the unit you write thresholds in, and what happens in browsers without support. Then comes the real decision, which query to use at all.
Write thresholds in rem. The demo below shows why, then writes the rule for you. Two cards sit in slots of the same width. The top card stacks below a rem threshold. The bottom card uses the same threshold converted to px at a 16px default, which is what you get if you design in px. Raise default text size, the setting a reader changes in browser preferences. The text in both cards grows. The rem threshold grows with it, so the top card stacks while its text still fits. The px card stays side by side and squeezes the larger text into a column that is too narrow.
slot: measuring</> Your rule, written with the values you tuned.Show code
Move a slider to write the rule.
</> A ready prompt for your coding agent.Show code
Move a slider to write the prompt.
The text-size slider starts at your browser's real root font size, measured on load. It simulates a different default by scaling the cards' text and converting the rem threshold at that size, because changing the real root would resize this whole page.
One level deeper. Units in a container condition resolve against the query container, not the element you style. rem means the root element's computed font size, so a page that sets html { font-size: 62.5% } also shifts every rem container threshold. Media queries behave differently: their rem and em use the browser's initial font size and ignore the page's root rule. An em in a container condition uses the container's own font size. Page zoom scales px and rem alike, so the difference you just felt only appears when someone changes the default text size.
Browsers without support ignore @container and keep the base styles. That is why the generated rule writes the narrow layout as the base and adds the wider layout inside the query. The panel below tests the browser you are using now.
checkingChecking this browser.
For context: size container queries and container units shipped in Chrome and Edge 105, Safari 16 and Firefox 110. Style queries on custom properties shipped in Chrome and Edge 111, Safari 18 and Firefox 151.
The first three checks ask CSS.supports() whether the browser can parse each declaration. Parsing is not proof that a feature works, so the two pills go further. Each one is styled only by a real @container rule, and the script reads the custom property that rule sets. A filled pill means the query actually matched here.
If you animate between layouts, turn the motion off under prefers-reduced-motion. If a narrow container hides content, make sure nothing essential disappears. If it fades content in and out, fading out of display: none shows how to do that without a jump.
Now the decision itself. Use media queries for the page: the overall grid, the navigation pattern, and anything that depends on the device or the person rather than a box. No container can answer prefers-reduced-motion, prefers-color-scheme, hover, pointer or print. Use container queries for components that appear in slots of different widths, such as cards, media objects, forms inside dialogs and dashboard widgets. The two work together: media queries shape the page grid that creates the slots, and container queries respond inside them.
</> Media queries for the page, container queries for the parts.Show code
/* Page layout and preferences */
@media (min-width: 64rem) {
.page { grid-template-columns: 18rem 1fr; }
}
@media (prefers-reduced-motion: reduce) {
.card { transition: none; }
}
/* Components */
.slot { container: slot / inline-size; }
@container slot (min-width: 22rem) {
.card { grid-template-columns: 8rem 1fr; }
}Start from the question. If the question is how much room the component has, ask its container. If the question is about the screen, the device or the person using it, ask a media query.
When you do use a container query, put container-type on the slot wrapper, not on the component and not on every element. Make sure the wrapper's width comes from the layout, never from its content. Reach for size only when something outside the content sets the height. And do not reach for a container query at all when the component only ever lives in one slot. A one-off hero banner that always spans the page gains nothing from asking its slot, because its slot tracks the viewport. A media query is simpler there and needs no extra wrapper.
Quick answers
What is the difference between container queries and media queries?
Media queries test the viewport, which is the browser window or screen. Container queries test the size of an ancestor element marked with container-type. That lets a component change its layout to fit the space it actually gets, wherever it is placed. Use media queries for page layout and user preferences, and container queries for components that appear in more than one place.
Why is my container query not working?
The usual causes fail silently. An element never queries itself, so container-type must go on a wrapper, not on the element you style. A plain inline box, such as a default span, cannot be a size container. The name in @container may match no ancestor, or no container was declared. Chromium DevTools shows a container badge on every query container in the Elements panel.
Why does container-type: size make my element collapse?
Size containment makes the browser lay out the box as if it had no children. Width usually comes from the parent, but height comes from the content, so the content height of a size container becomes zero. Use container-type: size only when something outside the content sets the height, such as an explicit height, a fixed grid or flex track, or a region that fills the viewport.
Should container queries replace media queries?
No. They answer different questions and work together. Media queries shape the page grid and handle anything about the device or the person, such as prefers-reduced-motion, prefers-color-scheme, hover, pointer and print. No container can answer those. Container queries respond inside the slots that the grid creates, for cards, media objects, forms inside dialogs and dashboard widgets.
Which browsers support CSS container queries?
Size container queries and container query units such as cqi work in Chrome and Edge 105, Safari 16 and Firefox 110, and in every later version. Browsers without support ignore @container rules and keep the base styles. So write the narrow layout as the base and add the wider layout inside the container query.