Overview
@container rules respond to the size of a containment context (a named ancestor) rather than the viewport. Choose a container query when one component appears in slots of different widths, such as a card in a sidebar and in a main grid. Keep viewport media queries for page-shell decisions; the two solve different problems.
Rules
- Declare the container with
container-type: inline-size(width; the usual choice) orsize(both axes; needs a definite block size), and name it withcontainer-nameor thecontainer: name / typeshorthand. A container cannot query itself, only descendants. - Size queries are Baseline widely available (all engines since early 2023). Container units such as
cqisize relative to the container. - Style queries
@container style(--variant: compact)read custom property values only, not regular properties. They became Baseline newly available in May 2026 (Chrome 111, Safari 18, Firefox 151); add a fallback if you support older Firefox. container-type: normalmakes an element a style-query container only.
.card-wrapper { container: card / inline-size; }
@container card (min-width: 400px) {
.card { flex-direction: row; }
}Related
- viewport: viewport media queries remain for page layout.
- cascade:
@containerrules cascade like@mediarules. - custom-property: what style queries read.
- css-container-queries: patterns in depth.
- css: CSS conventions.