Sub topic 13
Logical units: vi, vb, cqi and cqb
This chapter assumes you know the block and inline axes, writing-mode and direction, and how inline-size and block-size replace width and height. It also assumes you have used vw, vh and basic container queries. Here you will learn the length units that follow the same axes: vi, vb, cqi and cqb.
Why units need logical versions
Properties such as margin-inline-start follow the writing mode. Units like vw and vh do not. They always mean the physical width and height of the viewport. Suppose a vertical Japanese layout sets inline-size: 50vw. The inline axis now runs top to bottom, so half the viewport's width is applied along the vertical direction. The result is rarely what you intended.
Logical units fix this. You name an axis (inline or block), and the browser picks the physical dimension.
| Physical unit | Logical unit | Meaning |
|---|---|---|
| `vw` | `vi` | 1% of the viewport along the inline axis |
| `vh` | `vb` | 1% of the viewport along the block axis |
| `cqw` | `cqi` | 1% of the query container's inline size |
| `cqh` | `cqb` | 1% of the query container's block size |
Viewport units: vi and vb
1vi is 1% of the viewport size along the inline axis. 1vb is 1% along the block axis. In a horizontal writing mode, vi equals vw and vb equals vh. In a vertical writing mode, they swap.
The important detail is which writing mode is used. For vi and vb, the browser reads the writing mode of the root element (html). It does not read the writing mode of the element where you use the unit. Setting writing-mode on a nested section does not change what vi means inside it.
html {
writing-mode: vertical-rl;
}
.page {
/* The root is vertical, so 1vi equals 1vh and 1vb equals 1vw. */
inline-size: 90vi;
min-block-size: 60vb;
padding-inline: 5vi;
}Here .page takes 90% of the viewport height as its inline size, because the inline axis is vertical. With vw and vh, you would have to rewrite the rule whenever the document's writing mode changed.
Small, large and dynamic variants
Mobile browsers show and hide toolbars, so the viewport size can change as the user scrolls. CSS has three variants of every viewport unit, and the logical units have them too.
| Variant | Inline | Block | Viewport size used |
|---|---|---|---|
| Small | `svi` | `svb` | Toolbars visible (smallest viewport) |
| Large | `lvi` | `lvb` | Toolbars hidden (largest viewport) |
| Dynamic | `dvi` | `dvb` | Current size, updates as toolbars move |
The plain vi and vb behave like the large variants. A full-height hero section on a phone usually wants the dynamic unit, with a plain fallback for older browsers:
.hero {
min-block-size: 100vb;
min-block-size: 100dvb;
}A browser that does not understand dvb drops the second declaration and keeps the first.
Container units: cqi and cqb
Container query units size things relative to an ancestor container instead of the viewport. To use them, an ancestor must set container-type. The unit then resolves against the nearest suitable ancestor.
.wrapper {
container-type: inline-size;
}
.wrapper .title {
font-size: clamp(1rem, 4cqi, 2rem);
}The .title is 4% of the wrapper's inline size, kept between 1rem and 2rem. In a horizontal layout the wrapper's width drives the size. In a vertical layout its height does. The CSS stays the same.
Which container qualifies
Eligibility is decided separately for each axis.
container-type: inline-sizemakes the element a container for the inline axis only.cqiworks.cqbdoes not.container-type: sizemakes the element a container for both axes. Both units work.container-type: normal(the default) does not make the element a size container, so it provides neither unit.
If no ancestor qualifies for the axis, the unit falls back to the small viewport size for that axis. So cqb inside an inline-size container does not fail. It quietly becomes 1svb. This is a common source of confusing results.
The container cannot size itself
A container's own properties cannot use its own container units. The units resolve against ancestors. Only descendants see the container. Put the unit on a child, as in the .wrapper .title example. If you write font-size: 4cqi on the container itself, it resolves against a higher container, or against the small viewport.
Using both axes
The block size of a size container does not come from its content. The element gets size containment on both axes, so it needs an explicit block-size or something else that sets it. Otherwise it collapses to zero height.
.panel {
container-type: size;
inline-size: 30rem;
block-size: 20rem;
}
.panel .label {
padding-block: 2cqb;
padding-inline: 3cqi;
}With inline-size containment you avoid this problem, because block size still comes from content. For that reason, inline-size is the usual choice, and cqb is used less often than cqi.
Choosing between viewport and container units
| Goal | Use |
|---|---|
| Full-screen section or page-wide spacing | `vi`, `vb` (or `dvb` on mobile) |
| Component that scales with the space it is placed in | `cqi`, `cqb` |
| A size tied to the screen even in a vertical document | `vi`, `vb` |
| A literal screen width or height regardless of writing mode | `vw`, `vh` |
Container units make components portable. A card in a sidebar and the same card in the main column each scale to their own container. Viewport units cannot do that, because both cards see the same viewport.
Common errors
- Expecting
vito follow a nested writing mode. It follows the root. If only one section is vertical,viinside it still maps to the root's inline axis. - Forgetting
container-type. The unit still computes, but against the small viewport. The text size or spacing looks like it ignores the container. - Using
cqbin aninline-sizecontainer. It falls back tosvb. Switch tocontainer-type: sizeand give the container an explicit block size, or avoidcqb. - Using the unit on the container itself. It resolves against an ancestor, not the element.
- Collapsed
sizecontainers. Acontainer-type: sizeelement with no set block size has zero block size.
Support and fallbacks
Container query units are supported in current versions of all major browsers (since early 2023). The logical viewport units arrived at different times in different browsers. Check a compatibility table for the browsers you must support. For progressive enhancement, put a safe value first and override it inside @supports:
.title {
font-size: 1.5rem;
}
@supports (font-size: 1cqi) {
.title {
font-size: clamp(1rem, 4cqi, 2rem);
}
}The @supports test works because a browser that does not know cqi treats the declaration as invalid. The fallback size stays in effect.
Summary
viandvbare viewport percentages along the root element's inline and block axes. They matchvwandvhin horizontal writing modes and swap in vertical ones.svi,lvi,dviand their block counterparts handle changing mobile toolbars.cqiandcqbare percentages of the nearest eligible container's inline and block sizes. A container must setcontainer-type, and eligibility is per axis.- With no eligible container, the unit falls back to the small viewport.
Exercise
A page has html { writing-mode: vertical-rl; }. A .box sets inline-size: 50vi. Separately, a .wrapper has container-type: inline-size, and a child of it uses padding-block: 5cqb. What physical viewport dimension does 50vi refer to, and what does 5cqb resolve against?
Show answer
Because the root is vertical, the inline axis is vertical, so 50vi is 50% of the viewport height (the same as 50vh). The wrapper is only an inline-size container, so it is not eligible for the block axis. 5cqb therefore falls back to 5% of the small viewport block size (5svb), which in this vertical document is 5% of the viewport width. To make cqb resolve against the wrapper, use container-type: size and give the wrapper an explicit block size.