What is a CSS container query?▾
A container query is a CSS rule that applies styles to an element based on the size of its nearest ancestor that has declared container-type. Unlike media queries that use viewport dimensions, container queries use the containing element's dimensions, enabling component-level responsive design.
How is @container different from @media?▾
@media queries respond to the viewport size (or device capabilities). @container queries respond to the size of a specific ancestor element that has container-type set. Container queries enable the same component to adapt differently based on where it is placed, not on the screen size.
What does container-type: inline-size do?▾
inline-size establishes the element as a query container for its inline dimension (width in horizontal writing modes). It applies only width-based containment without triggering height containment, avoiding the layout restrictions of container-type: size.
Why doesn't my container query trigger?▾
The most common cause is a missing container-type declaration on the parent. Without container-type, the element is not a query container and @container rules targeting it are silently ignored. Ensure the direct ancestor (or named ancestor) has container-type: inline-size or size.
Can I name multiple containers?▾
Yes. Use container-name: name on each container element and reference the name in the @container rule: @container name (min-width: 400px). Without a name, @container queries the nearest ancestor with container-type set, which may not be the intended element in nested layouts.
Which browsers support container queries?▾
Container queries are supported in Chrome 105+, Firefox 110+, Safari 16+, and Edge 105+. Global support exceeds 90% as of 2025. They are safe to use in production for modern web apps and progressive enhancement contexts.
Should I replace media queries with container queries?▾
Not entirely. Media queries remain appropriate for page-level layout shifts like switching from a one-column to three-column grid at a viewport breakpoint. Container queries are better for component-level adaptations. The two work together: use media queries for scaffolding, container queries for components.
Are container query units like cqw and cqi safe to use?▾
cqw (1% of query container width), cqi (1% of inline size), cqb, cqh, and cqmax/cqmin are supported in all browsers that support container queries. Use cqi rather than cqw in most cases since inline-size is the more commonly established container dimension.