Web Design & UI/UX8 min read

px vs rem in CSS: Which Unit Should You Use?

Use rem for font sizes and the space around text, px for borders, dividers and shadows. Both rendered at larger browser font sizes, with two common mistakes, a conversion table and the CSS.

By Boone
XLinkedIn
px vs rem in CSS: Which Unit Should You Use?

Both units can draw the same page, pixel for pixel, on your machine. They split the moment a reader changes the browser's font size. Below: both rendered at larger settings, when each one is right, two mistakes, a conversion table and CSS you can copy.

The one question that decides it

Should this value grow when the reader asks for bigger text? If yes, write it in rem. If no, write it in px.

A px value in CSS is a fixed length in CSS pixels. A rem value is a multiple of the font size on the root element, usually <html>. If you leave the root alone, it takes the browser's default, so 1.5rem is 24px at the usual default and 30px for someone who set their default to 20px.

Here is one card written both ways, with the browser at 16px. At this setting the px and rem versions come out the same.

A small article card from an invented plant app, titled Water your fern on Sundays, at the usual 16px browser font size

Now the reader sets the browser font to 20px.

Sized in px: nothing moves

The article card sized in px, unchanged although the browser font is set to 20px

Sized in rem: follows the reader

The same card sized in rem, with larger text and padding at a 20px browser font

The px card ignores the request. In the rem card the body text goes from 16px to 20px, and the title, the small line, the padding and the gaps grow by the same quarter.

Page zoom is a different control. Zooming with Cmd or Ctrl and plus scales px and rem alike. The gap between the two units shows up with the font size setting, which in Chrome sits under Settings, then Appearance.

When rem is right

  • It is a font size. MDN's advice is to use values relative to the reader's default font size.
  • It is padding or a gap that belongs to text. Bigger words need more room around them.
  • It is a reading width. max-width: 40rem is 640px at a 16px root and 800px at 20px, so the column widens with the type. The 40 is a common convention, not a standard. More on that in line length.

When px is right

  • It is a border or a divider. A hairline is there to separate things, and it does that job at any text size.
  • It is a shadow or a blur. Depth has nothing to do with how big the reader likes their letters.
  • It should not grow with the text. If a bigger value would only look heavier, keep it fixed.

Below, the browser font is set to 32px, double the usual default. The text in both lists is in rem. Only the lines and the shadow differ.

Lines in rem: they thicken

A plant care list at a 32px browser font where the rem border and dividers have doubled to 2px

Lines in px: they stay thin

The same list at a 32px browser font with its border and dividers still 1px

A border written as 0.0625rem looks like 1px on your screen. At a 32px setting it is 2px, and its shadow has doubled too.

Popular frameworks split it the same way. Tailwind's default theme sets its base text size to 1rem, its spacing step to 0.25rem and its small shadows in px, and its border class is 1px. Bootstrap 5's variables use 1rem for the base font size and spacer, and 1px for the border width.

Mistake 1: rem text inside px padding

Wrong: padding stuck in px

A watering reminder with large text squeezed against the card edges and a tight button

Right: padding in rem

The same reminder with padding and gaps that grew along with the text

Converting only the font sizes does half the job. At the 32px setting the text has doubled, while the card still has 16px of padding and 8px gaps. With the padding in rem, those become 32px and 16px, and the card keeps its proportions. The price is height and an extra line break in the button, which is a fair trade for room to read.

Mistake 2: a fixed px height around rem text

Wrong: height 40px

A green button locked to 40px tall, with its wrapped label cut off above and below

Right: min-height 2.5rem

The same button grown tall enough to show its whole label on three lines

height: 40px looks fine until the label wraps. Then the words spill out of the box and part of the sentence disappears against the white card. Use min-height with a little vertical padding, and let the button grow.

px to rem conversion table

To convert, divide the px value by 16. The last column shows what the same rem value becomes for a reader at 20px.

Design valueWriteAt a 20px browser font
12px0.75rem15px
14px0.875rem17.5px
16px1rem20px
20px1.25rem25px
24px1.5rem30px
32px2rem40px
48px3rem60px

For picking the sizes themselves, see the modular type scale guide and our notes on mobile font size.

What about em?

An em is relative to a font size close by, where rem always looks at the root. On font-size, 1.5em means one and a half times the parent's size. On padding, em follows the element's own font size.

That makes em handy for a button that comes in three sizes: set the padding once in em and it scales with each label. For type across a whole page, rem is easier to predict, because nesting does not multiply it.

Two traps: the root size and media queries

Do not set the root in px. html { font-size: 10px } makes the math easy and cuts the reader out, because 1rem stays 10px whatever they choose. The 62.5% version also gives you 10px at the usual default, but it still follows the setting: at 20px, 1rem becomes 12.5px. If you use it, remember to set the body text back to 1.6rem.

rem in a media query means the browser's setting. Inside a query, rem is based on the initial font size, not on anything your stylesheet puts on html. So (min-width: 40rem) switches at 640px for a reader at 16px and at 800px for a reader at 20px. With bigger text, the page keeps its single column up to a wider screen.

px or rem: a decision table

ValueUnitWhy
Body and heading sizesremThey are the text
Padding inside cards and buttonsremRoom should grow with words
Gap between paragraphsremSpacing belongs to the text
Reading column widthremWider type needs a wider column
BreakpointsremThey respond to the reader's setting
Button heightrem, as min-heightThe label may wrap
Button padding across sizesemIt follows each label
Border and dividerpxIt should stay thin
Shadow and blurpxDepth does not need to scale
Line heightNo unitIt multiplies each element's own font size

The CSS

css
html { font-size: 100%; } /* keep the reader's setting */ body { font-size: 1rem; line-height: 1.5; } h2 { font-size: 1.5rem; line-height: 1.25; } .card { max-width: 40rem; padding: 1.25rem; border: 1px solid #D9D9DE; box-shadow: 0 1px 3px rgb(0 0 0 / 0.1); } .button { min-height: 2.5rem; padding: 0.5rem 1rem; font-size: 1rem; } @media (min-width: 40rem) { .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; } }

To test it, set your browser font to 20px or higher and walk through the page. Look for clipped labels, cramped boxes and text that refused to grow.

FAQ

Should I use px or rem for font size?

Use rem. It follows the font size a reader has set in their browser, while a px size stays where you put it.

What is 1rem in px?

It equals the font size of the root element. With browser defaults and no override, that is usually 16px.

When should I use px in CSS?

Use px for values that should stay fixed as text grows, such as 1px borders, dividers and shadows.

Does using px break accessibility?

Not by itself. WCAG 1.4.4 asks that text can be resized to 200 percent, and page zoom scales px too. Text sized in px still ignores the browser's font size setting, so rem is the safer choice.

What is the difference between rem and em?

rem is relative to the root font size. em is relative to the font size nearby, so it can compound when elements are nested.

Sources

More from Brainy Papers

Keep reading