Skip to content
Built in Lahore, PakistanHow we check the math
mathbehind

PX vs REM in CSS: When to Use Each and How to Convert

Rem sizes follow the reader's font settings; pixel sizes do not. Here is how the conversion works and where each unit makes sense.

By Muhammad Ahmad, Software EngineerLinkedIn· figures checked

Published , updated . 4 min read.

Want to run your own numbers?PX to REM ConverterOpen the calculator

Designs arrive in pixels, but many style guides recommend writing CSS in rem. The reason is accessibility: rem sizes respect the reader's browser settings, and pixel sizes do not.

What a rem is

One rem equals the font size of the page's root html element. Browsers set that to 16 pixels by default. If someone increases the default font size in their browser to make text easier to read, every size written in rem grows with it.

Converting

To turn pixels into rem, divide by the root font size. To turn rem into pixels, multiply.

Common conversions at a 16px root
  1. Body text

    16px ÷ 16pxequals1rem

  2. Small text

    14px ÷ 16pxequals0.875rem

  3. A heading

    24px ÷ 16pxequals1.5rem

The 62.5% trick

Some projects set html { font-size: 62.5% }, which makes 1rem equal 10 pixels, so 24px becomes an easy 2.4rem. It works, but it means you have to set a readable font size on the body again, and third-party components that assume 16px can come out too small. Many teams now keep the default and use a converter instead.

When to use which

  • Font sizes: rem, so text respects the reader's settings.
  • Spacing and layout widths: rem is a good default, so layouts scale sensibly with text.
  • Borders and fine details: pixels are fine, since a 1px line should stay 1px.
  • Media queries: em or rem, so breakpoints move when text size changes.

Rem vs em

Em is relative to the font size of the element's parent, not the root. That is useful for things that should scale with their own text, like padding inside a button, but it compounds when elements are nested, which can make sizes hard to predict. Rem always means the same thing anywhere on the page.

Why rem matters for accessibility

Browsers let people change their default text size in settings, and many people with low vision do. Text sized in rem follows that setting: if someone sets their default to 20px, 1rem becomes 20px everywhere. Text sized in px ignores it. Page zoom scales both, but the font-size setting only works with relative units. The Web Content Accessibility Guidelines ask that text can be resized up to 200% without losing content or function (success criterion 1.4.4), and rem-based layouts make that far easier to meet.

Quick conversion table (16px base)

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

Media queries use the browser default, not your root size

One detail surprises people. In media queries, rem and em are based on the browser's initial font size, not on any font-size you set on the html element. So @media (min-width: 48rem) means 768px at the default setting, even if you used the 62.5% trick. The upside is that breakpoints in rem also respond to a user's larger text setting, so the layout switches to a roomier version when text gets bigger.

Fluid type with clamp()

Modern CSS lets text scale smoothly between a minimum and a maximum using clamp(). For example, font-size: clamp(1rem, 0.9rem + 0.5vw, 1.25rem) never goes below 1rem or above 1.25rem, and grows with the viewport in between. Keep the minimum and maximum in rem so the user's text setting still applies; a value made purely of vw units would ignore it.

What to keep in px

  • Hairline borders (1px) and fine dividers, which should stay crisp at any text size.
  • Box shadows and small decorative offsets.
  • Icon sizes that must line up with a pixel grid, although many designers size icons in em so they match adjacent text.

Setting up a rem-based type scale

A simple, consistent scale makes rem easy to work with. Starting from the browser's default of 16px:

  • Body text: 1rem (16px).
  • Small text and captions: 0.875rem (14px).
  • Headings: 1.25rem (20px), 1.5rem (24px), 2rem (32px) and 3rem (48px).
  • Spacing: multiples of 0.25rem (4px), such as 0.5rem, 1rem, 1.5rem and 2rem.

Define these once as CSS custom properties or design tokens, then use the names everywhere. If you later decide body text should be slightly larger, one change updates the whole site in proportion.

Testing that it works

Change your browser's default font size to 20px or 24px and reload the page. Text should grow, layouts should reflow without overlapping, and nothing important should be cut off. Then zoom to 200%, the level named in the accessibility guidelines. These two quick checks catch most problems caused by fixed pixel sizes.

Tip: Test your site with the browser's default font size set to large. Anything written in pixels that should have grown will stand out immediately.

Questions people ask

Should spacing use rem or px?
Rem, for most spacing, so padding and margins grow with the text. Keep px for hairline borders and fine details that should stay crisp.
How do I convert px to rem?
Divide pixels by the root font size, usually 16px. 24px is 1.5rem and 14px is 0.875rem.
Why use rem instead of px for font sizes?
Rem sizes follow the reader's browser font setting, so text grows for people who need larger text. Pixel sizes don't.
What is the difference between rem and em?
Rem is relative to the root element's font size; em is relative to the parent's, so em compounds when elements are nested.
Do media queries in rem use my html font size?
No. In media queries, rem and em use the browser's initial font size, usually 16px, so 48rem is 768px even if you changed the root font size.
How do I test whether my site respects text size settings?
Increase the browser's default font size to 20px or 24px and reload, then zoom to 200%. Text should grow and layouts should reflow without overlap or cut-off content.

Sources

  1. W3C: Understanding WCAG Success Criterion 1.4.4 Resize Text