CSS Units: px vs rem vs em (and When to Use Each)

5 min read · Published 1 November 2026 · Updated 2 November 2026

Choosing between px, rem and em looks like a small styling detail, but it decides whether your layout respects the font size a visitor has chosen in their browser. This guide explains what each unit is relative to, how to convert between them, and a few rules that keep a design flexible and accessible.

px: the CSS pixel

A px in CSS is a fixed reference unit, not a physical screen dot. Browsers scale it to the display, so a 16px font looks about the same size on a high-resolution phone and a regular monitor. A value in px does not change when the user increases their default font size in the browser settings. That predictability is good for borders, shadows and thin lines, and bad for text that people may need to enlarge.

rem: relative to the root

rem means "root em": it is a multiple of the font size of the root element (html). The default root font size in browsers is 16px, so 1rem is normally 16px, 1.5rem is 24px and 0.875rem is 14px. If the user changes their default font size, everything sized in rem scales with it. Because it always refers to the same root value, rem is predictable.

em: relative to the current element

em is relative to the font size of the element it is used on (or, for the font-size property itself, the parent). That is powerful for components that should scale together, such as padding on a button that follows the button's text size. It also compounds: if a list inside a list inside a list each uses font-size: 0.9em, the text keeps shrinking at every level.

Converting between px and rem

Divide pixels by the root font size to get rem, and multiply to go back. With a 16px root:

Quick table (root = 16px)

12px = 0.75rem     20px = 1.25rem
14px = 0.875rem    24px = 1.5rem
16px = 1rem        32px = 2rem
18px = 1.125rem    48px = 3rem

Why accessibility matters

Many people set a larger default font size in their browser or operating system. Text sized in rem respects that choice, while text locked in px may not follow it. Browser zoom works with both, but respecting the default size is part of building pages that are comfortable to read for everyone, and it is a recommendation of accessibility guidelines.

Simple rules that work

  • Use rem for font sizes and for spacing that should grow with text (margins, padding, gaps, max widths of text blocks).
  • Use em for properties of a component that should scale with that component's own text size.
  • Use px for hairline borders, outlines and shadows where an exact size is wanted.
  • Use unitless values for line-height (1.5 rather than 24px) so that it scales with the font size.
  • Use percentages and viewport units (vw, vh) for fluid layout widths, but be careful with text sized only in vw, which ignores user preferences.

Common mistakes

  • Setting html { font-size: 10px } to make the maths easy. It overrides the user's default size. If you want easier maths, use a percentage such as 62.5% knowing that it also shrinks the default, or simply learn that 1rem is 16px.
  • Nesting em values without noticing the compounding effect.
  • Using px in media queries for breakpoints when the layout should respond to larger default font sizes. Breakpoints in em or rem follow the user's font size setting.

Fluid text with clamp()

The CSS clamp() function lets a size grow with the screen but never go below a minimum or above a maximum. For example, font-size: clamp(1rem, 0.8rem + 1vw, 1.5rem) keeps text at least 1rem, scales with the viewport width in between, and stops at 1.5rem. Using rem for the limits keeps the text responsive to the user's font setting, while the viewport unit provides the smooth growth.

A small spacing scale in rem

Defining spacing once as a scale keeps a layout consistent, and because the values are in rem the whole layout grows proportionally if a visitor increases their default text size.

CSS variables

:root {
  --space-1: 0.25rem;  /* 4px  */
  --space-2: 0.5rem;   /* 8px  */
  --space-3: 1rem;     /* 16px */
  --space-4: 1.5rem;   /* 24px */
  --space-5: 2rem;     /* 32px */
}

Frequently asked questions

Is rem supported in all browsers?
Yes. rem has been supported by every current major browser for many years, so you can use it without a fallback.
Does browser zoom change rem values?
Zoom scales the whole page, including px values, so both px and rem layouts get bigger when you zoom. The difference is that a larger default font size set in browser settings changes rem values but does not change px values.
Why do vh units behave oddly on phones?
On mobile browsers the address bar can appear and disappear, changing the visible height. Newer units such as dvh (dynamic viewport height) follow the currently visible area and often work better for full-height layouts.
Should media queries use px, em or rem?
em or rem are better, because the breakpoints then follow the user's font size setting. In media queries these units are based on the browser default font size, not on the html element size you set in your CSS.

Sources and further reading

Try it yourself: Unit & Currency Converter runs in your browser, free and with no sign-up.

More guides