CSS gives you more than a dozen ways to say how big something is. They fall into four families, and knowing which family a unit belongs to tells you what it scales with.

The four families of CSS units

Absolute units

px, pt, cm, in

Fixed sizes that don't scale with font size or viewport. On screens they are all anchored to the CSS pixel (1in = 96px).

px (pixels) · width: 200px;
pt (points) · font-size: 12pt;
cm (centimetres) · width: 5cm;
in (inches) · padding: 0.5in;
mm (millimetres) · margin: 10mm;

Relative units

%, em, rem

Relative to another length: the parent, the font size or the root font size. Essential for responsive design.

% (percentage) · width: 50%;
em (element font size) · font-size: 1.5em;
rem (root em) · padding: 2rem;
ch (width of "0") · width: 20ch;
ex (x-height) · height: 2ex;

Viewport units

vw, vh, vmin, vmax

Relative to the size of the browser viewport. Perfect for full-screen layouts.

vw (viewport width) · width: 50vw;
vh (viewport height) · height: 100vh;
vmin (smaller side) · font-size: 5vmin;
vmax (larger side) · padding: 2vmax;
dvh (dynamic height) · min-height: 100dvh;

Flexible sizing

fr, min-content, max-content

Used in CSS Grid and Flexbox to build flexible, responsive layouts.

fr (fraction) · grid-template-columns: 1fr 2fr;
min-content · width: min-content;
max-content · width: max-content;
fit-content · width: fit-content(200px);
auto · margin: auto;

Live unit demonstrations

Resize the window: the fixed box never changes, the others react to their parent, the viewport or the root font size.

200px (fixed)
50% (of the parent)
20vw (20% of the viewport width)
10rem (10 × root font size)

Unit comparison table

UnitTypeRelative toUse caseExample
pxAbsoluteCSS reference pixelFixed sizes, bordersborder: 1px solid #000;
%RelativeParent elementLayout widths, heightswidth: 80%;
emRelativeParent font-sizeTypography scalingfont-size: 1.2em;
remRelativeRoot element font-sizeConsistent typographypadding: 1.5rem;
vwViewportViewport widthFull-width elementswidth: 100vw;
vhViewportViewport heightFull-height sectionsheight: 100vh;
vminViewportSmaller viewport dimensionMobile-friendly textfont-size: 4vmin;
vmaxViewportLarger viewport dimensionLarge-screen spacingpadding: 2vmax;
frFlexibleFree space in the gridCSS Grid layoutsgrid-template-columns: 1fr 2fr;

Detailed unit explanations

Viewport units (vh, vw, vmin, vmax)

vh (viewport height): 1vh = 1% of the viewport height
vw (viewport width): 1vw = 1% of the viewport width
vmin: 1% of the viewport's smaller dimension
vmax: 1% of the viewport's larger dimension

Example: an element with height: 50vh is half the height of the viewport, regardless of scrolling. On mobile, where toolbars grow and shrink, use dvh (dynamic), svh (smallest) or lvh (largest) for predictable results.

Pixel (px) vs relative units

px is a reference pixel, not a device pixel: on a high-density screen one CSS pixel covers several physical pixels.
Relative units adapt to the reader's font-size preference and to the layout around them.

em vs rem

em is relative to the font-size of the element's parent (and compounds when nested).
rem is always relative to the root (html) element's font-size.

Example: if the html font-size is 16px, then 1rem = 16px everywhere, but 1em can differ in each context.

When to use which unit

  • Layouts: %, fr, vw/vh
  • Typography: rem, em
  • Borders and shadows: px
  • Full-screen elements: vh/dvh, vw
  • Grid systems: fr, %
  • Print styles: cm, in, pt

Pro tip: combine units. width: min(1000px, 90vw) creates a responsive container that is never wider than 1000px or 90% of the viewport.