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.


_14
html {
_14
/* leave the root alone so the user's preference wins */
_14
}
_14
_14
h1 {
_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.

  • px means "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.
  • em means "multiples of the current font size". On most properties, 1em equals the computed font-size of the same element. On the font-size property itself, 1em refers to the parent's font size, because the element's own size is the thing being worked out.
  • rem means "root em": multiples of the font size on the html element. Every rem on 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.


_10
ul {
_10
font-size: 1.2em; /* nested lists grow each level */
_10
}
_10
_10
ul {
_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 html to a px font size, such as font-size: 16px. This overrides the user's browser preference for the whole site.
  • Using em for font sizes on nested components and getting text that grows or shrinks with each level.
  • Media queries: em and rem in a media query both refer to the browser's initial font size, not your html rule. Using them there is fine and makes breakpoints follow the user's text size.
  • Assuming px equals 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.
#
file under:
CSS

quick question

Was this resource helpful?