Sub topic 5
Container query units: cqi, cqb, cqw, cqh, cqmin, cqmax
This chapter assumes you know media queries and viewport units (vw, vh), custom properties, basic flexbox or grid, and clamp(). It also assumes you have declared a container with container-type, as covered in the earlier chapters.
Units that measure a container
Viewport units such as vw are percentages of the browser window. Container query units work the same way, but the percentage is taken from a query container. A component can then size its padding, type and gaps relative to the space it has been given, wherever it is placed.
There are six units. Each is 1% of a dimension of the container.
| Unit | Meaning |
|---|---|
| `cqw` | 1% of the container's width |
| `cqh` | 1% of the container's height |
| `cqi` | 1% of the container's inline size |
| `cqb` | 1% of the container's block size |
| `cqmin` | The smaller of `cqi` and `cqb` |
| `cqmax` | The larger of `cqi` and `cqb` |
Inline size is the dimension along the text direction. In horizontal writing modes such as English, cqi equals cqw and cqb equals cqh. In a vertical writing mode they swap. Prefer cqi and cqb when the component might be used in more than one writing mode, because they follow the writing mode automatically.
Using the units
Take the card markup from earlier chapters.
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text that reflows with the wrapper, not the viewport.</p>
</article>
</div>The wrapper is the container. The units are used on its descendants.
.card-wrapper {
container: card / inline-size;
}
.card {
padding: 2cqi;
}
.card h2 {
font-size: clamp(1.125rem, 4cqi, 2rem);
}If the wrapper is 600px wide, 2cqi is 12px and 4cqi is 24px. At 300px wide they are 6px and 12px. The clamp() call keeps the heading between 1.125rem and 2rem, so the text never becomes tiny or huge.
No media query is involved. If you put the same card in a narrow sidebar and a wide main column, each copy sizes itself to its own wrapper.
Which axes need containment
A unit only works on an axis the container has size containment for.
container-type: inline-sizecontains the inline axis.cqiandcqwresolve against it.container-type: sizecontains both axes. All six units resolve against it. It also requires the container to get its height from somewhere other than its content, such as an explicitheightor a flex or grid parent.
.panel {
container-type: size;
height: 20rem;
width: 100%;
}
.panel .badge {
/* 1cqb is 1% of the panel's block size (200px / 100 = 2px per cqb) */
width: 10cqmin;
height: 10cqmin;
}10cqmin keeps the badge square. It is 10% of whichever panel dimension is smaller, so the badge shrinks with the panel's tighter dimension and never overflows it.
How a unit finds its container
For each unit, the browser walks up the ancestors and picks the nearest one with containment on the matching axis. For cqi that means the nearest inline-size or size container. For cqb it means the nearest size container. A single element can therefore use cqi from one ancestor and cqb from another.
When no ancestor qualifies, the unit falls back to the small viewport unit for that axis, for example svb for cqb. A later chapter covers this fallback in detail.
Common mistakes
- Using
cqborcqhwithcontainer-type: inline-size. The block axis is not contained, so the unit skips this container and uses a higher one or the fallback. Usecontainer-type: sizeand give the container a definite height. - Expecting units to be pixel-exact for children of a padded container. The unit measures the container's content box, so the container's own padding is excluded.
- Using large values without bounds.
10cqiin a 1200px container gives 120px. Wrap type sizes inclamp().
Exercise
A .gallery element has container-type: inline-size and is 800px wide and 500px tall. A child has width: 5cqi; height: 5cqb;. Which container does each unit use, and what does the height resolve to?
Show answer
cqi resolves against .gallery because it has inline-size containment, so the width is 40px. .gallery has no block-axis containment, so cqb skips it. The height resolves against a higher size container if one exists. Otherwise it falls back to 5% of the small viewport block size (5svb). It does not resolve to 25px. To get 25px (5% of 500px), change .gallery to container-type: size.