# CSS container query

## What it is

A CSS container query applies rules to descendants according to conditions on an
ancestor container. A size query can ask whether the container's inline size is
at least a given value. This lets a component respond to the space the page
gives it. The same card can use a stacked layout in a narrow sidebar and a split
layout in the wider article column, even when both cards appear in the same
browser window.

For a size query along the inline axis, declare an ancestor as a query container
with `container-type: inline-size`. Then put the conditional styles for its
descendants in an `@container` rule. A threshold expressed as
`inline-size >= 32rem` refers to the eligible container's inline dimension. In
an ordinary horizontal writing mode that is the width. Inline size follows the
writing mode, which matters when layouts also support vertical text.

Without a name, the query uses the nearest eligible ancestor for the queried
features. An explicit container name can tie the condition to a particular
wrapper when a component contains nested query containers. Use a name that
describes the layout relationship so the rule makes sense when reading the
stylesheet apart from the HTML.

## Why it matters

Viewport media queries work well when a choice depends on the whole page or the
user's environment. Reusable components also encounter differences within one
viewport. A search result could appear in a one-column mobile layout, a
full-width desktop panel and a desktop drawer. The drawer may be narrower than
the mobile result. Making a component depend on the space it receives keeps its
layout decision relevant in all three placements.

This can reduce the number of page-specific CSS overrides. It also makes the
layout easier to verify. The same article card can be exercised at a range of
container sizes in a test page with no change to the browser window. The
comparison shows which adjustments belong to the component and which still
belong to the overall page.

The declaration creates a containment boundary and can change intrinsic sizing.
In the queried axis the element no longer determines its size from its children
as it did before. Consider a wrapper that would otherwise shrink to the natural
width of its text. Give the wrapper a predictable size from its parent grid or
normal flow before its contents depend on that size.

## What it looks like in practice

Start with a reference card whose image, heading and description stack in normal
flow. Put it in a wrapper that receives space from the parent layout and set
`container-type: inline-size` and `container-name: reference-card` on the
wrapper. When that container crosses the point where the picture and the prose
fit comfortably beside one another, use
`@container reference-card (inline-size >= 32rem)` to turn the card itself into
a two-column grid.

Place the same card in an article grid and in a narrow sidebar at a single
desktop viewport. Verify the baseline stacking in the sidebar and the wider
arrangement in the article. Then resize just the sidebar and test immediately on
either side of the condition. Set the threshold from the real content. A long
heading, larger user text or translated description can show that a visually
appealing threshold leaves too little room for the reading column.

Engineer's
[typographic scale case study](/portfolio/replaced-27-font-sizes-with-a-6-step-scale/)
describes replacing 27 font sizes with six deliberate steps and reviewing the
small text on a phone. Use the same kind of inspection for the card. A width
condition can set the arrangement, while the type decisions still need to
produce readable text in both arrangements. Review the same card with the
longest useful copy, including a title that wraps over multiple lines in a
translated edition.

Keep the baseline styles complete. The links, source order and heading should
remain usable when the rule does not apply. Compare keyboard movement and visual
order through both layouts. A responsive rule can move a picture visually
without gaining any reason to reorder the controls.

## Where it goes wrong

A frequent mistake is putting the changing layout on the same element meant to
provide the query size. A query conditions styles on an eligible ancestor of the
element being styled. Use a wrapper as the query container and put the layout
rule on the inner card. Record that relationship in the component so a refactor
preserves it.

Nesting can change which size is read. An unnamed rule may start responding to a
newly introduced wrapper closer to the target. Inspect the matched container
before tuning a threshold that appears wrong. Use a name when a rule
intentionally measures a specific ancestor, and check that each instance still
supplies it.

Containment also exposes layouts that depend on the width of their contents. If
a wrapper loses its expected size in a flexible layout, inspect the parent's
grid tracks or sizing rules. Fix the allocation of space at that level. Raising
or lowering the query threshold cannot recover a dimension the parent never
allocated.

Finally, test the condition at useful extremes. Check a narrow column, a wide
one, a screen with increased text size and a second language that wraps
differently. A finished component explains its baseline layout, the change it
makes at a measured width and the reading behaviour people experience on both
sides.

## Further reading

- [CSS Conditional Rules on container queries](https://drafts.csswg.org/css-conditional-5/#container-queries).
  Defines the syntax and selection of a query container.
- [MDN's container query guide](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Container_queries).
  Shows how to declare a container and style its descendants.
- [CSS Containment Level 3](https://drafts.csswg.org/css-contain-3/). Explains
  containment and links to the current definitions for container queries.

<https://engineer.company/glossary/css-container-query/>
