completefrontend

px vs em vs rem in CSS

px is fixed, em follows the font size of the element or its parent, and rem follows the root font size. Here's how each one works out in pixels, why em compounds, and which to use so text grows with the reader's settings.

Title card for px vs em vs rem in CSS, with three blocks: px is a fixed size, em follows the local font size, and rem follows the root font size

Chrome and Firefox both let you choose a default text size in their settings. Someone with weak eyesight turns it up, then opens your website. On some sites the text grows with that choice. On others it stays exactly the same, because of the unit the developer typed after each number.

In CSS, px is a fixed size, em follows the font size of the element or its parent, and rem follows the root font size, which is usually 16px. So the px vs em vs rem choice comes down to where you use them. Use rem for font sizes and most spacing. Use em for spacing that should grow with an element's own text, and px for small details like borders.

What does px mean in CSS?

The name px is short for pixel. In CSS it's a fixed length: 1px is 1/96 of an inch. It isn't one dot on your screen, though. On a sharp phone or laptop screen, one CSS pixel covers several of the screen's tiny dots. That's why 16px text looks about the same size on a phone and a laptop, each at its usual reading distance.

A px value stays the same when the reader changes their default text size. Browser zoom is different: zooming in makes every CSS pixel bigger, so px sizes grow too. But a reader who sets a bigger default text size once expects every site to follow it, and px text won't.

What is em in CSS?

The em unit is relative to font size, and it follows two rules:

  • When you use it for font-size, 1em is the parent element's font size.
  • When you use it for anything else, like padding or width, 1em is the element's own font size.
.button {
  font-size: 20px;
  padding: 0.5em 1em; /* 10px top and bottom, 20px left and right */
}

The padding follows the button's own text. Make the text bigger, and the padding grows with it, so the button keeps its shape. That's what em does best.

Why does em compound?

When you use em for font-size on elements inside each other, each one multiplies its parent's size again. That's called compounding.

span {
  font-size: 1.6em;
}
<div>Normal <span>outer <span>inner</span></span></div>
Element Calculation Font size
div the browser default 16px
outer span 16 × 1.6 25.6px
inner span 25.6 × 1.6 40.96px

The same rule makes the inner span much bigger than the outer one. This is how nested lists, cards inside cards, and menus inside menus end up growing or shrinking by surprise.

What is rem in CSS?

The name rem is short for "root em". It's relative to the font size of the root element, <html>, and nothing else. Change the rule above to 1.6rem, and both spans are 25.6px, however deep they sit.

Think of giving directions. An em says "two streets from where you're standing", so the answer depends on where you are. A rem says "two streets from the town square", so everyone ends up in the same place.

With the usual 16px default, here's what common rem values work out to:

rem px at the default size
0.75rem 12px
0.875rem 14px
1rem 16px
1.25rem 20px
1.5rem 24px
2rem 32px

To turn pixels into rem, divide by 16. A 24px heading is 24 ÷ 16 = 1.5rem.

Three cards apply font-size 24px, 1.6em and 1.6rem to a span inside a span. The px text is 24px everywhere, the em text grows from 25.6px to 40.96px, and the rem text stays 25.6px. Below, when the reader picks a 20px default, 16px stays 16px while 1rem becomes 20px
The same nested spans in px, em, and rem, and what happens when the reader picks bigger text.

Why is rem better for accessibility?

Accessibility means building pages that everyone can use, including people with poor eyesight. Many of them raise the browser's default text size. Here's what happens when a reader changes it from 16px to 20px:

Your CSS With the 16px default When the reader picks 20px
font-size: 16px 16px 16px
font-size: 1rem 16px 20px
font-size: 1.5rem 24px 30px

Text in px ignores the reader's choice. Text in rem follows it, and everything keeps the same proportions. So set font sizes in rem or em, not px.

One mistake undoes all of this: html { font-size: 16px; }. A px size on the root pins every rem on the page to a fixed number again. Leave the root's font size alone, or use a percentage like 100%, which keeps the reader's choice. The color, contrast, and motion lesson covers the rules for text resizing and zoom in more depth.

When should you use px, em, or rem?

Unit Relative to Follows the reader's text size Good for
px Nothing, it's fixed No Borders, outlines, shadows
em The element's font size (the parent's, for font-size) Yes Padding and icons that grow with their own text
rem The root <html> font size Yes Font sizes, margins, gaps, and layout widths

In practice:

  • Use rem for font sizes. Use it for margins and gaps too, so the spacing grows along with the text.
  • Use em inside small parts. Button padding and an icon set to width: 1em stay in step with the text next to them.
  • Use px for fine details. A 1px border should stay thin when the text grows.
  • Use em or rem in media queries. A media query is a CSS rule that only applies at certain screen sizes. Relative units there work better when the reader changes their text size.

What about percentages and vw?

Two more units show up often. A percentage works like em for font-size: 80% is 0.8 times the parent's size. For width, it's a share of the parent's width instead.

The vw unit means 1% of the width of the browser window. Don't size text in vw alone, because zooming then doesn't make it bigger. Mix it with rem instead, as in font-size: calc(1rem + 1vw). Text then grows a little on wide screens and still responds to zoom. Responsive web design shows where this fits in a layout that adapts to any screen.

What should you learn next?

Units decide how big things are. To see how padding, borders, and margins add up around an element, read the CSS box model. Then lay those boxes out with flexbox and grid. If a size you set doesn't show up, a stronger rule may be winning, and CSS specificity explains why.

Tailwind's spacing scale is built on rem too, as Tailwind CSS vs CSS shows. To check the final pixel size of any element from code, the styles and classes lesson covers getComputedStyle. It's part of the JavaScript course.