Sub topic 5
Container query units: cqi, cqb, cqw, cqh, cqmin, cqmax
This chapter assumes you already know media queries and viewport units (vw, vh), CSS custom properties, flexbox or grid basics, and clamp(). It also assumes you have declared a container with container-type and written a basic @container rule. If not, review the earlier chapters on those topics first.
What container query units are
A viewport unit such as vw is a percentage of the browser window. A container query unit is a percentage of a query container, which is an ancestor element that has container-type set. The unit lets a component size its type, padding or decorations in proportion to the space it was given, wherever it is placed on the page.
There are six units. Each one equals 1% of a dimension of the query container.
| Unit | Meaning | Closest viewport counterpart |
|---|---|---|
| `cqw` | 1% of the container's width | `vw` |
| `cqh` | 1% of the container's height | `vh` |
| `cqi` | 1% of the container's inline size | `vi` |
| `cqb` | 1% of the container's block size | `vb` |
| `cqmin` | The smaller of `cqi` and `cqb` | `vmin` |
| `cqmax` | The larger of `cqi` and `cqb` | `vmax` |
Take a container that is 400px wide. Then 1cqi is 4px, 10cqi is 40px, and 50cqi is 200px. If the container gets resized to 600px, every length written in cqi inside it is recomputed. No @container rule is needed, so the values scale continuously.
Logical versus physical units
cqw and cqh are physical. They always mean width and height. cqi and cqb are logical. They follow the writing mode of the container. In a horizontal writing mode such as English, the inline axis is horizontal, so cqi equals cqw and cqb equals cqh. In a vertical writing mode such as writing-mode: vertical-rl, the axes swap: cqi tracks the height and cqb tracks the width.
Prefer cqi and cqb unless you specifically need a physical dimension. They keep working if the component is later used in a vertical or right-to-left layout. They also match the axis names used by container-type: inline-size.
A worked example
Here is the markup and CSS. The wrapper is the container, and the article is the styled descendant.
<div class="card-wrapper">
<article class="card">
<h2 class="card-title">Title</h2>
<p>Body text that scales with the wrapper, not the viewport.</p>
</article>
</div>.card-wrapper {
container: card / inline-size;
}
.card {
padding: 2cqi;
border-radius: 1.5cqi;
}
.card-title {
font-size: clamp(1.125rem, 4cqi, 2rem);
}With a 600px wrapper, the padding is 12px and the radius is 9px. The heading asks for 4cqi, which is 24px. That sits inside the clamp() range of 18px to 32px, so the heading is 24px. At 300px the heading asks for 12px, and clamp() raises it to the 18px minimum.
Always combine a unit that scales linearly with clamp(). On its own, 4cqi is unreadably small in a narrow container and enormous in a wide one.
Where the unit may be used
Container units are ordinary length values. You can use them in width, margin, gap, font-size, border-width, calc(), min(), max(), translate and anywhere else a length is valid.
There is one rule to remember: an element cannot use its own container units. A unit on the container element resolves against the container's ancestors, not against itself. Style the descendants with the units, and size the container itself in ordinary ways.
.card-wrapper {
container-type: inline-size;
/* 5cqi here refers to some ancestor container, or the fallback */
padding: 5cqi;
}Which container does a unit use?
Each unit resolves against the nearest ancestor that is a query container for that axis. This is decided per axis, not per element.
The container type controls which axes qualify:
container-type: inline-sizemakes the inline axis available.cqiandcqw(in horizontal text) can resolve against it.container-type: sizemakes both axes available.container-type: normalprovides no size container. That includes an element that only has acontainer-nameor only supports style queries.
If the nearest container does not provide the axis, the browser keeps looking further up the ancestor chain.
<main class="page">
<section class="card-wrapper">
<div class="badge"></div>
</section>
</main>.page {
container: page / size;
height: 40rem;
}
.card-wrapper {
container: card / inline-size;
}
.badge {
width: 20cqi; /* inline axis: nearest is .card-wrapper */
height: 10cqb; /* block axis: .card-wrapper has none, so .page */
background: teal;
}The width follows .card-wrapper, and the height follows .page. So 10cqb here is 4rem.
Names do not apply to units
@container card (min-width: 30rem) can pick a container by name. Units cannot. The unit always uses the nearest qualifying ancestor. If you need cqi to refer to a farther container, restructure the markup or the container declarations so that container is the nearest one for that axis.
cqmin and cqmax across containers
cqmin is the smaller of cqi and cqb. cqmax is the larger. Each of those two values is resolved separately with the rule above, so they can come from different containers. In the example above, 10cqmin on the badge compares 10% of .card-wrapper's inline size with 10% of .page's block size, and uses the smaller.
For one container that has both axes, the meaning is simple:
.panel {
container-type: size;
width: 30rem;
height: 20rem;
}
.panel .badge {
width: 10cqmin; /* min(3rem, 2rem) = 2rem */
height: 10cqmin;
}A square badge sized with cqmin stays square and always fits in the container. With cqmax it would be 3rem, which fills the larger dimension.
What the units become when there is no container
If no ancestor provides the axis, the unit falls back to the matching small viewport unit:
| Unit | Fallback |
|---|---|
| `cqw` | `svw` |
| `cqh` | `svh` |
| `cqi` | `svi` |
| `cqb` | `svb` |
| `cqmin` | `svmin` |
| `cqmax` | `svmax` |
The small viewport is the viewport size with browser toolbars fully expanded, so these values do not change as mobile toolbars appear and disappear. On a 1000px-wide window, a 4cqi heading with no container ancestor is 40px.
This fallback has a practical consequence: a typo in a container declaration does not produce an error. The component still renders, but it scales with the viewport, so the bug is easy to miss. If your sizes seem tied to the window, check the container declaration first.
Containment side effects and common mistakes
Container units depend on containment, so the pitfalls of container-type apply to them too.
- Inline-size collapse. An element with
container-type: inline-sizecannot take its width from its content. If it is a flex item, a grid item withautotracks, ordisplay: inline-block, it can shrink to nothing. Give it a width,flex: 1, or put it in a block-level context. - Height under
size. Withcontainer-type: sizethe element ignores its content when computing both dimensions. Without an explicit height, it is 0px tall andcqbvalues become 0. Useinline-sizeunless you really need the block axis. - Using
cqbwithinline-size. This produces no error. The unit skips that container and uses a higher container or thesvbfallback. - Self-styling. The element with
container-typecannot use its own units, and an@containerrule cannot restyle that same element. - Percentages are not container units.
width: 50%refers to the containing block, which may be a different element from the query container.50cqirefers to the container, regardless of how many wrappers are in between.
Browser support and fallbacks
Container query units shipped in the major browser engines together with size container queries. Older browsers ignore a declaration they cannot parse and keep the previous one. For cqi and its siblings, write a plain value first and the container-unit value second.
.card {
padding: 1rem;
padding: 2cqi;
}
.card-title {
font-size: 1.5rem;
font-size: clamp(1.125rem, 4cqi, 2rem);
}Browsers that do not understand cqi drop the second declaration and keep the first. Newer browsers use the second. A later chapter covers @supports for wrapping whole blocks.
Summary of the working model
- Each unit is 1% of one dimension of a query container.
cqiandcqbfollow the writing mode.cqwandcqhare physical.- Resolution is per axis, to the nearest ancestor that provides that axis. Names do not matter.
- No qualifying ancestor means the small viewport unit is used.
- An element cannot use units against its own container status.
- Pair scaling units with
clamp()so sizes stay in a readable range.
Exercise
A wrapper has container-type: inline-size and is 500px wide. A descendant has padding: 3cqi and margin-block: 2cqb. What does each value resolve to, and why?
Show answer
3cqi is 3% of 500px, which is 15px. 2cqb cannot use the wrapper because inline-size does not provide the block axis. The browser looks at higher ancestors for a container with block size. If there is none, it uses 2svb, which is 2% of the small viewport height.
Exercise
A page has .outer { container: outer / size; width: 80rem; height: 30rem; } and, inside it, .inner { container: inner / inline-size; width: 20rem; }. A child of .inner sets width: 10cqmin;. What is its width?
Show answer
cqi resolves against .inner (20rem), so 10cqi is 2rem. cqb skips .inner and resolves against .outer (30rem), so 10cqb is 3rem. cqmin takes the smaller, so the width is 2rem.
Exercise
You write .hero { container-type: inline-size; font-size: 5cqi; } and the text is sized relative to the window instead of the hero. Why, and how do you fix it?
Show answer
The .hero element is the container, and an element cannot use its own container units. The 5cqi resolves against an ancestor container, or falls back to 5svi if none exists. Move the font-size to a child of .hero, or wrap .hero content in an inner element that carries the font-size.