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.

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.

Now the reader sets the browser font to 20px.
Sized in px: nothing moves

Sized in rem: follows the reader

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: 40remis 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

Lines in px: they stay thin

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

Right: padding in rem

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

Right: min-height 2.5rem

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 value | Write | At a 20px browser font |
|---|---|---|
| 12px | 0.75rem | 15px |
| 14px | 0.875rem | 17.5px |
| 16px | 1rem | 20px |
| 20px | 1.25rem | 25px |
| 24px | 1.5rem | 30px |
| 32px | 2rem | 40px |
| 48px | 3rem | 60px |
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
| Value | Unit | Why |
|---|---|---|
| Body and heading sizes | rem | They are the text |
| Padding inside cards and buttons | rem | Room should grow with words |
| Gap between paragraphs | rem | Spacing belongs to the text |
| Reading column width | rem | Wider type needs a wider column |
| Breakpoints | rem | They respond to the reader's setting |
| Button height | rem, as min-height | The label may wrap |
| Button padding across sizes | em | It follows each label |
| Border and divider | px | It should stay thin |
| Shadow and blur | px | Depth does not need to scale |
| Line height | No unit | It multiplies each element's own font size |
The 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
- CSS length units, MDN, on px, em and rem.
- font-size, MDN, on relative font sizes.
- Media Queries Level 4, W3C, on relative units in media queries.
- Understanding Success Criterion 1.4.4, Resize Text, W3C.
- Change text, image and video sizes, Google Chrome Help.
- Tailwind CSS default theme and border width, Tailwind CSS.
- Bootstrap 5 Sass variables, Bootstrap.
- The screens, the 62.5% results and the media query widths come from our own tests in Chrome at the browser font sizes named. Fern Club, the article and the plant lists are invented.
- First published as a carousel on UX Brainy.





