How it works
container-type does two things. It marks the element as a container for size queries, and it applies containment: inline-size adds style and inline-size containment, size adds style and full size containment, and both make the box an independent formatting context. Contain 3, the Candidate Recommendation, also applied layout containment; the current draft of CSS Conditional 5, where container queries now live, dropped it. Chrome 156 and Firefox 159 follow the draft: fixed and absolute children of a container still positioned against the viewport. Inline-size containment means the box's inline-axis intrinsic sizes are computed as if it were empty, while its block size still grows with content. A normal block's width comes from its containing block, so it rarely notices. Boxes that size from their content, like inline-blocks, floats, absolutely positioned boxes with width: auto, and flex items with flex-basis: auto, all lose that content.
For each element, the browser picks the query container from its ancestors: those that are valid for every feature in the condition (width needs inline-size or size; height needs size), filtered by name if one is given, nearest first. If none qualifies, the query is unknown and its rules don't apply. Several queries can be nested to ask about different containers at once:
@container page (width > 60rem) {
@container (width < 20rem) {
.card { flex-direction: column; }
}
}
Container units are evaluated per axis the same way: cqi uses the nearest container that accepts inline-size queries, cqb the nearest one for block size. They can come from different containers. With no eligible container, they use the small viewport size, so cqi behaves like svw in horizontal writing.
Edge cases
- Nearest wins. With an 800 px container named
page and an unnamed 300 px container inside it, @container (width > 500px) didn't match a paragraph inside both; @container page (width > 500px) did.
- Self-queries don't exist. A rule
@container (width > 0px) { .self { color: red } } on an element that is itself the only container left its colour unchanged.
- Style queries on standard properties. The spec allows
style(display: flex), but in Chrome 156 and Firefox 159 it didn't match a display: flex parent, while style(--variant: compact) did. Custom-property style queries shipped in Chrome 111, Firefox 151 and Safari 18.
- Pseudo-elements query their originating element's ancestors and can't be containers.
Real-world scenario
A design system ships a <product-card> used in a 4-column grid, a carousel, and a checkout sidebar. With media queries, each placement needs page-specific overrides. Instead:
.card-slot { container: card / inline-size; }
@container card (width >= 28rem) {
.product-card { grid-template-columns: 10rem 1fr; }
}
@container card (width < 16rem) {
.product-card .price-note { display: none; }
}
.product-card h3 { font-size: clamp(1rem, 0.75rem + 2cqi, 1.5rem); }
Each page wraps cards in .card-slot (or puts it on grid items, which get their width from the track), and the card adapts wherever it lands. Variants like compact can be a custom property queried with style(--card-variant: compact) instead of a modifier class passed through every layer.
Trade-offs
- Wrapper elements. The container can't be the element being restyled, so components often need an extra wrapper, or the slot itself becomes the container.
- Containment side effects. No shrink-to-fit width;
size containers need explicit heights. The container is also an independent formatting context, so it contains its floats.
- Debuggability. Which ancestor answered a query isn't visible in the CSS; naming containers makes it explicit.
- Units.
cqi scales with the slot, which suits components; for text, combine it with rem in clamp() so user font settings still apply.
- Newer features. Scroll-state queries (
container-type: scroll-state) are supported in Chrome since 133, not in Firefox or Safari.