Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Media Queries and Features

Container query features

Reference for the features you can test in @container: width, height, inline-size, block-size, aspect-ratio and orientation, with range syntax and an example.

Inside @container, the conditions you write test the size of a container element, not the viewport. The feature list is smaller than for @media: only features that describe the container's own dimensions are available. Everything else (hover, color scheme, motion) is still a media feature.

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

@container (inline-size > 30em) {
  .card { display: flex; }
}

Try it

The same card sits in two containers of different widths and rearranges itself according to its own container, not the window.

Same card, two containers

Syntax

CSS
@container (inline-size >= 30em) { }
@container card (min-width: 400px) { }          /* named container */
@container (orientation: landscape) { }
@container (400px < width < 800px) and (aspect-ratio > 1) { }

A container must set container-type (inline-size, size or normal) for these queries to work against it.

Values

Feature Description
width Width of the container's content box. Needs container-type: size.
height Height of the content box. Needs container-type: size.
inline-size Size along the inline axis (the width in horizontal writing modes). Works with inline-size or size.
block-size Size along the block axis. Needs container-type: size.
aspect-ratio Width to height ratio of the container. Needs container-type: size.
orientation portrait or landscape, from the container's dimensions. Needs container-type: size.

All are range features except orientation, so the range syntax and the min- and max- prefixes work.

Usage notes

  • Pick inline-size by default. It only constrains the container in one direction, so its height can still follow the content. size containment makes the container ignore the height of its children, which is easy to get wrong.
  • Units. Inside @container the units cqw, cqh, cqi, cqb, cqmin and cqmax refer to the container's dimensions.
  • Nearest matching container. A query applies to the nearest ancestor container that can answer it. Use a container name to pick a specific one.
  • Not the container itself. A container query styles descendants of the container, never the container element.
  • Style queries are separate. @container style(--theme: dark) asks about custom properties, not size; support is more limited, so check a compatibility table.
  • Media features unavailable. Features like hover and prefers-color-scheme do not work in @container; use @media for them.

Browser support

Size container queries are supported in current versions of all major browsers; check a compatibility table for older versions.