CSS size units (px rem em vh)

Prefer rem for UI type scales; use px only for hairline borders and pixel-precise details.

Updated:

UnitRelative toTypical use
pxDevice pixel (CSS pixel)Borders, precise icons
remRoot element font-sizeType scale, spacing
emParent font-sizeComponent-local sizing
%Parent propertyWidths, flexible layouts
vw1% viewport widthFull-bleed sections
vh1% viewport heightHero height (mind mobile UI)
vmin / vmaxSmaller/larger viewport sideSquare-ish responsive sizing
chWidth of “0” glyphCode blocks, monospace fields
exx-height of fontRare fine typography
svh / lvh / dvhSmall/large/dynamic viewportMobile browser chrome-safe height

Notes

Frequently asked questions

What is 1rem in px?
1rem equals the root font size, so with the browser default of 16 px it is 16 px.
What is the difference between em and rem?
rem is always relative to the root element’s font size, while em is relative to the current element’s inherited font size.
Why does 100vh not work on mobile?
Mobile browsers may show or hide UI bars, so 100vh can be taller than the visible area; use dvh or svh when supported.