Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท At-rules

The @container at-rule

Reference for the CSS @container at-rule: query the size of a parent container, name containers, use container units, with live resizable examples.

The @container at-rule applies styles based on the size of a container element instead of the viewport. A card can then switch from a stacked to a side-by-side layout depending on the space it is given, no matter where on the page it sits.

For this to work you must first mark an ancestor as a container with container-type (and optionally name it with container-name). The query then looks at the nearest matching container ancestor.

CSS
.card-wrap { container-type: inline-size; }

@container (min-width: 30rem) {
  .card { display: flex; }
}

Try it

A card that reacts to its box

Syntax

CSS
.parent {
  container-type: inline-size | size | normal;
  container-name: <name>;
  container: <name> / <type>;   /* shorthand */
}

@container [<name>]? <container-condition> {
  /* rules */
}

/* examples */
@container (min-width: 400px) { }
@container card (inline-size > 30em) { }
@container style(--theme: dark) { }
  • Condition features: width, height, inline-size, block-size, aspect-ratio, orientation, and style() queries for custom properties.
  • Operators: and, or, not, and the range syntax (>=, <).
  • Container units: inside a query's rules, cqw, cqh, cqi, cqb, cqmin, cqmax are 1% of the container's size.

Values

Syntax and arguments Description
container-type: inline-size Query the inline dimension (width in horizontal writing). Most common.
container-type: size Query both dimensions; the container then needs an explicit size.
container-type: normal Not a size container (the default).
@container (min-width: 30rem) Nearest ancestor with a container type.
@container name (...) Only the ancestor with container-name: name.
@container style(--x: 1) Style query on a custom property of the container.

Usage notes

  • A container cannot style itself. The query applies to descendants, so put the query target one level below the container element.
  • Containment has side effects. inline-size containment means the element's width no longer depends on its content, so it may collapse in a shrink-to-fit parent (flex or inline-block). Give it an explicit width or let it stretch.
  • Name your containers in larger projects, so a query does not accidentally match a nearer, unrelated container.
  • Do not use size unless you need height queries. It requires a definite height.
  • Combine with viewport queries. Use @media for page-level layout and @container for components.
  • Container units like cqi make type and spacing scale with the component without any query.
  • Style queries read computed custom properties and are the part with the least support.

Browser support

Size container queries are Baseline since February 2023 (Chrome and Edge 105, Safari 16, Firefox 110). Style queries for custom properties are newer and less widely supported; test before depending on them.