liveBook · Manning

Container-relative unit

As explained in Vanilla Web

A container unit is a CSS length unit whose value is calculated from the size of a query container.

Container-relative units measure sizes against the nearest query container, rather than the viewport. They let components scale with the space they occupy and pair naturally with container queries. section 4.1.3 section 4.1.6

Choose the unit that matches the container axis:

  • Use cqw and cqh for widths and heights; use cqi and cqb for inline and block dimensions. section 4.1.6
  • Use these units when component sizing should follow its container rather than the viewport. section 4.1.6
Table 4.1 The most useful modern CSS units
Category Unit Relative to Recommended use

Dynamic viewport

dvh, dvw

1% of the current viewport, adjusting as the browser chrome appears or disappears

Full-height sections that must not jump on mobile when the address bar shows or hides

svh, svw

1% of the small viewport (chrome fully expanded, smallest area)

Guaranteeing content still fits when the browser UI takes the most space

lvh, lvw

1% of the large viewport (chrome fully retracted, largest area)

Sizing against the maximum available area

Logical viewport

vi, vb

1% of the viewport along the inline / block axis

Viewport-based sizing that follows the writing mode automatically

Container

cqi, cqb

1% of the nearest query container’s inline / block size

Components that scale to their container rather than the viewport, paired with container queries

cqw, cqh

1% of the query container’s width / height

Physical container sizing when the axis is fixed regardless of writing mode

Text-relative

lh, rlh

The line height of the element / root element

Vertical rhythm: margins and spacing that align to the baseline grid

cap

The cap height of the current font (height of capital letters)

Headings and display type sized to the actual letter shapes

ch

The advance width of the 0 glyph of the current font

Line lengths, inputs, and columns measured in characters

ic

The advance width of the CJK water ideograph (水)

Consistent sizing for ideographic scripts

Compare container units with other modern CSS unit families and their relative sizing bases. section 4.1.6

How Vanilla Web covers it

How deep it goes
Defines four units and their relative basis, but provides no worked container-unit code example.
Examples
Names cqw, cqh, cqi, and cqb, alongside a card layout that responds to container width.
What it assumes
Assumes a query container for relative sizing and shows container setup with container-type.
What you can do after
Apply container-relative measurements to scale components with their query container.
How it connects
Pairs container units with container queries for responsive component sizing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.