---
title: "What is the difference between px, em and rem in CSS?"
description: "px is a fixed unit, em is relative to the current element's font size and rem is relative to the root font size. Learn which to use for text, spacing and borders."
url: https://www.danny.engineering/answer/css/what-is-the-difference-between-px-em-and-rem-in-css
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:58:00.000Z
tags: ["CSS"]
---

# What is the difference between px, em and rem in CSS?

## The short answer


`px` is an absolute CSS pixel. `em` is relative to the font size of the element it is used on (or its parent, when used for `font-size` itself). `rem` is relative to the font size of the root `html` element, which is 16px by default. Use `rem` for font sizes and most spacing so text respects the user's browser settings, `em` when something should scale with its own text, and `px` for things like hairline borders.


```css
html {
  /* leave the root alone so the user's preference wins */
}

h1 {
  font-size: 2rem;      /* 2 x root size, usually 32px */
  margin-block: 1.5rem;
}

.button {
  font-size: 1rem;
  padding: 0.5em 1em;   /* scales with the button's own text */
  border: 1px solid currentColor;
}
```


## Why it happens


The three units answer different questions.

- `px` means "this many CSS pixels". A CSS pixel is not a physical screen pixel; on a high-density display one CSS pixel may be two or three device pixels. It does not change when the user picks a larger default font size in their browser settings.
- `em` means "multiples of the current font size". On most properties, `1em` equals the computed `font-size` of the same element. On the `font-size` property itself, `1em` refers to the parent's font size, because the element's own size is the thing being worked out.
- `rem` means "root em": multiples of the font size on the `html` element. Every `rem` on the page points at the same value, so it does not compound.

The compounding of `em` is the classic surprise. If a list sets `font-size: 1.2em` and you nest lists three deep, the innermost text ends up at 1.2 x 1.2 x 1.2, about 1.73 times the base size.


```css
ul {
  font-size: 1.2em; /* nested lists grow each level */
}

ul {
  font-size: 1.2rem; /* every list is the same size */
}
```


The accessibility point matters too. Many people raise the default font size in their browser rather than zooming the whole page. Font sizes in `rem` or `em` follow that setting; font sizes in `px` ignore it. Page zoom scales everything, including `px`, so the difference only shows with the font size setting.


## How to fix it


### Use rem for font sizes and layout spacing


A type scale and spacing scale in `rem` keeps everything proportional to the user's chosen base size, with no compounding.


```css
:root {
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-4: 1rem;
  --text-sm: 0.875rem;
  --text-lg: 1.25rem;
}

.card {
  padding: var(--space-4);
  font-size: var(--text-sm);
}
```


### Use em for things tied to their own text


Padding on buttons, the size of an inline icon and the gap after a heading often look best when they scale with the text next to them. With `em`, a small button and a large button keep the same proportions without separate padding values.


```css
.icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.125em;
}

.button--large {
  font-size: 1.25rem; /* padding in em grows with it */
}
```


### Use px where scaling would look wrong


Borders, outlines, box shadows and other fine details usually should stay crisp at a fixed size. A 1px border that becomes 1.25px at a larger font size can render blurry.


### Do not shrink the root to make maths easier


An old trick sets `html` to `font-size: 62.5%` so that `1rem` equals 10px. It works, but it means every component has to set a font size or the text is tiny, and third-party widgets that assume a 16px root break. If you want easier maths, write `rem` values with a comment, or use custom properties.


## Common pitfalls

- Setting `html` to a `px` font size, such as `font-size: 16px`. This overrides the user's browser preference for the whole site.
- Using `em` for font sizes on nested components and getting text that grows or shrinks with each level.
- Media queries: `em` and `rem` in a media query both refer to the browser's initial font size, not your `html` rule. Using them there is fine and makes breakpoints follow the user's text size.
- Assuming `px` equals device pixels. On high-density screens it does not, so a 1px border is still a thin line, not an invisible one.
- Mixing units at random. Pick a rule (for example rem for layout, em for components, px for borders) and stick to it.

## Related reading

- [MDN: CSS values and units](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Values_and_units)
- [MDN: length](https://developer.mozilla.org/en-US/docs/Web/CSS/length)
