All articles

px vs rem vs em in CSS

· 3 min read

CSS offers many length units, but three cover most everyday sizing: px, rem and em. The choice matters most for one group of people: those who have increased their browser's default font size because small text is hard for them to read.

The three units

  • px: a fixed CSS pixel. 16px is 16px no matter what.
  • rem: relative to the font size of the root element (<html>). With the default 16px, 1rem = 16px, 1.5rem = 24px.
  • em: relative to the font size of the element itself (for font-size, relative to the parent). It compounds when nested.
html { font-size: 100%; }            /* 16px by default */
.card { font-size: 1.125rem; }       /* 18px */
.card .note { font-size: 0.875em; }  /* 0.875 × 18px = 15.75px */
.card .note { padding: 1em; }        /* 15.75px: em on padding uses the element's own font size */

Why rem matters for accessibility

Browsers let users change the default font size (Chrome: Settings > Appearance > Font size; Firefox: Settings > Fonts). If someone sets it to 20px:

CSS Default (16px) User setting 20px
font-size: 16px 16px 16px (setting ignored)
font-size: 1rem 16px 20px
font-size: 1em (root) 16px 20px

Text in px ignores the user's choice. WCAG 1.4.4 asks that text can be resized to 200% without loss of content; rem-based sizing makes that work with both zoom and font settings.

The em compounding problem

em multiplies through nested elements:

li { font-size: 0.9em; }
<ul><li>Level 1 (14.4px)
  <ul><li>Level 2 (12.96px)
    <ul><li>Level 3 (11.66px)</li></ul>
  </li></ul>
</li></ul>

Each nested list gets smaller. With rem, every level stays at the same size. That predictability is why rem is the usual default for font sizes.

When em is the right choice

em is useful exactly because it follows the local font size. Use it for spacing that should scale with the text of a component:

.button {
  font-size: 1rem;
  padding: 0.5em 1em;     /* grows with the button's font size */
  border-radius: 0.375em;
}
.button.large { font-size: 1.25rem; }  /* padding and radius scale automatically */

Icons sized 1em match the surrounding text, and max-width: 65ch keeps line length readable for any font size.

A practical rule

Use Unit
Font sizes rem
Spacing in layouts (margins, gaps, section padding) rem
Padding and radius that should scale with a component's text em
Borders, hairlines, small shadows px
Media query breakpoints em or rem
Line height Unitless (1.5)
Readable line length ch

Media queries

In media queries, rem and em both refer to the browser's default font size, not to your html font size. Using them makes breakpoints respond to the user's font setting: someone with large text gets the narrower layout sooner, which usually fits better.

@media (min-width: 48em) { /* 768px at default settings */ }

The 62.5% trick

Some projects set the root size so that 1rem equals 10px:

html { font-size: 62.5%; }   /* 10px at default settings */
body { font-size: 1.6rem; }  /* 16px */
.title { font-size: 2.4rem; }/* 24px */

It makes mental math easier and still respects user settings, because it's a percentage. The downsides: every component must set its font size explicitly, and third-party components that assume 1rem = 16px come out too small. With a converter or design tokens, the math isn't needed, and leaving the root at 100% is simpler.

Never set the root to a px value like html { font-size: 10px; }. That overrides the user's setting for the whole site.

Converting px to rem

Divide by the root font size:

rem = px / 16
24px → 1.5rem    14px → 0.875rem    40px → 2.5rem

In Sass or plain CSS:

@use "sass:math";

@function rem($px, $base: 16) { @return math.div($px, $base) * 1rem; }
.title { font-size: calc(24 / 16 * 1rem); }

Tailwind CSS already uses rem for its spacing and font scales: p-4 is 1rem. The px to rem for Tailwind page shows how Tailwind's scale maps to pixels.

Fluid sizing with clamp()

clamp() combines units to scale with the viewport while staying within limits:

h1 { font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3rem); }

Keep a rem component in the middle value: a value based only on vw doesn't respond to the user's font size setting.

Sizing in rem also makes layouts built with flexbox and grid scale with the text inside them.

Frequently asked questions

Isn't browser zoom enough to make px accessible?

Zoom scales px too, but many people set a larger default font size instead of zooming every site. Text sized in px ignores that setting; rem and em respect it.

Should borders be in rem?

Usually not. A 1px border or hairline should stay 1px; scaling it to 1.25px looks blurry. Keep borders, shadows and some fine details in px.

What's the default root font size?

16px in all major browsers, unless the user changes it. So 1rem = 16px by default.

What are rlh, ch and cap?

Other font-relative units: ch is the width of the 0 character (good for line length, e.g. max-width: 65ch), lh/rlh are line heights, and cap is the cap height.

Further reading