CodeOath
← All posts
HTML & CSS70 min total · 18 parts

CSS Fundamentals: The Box Model, Specificity, Positioning, and Layout

Contents — Part 11 of 18: Units: px, %, em, rem, and Viewport Units
Part 11 of 18 · ~1 min

Units: px, %, em, rem, and Viewport Units

CSS has several length units, and picking the wrong one is a common source of layouts that don't scale or don't respect user font-size preferences.

UnitRelative toCommon use
pxNothing — an absolute (device) pixelBorders, fine-grained values that shouldn't scale with text
%The relevant dimension of the containing blockFluid widths, max-width
emThe current element's font-size (or, for font-size itself, the parent's font-size)Spacing that should scale with a specific element's text size
remThe root (<html>) element's font-size, alwaysMost font-size and spacing values — predictable regardless of nesting
vw / vh1% of the viewport's width / heightFull-bleed hero sections, typography that scales with screen size

em compounds when nested, which is the classic em gotcha:

.parent { font-size: 20px; }
.child  { font-size: 1.5em; }   /* 30px — 1.5 × parent's 20px */
.grandchild { font-size: 1.5em; } /* 45px — 1.5 × child's 30px, NOT 1.5 × the root */

rem avoids this entirely by always referring to the root font-size, no matter how deeply nested the element is — which is why most component libraries default to rem for font sizes and spacing, reserving em for the rare case where you specifically want a value to scale relative to its own element's font-size (like padding on a button that should grow if the button's own text size changes).