CSS Reference ยท Units and Values
The cqw, cqh, cqi and cqb units
Reference for the CSS container query units cqw, cqh, cqi, cqb, cqmin and cqmax: sizes relative to the nearest query container, with live examples.
Container query units are lengths measured against the size of the nearest query container: an ancestor that has container-type set. 1cqw is 1% of that container's width and 1cqh is 1% of its height. They work like viewport units, but the reference box is a component instead of the browser window.
This lets a card scale its own text and spacing to the space it is given, wherever it is placed. The units work in any property, not only inside @container rules.
.card-wrap { container-type: inline-size; } .card h2 { font-size: clamp(1rem, 6cqi, 2rem); }
Try it
The dashed box is the query container. Its percentage units (cqi) shrink and grow as you drag, while the card inside is unaware of the page width.
Syntax
<length> = <number>cqw | <number>cqh | <number>cqi | <number>cqb
| <number>cqmin | <number>cqmax
- Reference: the nearest ancestor query container that can answer for that axis.
- Fallback: if there is no suitable container, the unit becomes the small viewport equivalent (
svw,svh, and so on). - Applies to: any property that accepts a
<length>. - Animatable: yes, as part of a length.
Values
| Value | Description |
|---|---|
cqw |
1% of the container's width. |
cqh |
1% of the container's height. |
cqi |
1% of the container's inline size (width in horizontal writing modes). |
cqb |
1% of the container's block size (height in horizontal writing modes). |
cqmin |
The smaller of cqi and cqb. |
cqmax |
The larger of cqi and cqb. |
Usage notes
- A container is required. The element needs
container-type: inline-size(orsize) for the units to refer to it. A normal block ancestor is not enough. - Inline size only gives width units. With
container-type: inline-size,cqiandcqwwork, butcqbandcqhneedsizecontainment (and an element with a defined height) to refer to the container; otherwise they fall back to viewport units. - A container never measures itself. The unit used on the container element refers to an outer container (or the viewport), not to itself.
- Prefer logical units.
cqiandcqbfollow the writing mode, so they keep working in vertical text. - Limit extremes. Wrap fluid text in
clamp()so small containers do not produce tiny type. - Accessibility. As with any fluid type, keep a
remterm in the formula so user font settings still have an effect.
Browser support
Container query units arrived together with container queries and are supported in current versions of the major browsers. Check a compatibility table for older versions, and give units a fallback value in the previous declaration.
Related
container-typeโ turns an element into a query container@containerโ conditional rules by container sizevw,vh,vmin,vmaxclamp()- Lesson: Container Queries