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.
.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.
Syntax
@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-sizeby default. It only constrains the container in one direction, so its height can still follow the content.sizecontainment makes the container ignore the height of its children, which is easy to get wrong. - Units. Inside
@containerthe unitscqw,cqh,cqi,cqb,cqminandcqmaxrefer 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
hoverandprefers-color-schemedo not work in@container; use@mediafor them.
Browser support
Size container queries are supported in current versions of all major browsers; check a compatibility table for older versions.
Related
@containerโ the at-rulerange syntaxโ comparison operatorswidth and heightโ the viewport equivalents- Lesson: Container Queries