Overview
rem resolves against the root (<html>) font size and em against the element’s own font size (for font-size itself, the parent’s). Size type and spacing in rem by default so nested components stay predictable and user font settings scale the page. Use em for padding and icon sizes that should track a component’s own text size.
Rules
emcompounds: a1.2emchild inside a1.2emparent renders at 1.44 times the root. That is usually the bug.- Set
html { font-size: 100%; }, not a pixel value, so the browser default (16px) and user preferences are respected. - Use
pxfor borders and shadows. Avoid it for text and spacing that should scale with the user’s font size.
html { font-size: 100%; }
h1 { font-size: 2rem; } /* 2 x root, at any depth */
.btn { font-size: 0.875rem; padding: 0.5em 1em; } /* padding tracks the button's text */Related
- box-model: unit choices feed box dimensions.
- custom-property: share
remtokens across components. - viewport:
vwanddvhare the viewport-relative units. - css-units: all CSS units at a glance.
- css: CSS conventions.