<length>

A CSS data type representing a distance value. It consists of a number followed by a unit (like 'px', 'em', 'rem', 'vw', etc.). Lengths fall into two big buckets: absolute units that map to physical sizes, and relative units that resolve based on context (the font, the viewport, the container, the line height).

Quick example

h1 {
  /* A few common lengths */
  font-size: 2.5rem;
  margin-top: 1lh;
  width: 50cqi;
  min-height: 100dvh;
}

All units

Font-relative units

Resolve based on the font of the current element or the root element.

UnitMeasuresNotesBaseline
emCurrent element's font sizeCompounds when nested. The most common pitfall: an em on a child resolves against the child's own font-size if it's been set.Widely
remRoot element's font sizeDoesn't compound. Default for typography and spacing in modern stylesheets.Widely
exx-height of the current fontTop of a lowercase 'x' to baseline. Rarely used outside font-fitting hacks.Widely
rexx-height of the root fontRoot-level version of ex.Newly (2024)
capCap height of the current fontBaseline to top of an uppercase letter. Useful for aligning icons to the visual top of text.Newly (2024)
rcapCap height of the root fontRoot-level version of cap.Newly (2024)
chWidth of the '0' (zero) characterBest for setting reading-width limits on prose: max-width: 65ch.Widely
rchWidth of '0' in the root fontRoot-level version of ch.Newly (2024)
icAdvance width of the CJK ideograph 水Sizing CJK columns of vertical Japanese or Chinese text.Newly (2024)
ricAdvance width of CJK 水 in the root fontRoot-level version of ic.Newly (2024)
lhComputed line height of the current elementVertical rhythm tied to type. Resolves to a real pixel value usable for any length.Newly (2024)
rlhComputed line height of the root elementRoot-level version of lh.Newly (2024)

Viewport-relative units

Each axis has four variants: the original (vw, vh), and small (svw, svh), large (lvw, lvh), and dynamic (dvw, dvh) versions that account for mobile browser chrome.

UnitMeasuresNotesBaseline
vw1% of viewport widthOn mobile, returns the largest viewport width regardless of address bar state.Widely
vh1% of viewport heightSame caveat: ignores the mobile address bar. Use dvh for mobile-safe full-screen layouts.Widely
vi1% of viewport inline axisWriting-mode aware version of vw.Widely
vb1% of viewport block axisWriting-mode aware version of vh.Widely
vmin1% of the smaller of vw or vhUseful for sizing that should never exceed the smaller viewport edge.Widely
vmax1% of the larger of vw or vhInverse of vmin.Widely
svw / svh / svi / svbSmall viewport (browser chrome visible)Returns the viewport size with mobile bars showing. Stable, won't jump on scroll.Newly (2023)
lvw / lvh / lvi / lvbLarge viewport (browser chrome hidden)Returns the viewport size with mobile bars hidden.Newly (2023)
dvw / dvh / dvi / dvbDynamic viewport, follows chrome state in real timeWhat you usually want for full-screen mobile elements. The element grows and shrinks with the address bar.Newly (2023)
svmin / lvmin / dvminvmin variants for small / large / dynamic viewportSame idea as vmin, with mobile-aware viewports.Newly (2023)
svmax / lvmax / dvmaxvmax variants for small / large / dynamic viewportSame idea as vmax, with mobile-aware viewports.Newly (2023)

Container-relative units

Resolve against the nearest ancestor with container-type set. Without one, they fall back to viewport-relative behavior.

UnitMeasuresNotesBaseline
cqw1% of query container's widthRequires container-type: inline-size or size on a parent.Newly (2023)
cqh1% of query container's heightRequires container-type: size on a parent.Newly (2023)
cqi1% of query container's inline sizeWriting-mode aware. Most useful unit of the group for typography that scales with a component.Newly (2023)
cqb1% of query container's block sizeWriting-mode aware version of cqh.Newly (2023)
cqmin1% of the smaller of cqi or cqbContainer-relative version of vmin.Newly (2023)
cqmax1% of the larger of cqi or cqbContainer-relative version of vmax.Newly (2023)

Absolute units

Map to physical sizes. On screens, all of these are anchored to px.

UnitMeasuresNotesBaseline
px1 CSS pixel (1/96 of an inch)Despite the name, not always one device pixel. The fundamental absolute unit on the web.Widely
cm1 centimeter (= 96/2.54 px)Mostly useful for print stylesheets.Widely
mm1 millimeter (= 1/10 cm)Print stylesheets.Widely
QQuarter-millimeter (= 1/40 cm)Print typography. Rare on the web.Widely
in1 inch (= 96 px)Print stylesheets.Widely
pt1 point (= 1/72 of an inch)Standard typographic unit in print.Widely
pc1 pica (= 12 pt)Print typography. Rarely seen on the web.Widely

<length> Browser Support

Widely availableSince 201596% global usage

This feature is well established and works across many devices and browser versions. It has been available across browsers since 2015.

Safe to use without fallbacks.

1+
1+
1+
12+
ESC