Sub topic 13
Logical units: vi, vb, cqi and cqb
This chapter assumes you know vw, vh, container queries at a basic level, and the block and inline axes from earlier chapters. It covers four units that measure along the inline or block axis instead of the screen's width or height.
Why width and height units are physical
vw is 1% of the viewport width and vh is 1% of the viewport height. Both are tied to the screen. In a vertical writing mode, text runs top to bottom, so the inline axis is vertical. A size set in vw then follows the screen's horizontal direction, not the direction the text flows.
The logical units fix this. They measure along an axis, and the writing mode decides which physical direction that axis points.
| Unit | Percent of | Axis |
|---|---|---|
| `vi` | Viewport size | Inline |
| `vb` | Viewport size | Block |
| `cqi` | Query container size | Inline |
| `cqb` | Query container size | Block |
Viewport units: vi and vb
1vi is 1% of the viewport's size along the inline axis. 1vb is 1% along the block axis. The axes come from the root element's writing mode, not from the element you style.
html {
writing-mode: horizontal-tb;
}
.hero {
inline-size: 100vi;
block-size: 60vb;
}With horizontal-tb on the root, vi equals vw and vb equals vh. If the root used vertical-rl, vi would equal vh and vb would equal vw. Most pages keep a horizontal root, so these units behave like vw and vh until the root's mode changes.
Container units: cqi and cqb
1cqi is 1% of the query container's inline size. 1cqb is 1% of its block size. Here the axes come from the container's own writing mode.
.wrapper {
container-type: inline-size;
}
.wrapper .title {
font-size: clamp(1rem, 4cqi, 2rem);
}The title scales with its container, not the screen. Place the same component in a narrow sidebar or a wide main column and it adjusts. If the container is vertical, cqi measures its height, so the rule still follows the text flow.
Common errors
- Using
cqbwithcontainer-type: inline-size. This type contains only the inline axis. Block size is not available for queries, socqbhas no eligible container and falls back to the small viewport unit (svb). Usecontainer-type: sizeif you needcqb. That type requires you to give the container an explicit block size. - Using
cqiwith no container. With no ancestor that has acontainer-type,cqialso falls back to the small viewport unit (svi). The page still renders, but sizes follow the viewport, which is rarely your intent. - Expecting
vito follow a nested element's writing mode. It follows the root only. For a local vertical block, use a container unit. - Using unclamped values for text.
4cqiin a very narrow container gives tiny text. Wrap it inclamp()as shown above.
Summary
Use vi and vb when you want viewport percentages that follow the document's flow. Use cqi and cqb when a component should respond to its container. Remember that the first pair reads the root's writing mode, the second pair reads the container's, and cqb needs container-type: size.
Exercise
A component has container-type: inline-size and uses height: 50cqb. Does 50cqb measure the container's block size? What should change?
Show answer
No. Inline-size containment makes only the inline axis available, so cqb falls back to the small viewport block unit (svb). Change the container to container-type: size and give it an explicit block size, or use a different unit for the height.