Sub topic 9
Container query units (cqi, cqw, and friends)
This chapter assumes you know core CSS (selectors, the cascade, the box model), basic flexbox or grid, and how to declare a query container with container-type. If a container is not declared yet, the units here will not do what you expect.
What container query units are
Viewport units such as vw and vh are relative to the browser window. Container query units are relative to the size of a query container. A component can then scale with the space it is placed in, not with the window.
| 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` |
In horizontal writing modes such as English, the inline size is the width and the block size is the height. cqi and cqb follow the writing mode, so they are usually the better choice. Use cqw and cqh only when you want physical directions.
Using a unit
The container must exist before a unit can resolve against it. This example makes the heading scale with the wrapper.
.card-wrapper {
container-type: inline-size;
}
.card h2 {
font-size: clamp(1rem, 4cqi, 2rem);
}The heading is 4% of the wrapper's inline size, but never below 1rem or above 2rem. In a 300px wrapper, 4cqi is 12px, so clamp() raises it to 1rem. In a 600px wrapper, 4cqi is 24px, which is inside the range.
Always wrap a container unit in clamp() or min()/max() for text. Without limits, text becomes tiny in narrow containers and huge in wide ones.
Which container a unit uses
A unit resolves against the nearest ancestor that is a query container for the relevant axis. Two details matter.
- The axis must be contained. With
container-type: inline-size,cqiandcqwwork.cqbandcqhneed a container whose block size is also contained, which meanscontainer-type: size. - If no suitable ancestor exists, the unit falls back to the matching small viewport unit. For example,
cqifalls back to1svi. The layout will not break, but the value will no longer track a container, which is a common source of confusion.
Unlike @container rules, units do not use a container name. You cannot ask for a specific named ancestor. They always use the nearest eligible one.
Units outside the container's own rules
The container itself cannot use its own units for its own size, because its size would depend on itself. Units apply to descendants. A unit on the container element resolves against the next container up.
.sidebar {
container-type: inline-size;
}
.widget {
padding: clamp(0.5rem, 3cqi, 1.5rem);
gap: 2cqi;
}Here .widget is a descendant of .sidebar, so both values track the sidebar's width. Moving the widget into a wider region changes its spacing with no extra rules.
Common errors
- No container declared. The unit silently falls back to a viewport unit. Check that an ancestor has
container-type. - Using
cqhorcqbwithinline-size. Block size is not contained, so these units fall back to viewport units. Usecontainer-type: sizeand give the container a definite height. - Unbounded text. A bare
font-size: 5cqiis unreadable in small containers. Clamp it. - Expecting the container to size itself. Set the unit on children, not on the container.
Exercise
A .panel element has container-type: inline-size. Its child .title sets font-size: 6cqb. The panel is 500px wide and 300px tall. What does the browser use for the font size, and how would you fix it so it tracks the panel?
Show answer
cqb needs a container with contained block size, but inline-size only contains the inline axis. The unit therefore falls back to the small viewport block unit (svb), so the size follows the window, not the panel. Fix it by using cqi (6% of 500px is 30px, ideally wrapped in clamp()), or by setting container-type: size on .panel with a definite height.