Sub topic 9
Container query units (cqi, cqw, and friends)
Container queries let you switch styles at a size threshold. Container query units let you scale values continuously with the container instead. This chapter covers the six units, how each one finds its container, and the pitfalls of using them for fluid sizing.
This chapter assumes you can already declare a query container with container-type, write a basic @container rule, and use clamp() and calc(). It also assumes you know viewport units such as vw and vh.
What the units measure
A container query unit is a length that equals a percentage of a query container's size. One unit is 1% of the relevant dimension.
| Unit | 1 unit equals | Axis |
|---|---|---|
| `cqw` | 1% of the container's width | Physical |
| `cqh` | 1% of the container's height | Physical |
| `cqi` | 1% of the container's inline size | Logical |
| `cqb` | 1% of the container's block size | Logical |
| `cqmin` | The smaller of `cqi` and `cqb` | Either |
| `cqmax` | The larger of `cqi` and `cqb` | Either |
They parallel the viewport units vw, vh, vi, vb, vmin, and vmax. The only change is that the reference box is a container instead of the viewport.
In horizontal writing modes, cqi equals cqw and cqb equals cqh. In a vertical writing mode, such as writing-mode: vertical-rl, inline size runs top to bottom, so cqi follows the container's height. Prefer the logical units (cqi, cqb) unless you need a physical direction. Your component then keeps working if the writing mode changes.
A first example
.card-wrapper {
container-type: inline-size;
}
.card h2 {
font-size: clamp(1rem, 4cqi, 2rem);
}The wrapper is a query container. Inside it, the h2 is 4% of the wrapper's inline width, limited to between 1rem and 2rem. A wrapper 300px wide gives 12px, which clamp() raises to 1rem. A wrapper 600px wide gives 24px, which is within bounds. A wrapper 900px wide gives 36px, which is capped at 2rem.
The same card placed in a narrow sidebar and in a wide main column therefore gets different heading sizes. The viewport width has no effect.
How a unit finds its container
A container query unit resolves against the nearest ancestor that is a query container and contains the axis the unit needs. The browser walks up from the element and takes the first ancestor that qualifies.
Which axes a container contains depends on its container-type:
inline-sizecontains the inline axis only.cqiworks.cqbdoes not.sizecontains both axes. All six units work.normalmakes the element not a size container. It is skipped.
When no ancestor qualifies for an axis, the unit falls back to the small viewport unit for that axis. So cqi becomes svi, cqw becomes svw, and cqmin becomes svmin. The page does not break, but the value silently stops following any container.
Names do not apply to units
An @container rule can target a container by name. Units cannot. There is no syntax like 4cqi(sidebar). A unit always uses the nearest eligible ancestor, even if a differently named container sits further up.
.page {
container: page / inline-size;
}
.sidebar {
container: sidebar / inline-size;
}
.widget h3 {
/* Measures .sidebar, the nearest eligible ancestor, not .page. */
font-size: 5cqi;
}If you want .widget to scale with .page, the nearest container must be .page. You can restructure the markup, or compute the value on an element that sits directly under .page and pass it down through a custom property.
An element does not measure itself
A container's own properties resolve against its ancestors, never against itself. This is the most common surprise with these units.
.card-wrapper {
container-type: inline-size;
/* Measures an ancestor container, or the small viewport if none exists. */
padding: 2cqi;
}
.card {
/* Measures .card-wrapper, as intended. */
padding: 2cqi;
}The wrapper's padding cannot depend on the wrapper's own width, because the container's size would then depend on its padding. Put the unit on a child element. If you need an extra layer, add an inner element.
Where to use the units
Container units are lengths, so they work anywhere a length does. The common uses are:
- Typography.
font-size,line-height, andletter-spacing. - Spacing.
padding,margin, andgap. - Sizing.
width,max-width,border-radius, and icon sizes. - Math. Inside
calc(),clamp(),min(), andmax().
They also work in custom properties, but resolution happens where the property is used. A custom property that holds 5cqi is a token. It is converted to pixels on the element that uses it, so the nearest-container rule applies at the point of use. Declaring the property on a container does not make it measure that container.
.card-wrapper {
container-type: inline-size;
}
.card {
--gap: clamp(0.5rem, 3cqi, 1.5rem);
display: grid;
gap: var(--gap);
}
.card .meta {
/* --gap is re-evaluated here, against the same nearest container. */
margin-block-start: var(--gap);
}Here both .card and .card .meta end up measuring .card-wrapper. The value would differ only if a nearer container sat between them.
Fluid sizing without breakpoints
Units and queries solve different problems. A query changes the layout at a threshold. A unit makes values change smoothly between thresholds. A good component usually uses both: a query switches from a single column to a grid, and units keep type and spacing proportional in each state.
.card-wrapper {
container-type: inline-size;
}
.card {
display: grid;
gap: clamp(0.75rem, 3cqi, 1.5rem);
padding: clamp(1rem, 4cqi, 2rem);
}
.card h2 {
font-size: clamp(1.125rem, 1rem + 2.5cqi, 2rem);
}
@container (min-width: 480px) {
.card {
grid-template-columns: 8rem 1fr;
}
}The clamp() calls matter. An unclamped 5cqi becomes 5px in a 100px container and 100px in a 2000px container. A minimum keeps text readable, and a maximum stops it growing without limit.
Keep text zoomable
A font size written only in container units ignores the user's preferred font size. Browser text-size settings and zoom behavior depend on a rem or em part in the value. Mix the two, as in calc(1rem + 1.5cqi), so the base scales with user settings and the container only adds a proportional boost.
.card h2 {
font-size: clamp(1.125rem, calc(1rem + 1.5cqi), 2rem);
}Compare the two forms in a browser. Raise the default font size in the browser settings and check whether the heading follows. A pure cqi value does not follow it. The mixed value does.
Block-axis units need `container-type: size`
cqb and cqh need a container that contains the block axis. That means container-type: size, and with it a height that does not depend on its content.
.panel {
container-type: size;
height: 24rem;
}
.panel .badge {
width: 10cqmin;
height: 10cqmin;
}cqmin here is the smaller of the panel's width and height, so the badge stays square and fits whichever side is shorter. If the panel had no explicit height, a size container would collapse to zero block size and the units would all resolve to zero. With container-type: inline-size, cqb and cqmin would not find an eligible container and would use the small viewport fallback.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Value follows the viewport, not the container | No eligible ancestor, so the unit fell back to `svi` or `svw` | Add `container-type` to an ancestor |
| Value on the container itself is wrong | The element measures its ancestors, not itself | Move the unit to a child |
| `cqb` or `cqh` ignores the container | The container is `inline-size`, which does not contain the block axis | Use `container-type: size` and give it a definite height |
| Wrong container is measured | A nearer container exists, and units cannot use names | Restructure so the intended container is nearest |
| Text ignores browser font settings | Font size uses only container units | Combine with `rem` using `calc()` or `clamp()` |
| Heading becomes tiny or huge | No `clamp()` bounds | Add a minimum and maximum |
.card h2 {
font-size: 1.5rem;
font-size: clamp(1.125rem, calc(1rem + 1.5cqi), 2rem);
}Browsers without support ignore the second declaration, so they keep 1.5rem.
Exercise
A page has .page { container-type: inline-size; }. Inside it, .panel { container-type: inline-size; } holds a .title with font-size: 6cqi. The .page is 1000px wide and .panel is 400px wide. What is the computed font size of .title, and what would it be if .title itself were the element with container-type: inline-size?
Show answer
The font size is 24px, because 6cqi resolves against the nearest eligible ancestor, .panel, and 6% of 400px is 24px. If .title were itself the container, its own cqi would still measure its ancestors, so it would still use .panel and the result would remain 24px.
Exercise
You want a square badge whose side is 12% of the smaller dimension of its parent, and the parent has container-type: inline-size. The badge stays the wrong size. Name the problem and give the fix.
Show answer
cqmin needs a container that contains both axes, and an inline-size container does not contain the block axis. The unit falls back to svmin. Change the parent to container-type: size, give it a definite height, and use width: 12cqmin; height: 12cqmin; on the badge.