Media queries respond to the viewport. Container queries respond to the size of an ancestor container, so a component can adapt to the space it's actually given, whether that's a sidebar, a main column or a modal, without knowing the page layout. You mark an element as a container with container-type: inline-size and write @container (min-width: 30rem) { … }. Size container queries have been Baseline in all major browsers since 2023, which makes them the right default for responsive components. Media queries remain right for page-level layout and user preferences.
.card-slot {
container-type: inline-size; /* query my inline size */
container-name: card; /* optional; target a specific ancestor */
}
.card { display: grid; gap: 1rem; }
@container card (min-width: 28rem) {
.card { grid-template-columns: 10rem 1fr; } /* image beside text */
}
@container card (min-width: 45rem) {
.card__title { font-size: clamp(1.25rem, 4cqi, 2rem); }
}
@container query matches the nearest ancestor container. Named containers let you skip levels.cqi/cqb (1% of the container's inline/block size), cqw, cqh, cqmin, cqmax. They're useful for fluid type inside components.@container (400px <= width < 700px).container-type: inline-size applies inline-size containment: the container's width can no longer depend on its children. Usually harmless for block-level slots that fill their parent. But on an element that shrink-wraps its content (inline-block, a flex item with flex: none, an absolutely positioned popover), the container may collapse to zero width. Put the container on a wrapper whose width comes from the layout, not from content. container-type: size also contains the block size, so it requires an explicit height. Use it rarely.
.theme-scope { --variant: compact; }
@container style(--variant: compact) {
.card { padding: .5rem; }
}
Style queries for custom properties are supported in Chromium and Safari. Firefox support has lagged, so treat them as progressive enhancement. Scroll-state queries (e.g. styling a sticky header when it's "stuck") are newer and Chromium-only at the time of writing.
<ProductCard> works in a 3-column grid, a sidebar and a carousel without page-specific overrides or variant="small" props.ResizeObserver hacks for layout.prefers-color-scheme, prefers-reduced-motion, prefers-contrast.hover, pointer: coarse.vw units into components that are supposed to be container-relative. Use cqi.container-type: inline-size, containment, and the shrink-wrap collapse pitfall.cq* units, style queries' support caveats, and keeping media queries for page and preference concerns.Spotted a mistake or a rights concern? Report an issue