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) or size (both axes; needs a definite block size), and name it with container-name or the container: name / type shorthand. A container cannot query itself, only descendants.
  • Size queries are Baseline widely available (all engines since early 2023). Container units such as cqi size 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: normal makes an element a style-query container only.
.card-wrapper { container: card / inline-size; }
 
@container card (min-width: 400px) {
  .card { flex-direction: row; }
}