The short answer
#px is an absolute CSS pixel. em is relative to the font size of the element it is used on (or its parent, when used for font-size itself). rem is relative to the font size of the root html element, which is 16px by default. Use rem for font sizes and most spacing so text respects the user's browser settings, em when something should scale with its own text, and px for things like hairline borders.
_14html {_14 /* leave the root alone so the user's preference wins */_14}_14_14h1 {_14 font-size: 2rem; /* 2 x root size, usually 32px */_14 margin-block: 1.5rem;_14}_14_14.button {_14 font-size: 1rem;_14 padding: 0.5em 1em; /* scales with the button's own text */_14 border: 1px solid currentColor;_14}
Why it happens
#The three units answer different questions.
pxmeans "this many CSS pixels". A CSS pixel is not a physical screen pixel; on a high-density display one CSS pixel may be two or three device pixels. It does not change when the user picks a larger default font size in their browser settings.emmeans "multiples of the current font size". On most properties,1emequals the computedfont-sizeof the same element. On thefont-sizeproperty itself,1emrefers to the parent's font size, because the element's own size is the thing being worked out.remmeans "root em": multiples of the font size on thehtmlelement. Everyremon the page points at the same value, so it does not compound.
The compounding of em is the classic surprise. If a list sets font-size: 1.2em and you nest lists three deep, the innermost text ends up at 1.2 x 1.2 x 1.2, about 1.73 times the base size.
_10ul {_10 font-size: 1.2em; /* nested lists grow each level */_10}_10_10ul {_10 font-size: 1.2rem; /* every list is the same size */_10}
The accessibility point matters too. Many people raise the default font size in their browser rather than zooming the whole page. Font sizes in rem or em follow that setting; font sizes in px ignore it. Page zoom scales everything, including px, so the difference only shows with the font size setting.
How to fix it
#Use rem for font sizes and layout spacing
A type scale and spacing scale in rem keeps everything proportional to the user's chosen base size, with no compounding.
_12:root {_12 --space-1: 0.25rem;_12 --space-2: 0.5rem;_12 --space-4: 1rem;_12 --text-sm: 0.875rem;_12 --text-lg: 1.25rem;_12}_12_12.card {_12 padding: var(--space-4);_12 font-size: var(--text-sm);_12}
Use em for things tied to their own text
Padding on buttons, the size of an inline icon and the gap after a heading often look best when they scale with the text next to them. With em, a small button and a large button keep the same proportions without separate padding values.
_10.icon {_10 width: 1em;_10 height: 1em;_10 vertical-align: -0.125em;_10}_10_10.button--large {_10 font-size: 1.25rem; /* padding in em grows with it */_10}
Use px where scaling would look wrong
Borders, outlines, box shadows and other fine details usually should stay crisp at a fixed size. A 1px border that becomes 1.25px at a larger font size can render blurry.
Do not shrink the root to make maths easier
An old trick sets html to font-size: 62.5% so that 1rem equals 10px. It works, but it means every component has to set a font size or the text is tiny, and third-party widgets that assume a 16px root break. If you want easier maths, write rem values with a comment, or use custom properties.
Common pitfalls
#- Setting
htmlto apxfont size, such asfont-size: 16px. This overrides the user's browser preference for the whole site. - Using
emfor font sizes on nested components and getting text that grows or shrinks with each level. - Media queries:
emandremin a media query both refer to the browser's initial font size, not yourhtmlrule. Using them there is fine and makes breakpoints follow the user's text size. - Assuming
pxequals device pixels. On high-density screens it does not, so a 1px border is still a thin line, not an invisible one. - Mixing units at random. Pick a rule (for example rem for layout, em for components, px for borders) and stick to it.
Related reading
#CSSquick question
Was this resource helpful?