The difference in em vs rem is what each unit measures against. An em is relative to the font size of the element it is used on, and a rem is relative to the font size of the root <html> element. That makes em values compound when elements are nested, while a rem value computes to the same pixels anywhere on the page.
What em and rem are relative to
An em is relative to the current element's font size, and a rem is relative to the root element's font size. The "r" stands for root: rem is the "root em".
Em has two meanings. The CSS length reference puts it this way: em "represents the calculated font-size of the element," but on the font-size property itself, it represents the inherited font size.
So on font-size, em means "my parent's font size". On anything else (padding, margin, width, border-radius) it means "my own font size". Rem means "the root font size" on every property.
A common browser default for the root is 16px, though users can change it in their settings. With that default, 1rem is 16px everywhere.
How em compounds when you nest elements
Nested em font sizes multiply, because each level measures against the level above it. I tested this card in Chrome with the root at 16px and read the results with getComputedStyle:
.card {
font-size: 1.25em;
padding: 1.5em;
}
| Font size | Padding | |
|---|---|---|
.card |
20px | 30px |
.card inside .card |
25px | 37.5px |
The outer card's text is 16 × 1.25 = 20px. Its padding is 1.5 times its own 20px, so 30px, not 24px. The inner card starts from 20px and lands on 25px text with 37.5px padding.
Lists show the effect fastest. A beginner CSS units guide uses li { font-size: 1.3em; } on a list nested three levels deep. In my test those three levels computed to 20.8px, 27.04px and 35.152px.
Percentage font sizes compound the same way, since a percentage is also relative to the parent. Switching 1.3em to 130% fixes nothing.
How rem stays the same at every level
A rem value ignores nesting, because it always resolves against the root font size. The same card written with rem:
.card {
font-size: 1.25rem;
padding: 1.5rem;
}
| Font size | Padding | |
|---|---|---|
.card |
20px | 24px |
.card inside .card |
20px | 24px |
Move the component anywhere in the page and its size holds.
Rem still follows the user. When I set the root to 20px, as a reader who prefers larger text might, 1.25rem computed to 25px and 1.5rem to 30px.
On the root element itself, rem refers to the initial value, so html { font-size: 1.25rem; } gives 20px at a 16px default instead of looping back on itself.
em vs rem side by side
The two units differ in what they measure against, whether they compound, and what they are good at.
| em | rem | |
|---|---|---|
| Relative to | The element's font size (the parent's, on font-size) |
The root element's font size |
| Compounds when nested | Yes | No |
| Same value, same pixels everywhere | No | Yes |
| Follows the user's browser font setting | Yes, through inheritance | Yes, directly |
| Best for | Spacing that should track a component's text size | Font sizes and page-level spacing |
| In a media query | Browser's initial font size | Browser's initial font size |
When to use em and when to use rem
Use rem for font sizes and layout spacing, and em for spacing that should grow with a component's own text. A button is the standard case for mixing them:
.btn { font-size: 0.875rem; padding: 0.5em; }
.btn-large { font-size: 1.25rem; }
The small button computed to 14px text with 7px padding. The large one, with the same 0.5em padding rule, came out at 20px text and 10px padding. One font-size change resized the whole button. No padding override needed.
My take: default to rem, and use em only inside a component, never on font-size. Em on font-size is where the surprises come from. Em on padding is predictable once the font size itself is in rem.
Em also suits anything tied to the text around it: the gap after an inline icon, letter-spacing, the indent of a blockquote.
To turn a design's pixel values into rem, converting px to rem covers the formula, and the px to rem converter does the division for you. Designers can also show rem values in Figma.
Why you can't convert px to em without context
A pixel value has no single em equivalent, because the answer depends on where the element sits. 24px is 1.5rem on any page with a 16px root. In em it is 1.5em inside a 16px parent and 1.2em inside a 20px one.
That is the limit of em, and of any calculator. A converter can do px to rem with one number, the root size. For em it also needs the element's font size, and that changes whenever someone moves the markup. Once a stylesheet is minified nobody can see that context at a glance either.
em and rem in media queries
In a media query, em and rem both use the browser's initial font size, so they behave identically. The Media Queries specification says relative length units there "are based on the initial value, which means that units are never based on results of declarations."
So @media (min-width: 40em) and @media (min-width: 40rem) break at the same width. Setting html { font-size: 20px; } moves neither. Only the user's own browser font setting does. If you inherit breakpoints in a compressed file, format the minified CSS first so you can read them.
FAQ
Is 1em always equal to 1rem?
No. They are equal only when the element's font size matches the root font size. Inside a parent with a 20px font size, 1em is 20px while 1rem stays at the root size, 16px by default.
Is em relative to the parent or to the element itself?
Both, depending on the property. On font-size, em uses the inherited font size, which comes from the parent. On every other property, such as padding, margin or width, em uses the element's own font size.
Should I use em or rem for font size?
Use rem for font sizes in most cases. Each value then depends only on the root font size, so nesting an element inside another one does not change its text size.
Do em and rem work in media queries?
Yes, but both are based on the browser's initial font size there, not on any font-size you declare in CSS. In a media query, em and rem give the same result.