BuildUtilities

CSS Units Cheat Sheet

CSS has dozens of length units. Pick the one whose 'relative to' matches the design intent, don't default to px for everything.

Absolute units

pxPixel, fixed size. Use for borders and small fixed offsets.
pt, pc, in, cm, mmPrint units. Rarely needed on the web.

Font-relative units

emRelative to the parent's font size. Compounds, careful with nesting.
remRelative to the root font size. The go-to for typography.
exHeight of the lowercase x, rare.
chWidth of '0', perfect for character-based widths (code blocks).
capCap height, newer, less supported.
lh, rlhLine height of element / root.

Viewport-relative units

vw, vh1% of viewport width/height.
vmin, vmax1% of the smaller / larger viewport dimension.
svh, lvh, dvhSmall/large/dynamic viewport height, handle mobile URL bar correctly.
svw, lvw, dvwSame idea for width.

Percentage & fr

%Relative to the parent's same property.
frFraction of the available space in a grid track.

Special

autoLet the browser decide based on context.
min-content, max-content, fit-contentIntrinsic sizes based on the content.
calc(100% - 16px)Mix units arithmetically.
clamp(min, ideal, max)Responsive sizing without media queries.

Related tools

More references

Tip Jar