rem
A size measured from the font size set on the page's html element.
Safe to useBaseline: widely available
| Browser | Since |
|---|---|
| Chrome | 4+ |
| Chrome Android | 18+ |
| Edge | 12+ |
| Firefox | 3.6+ |
| Firefox Android | 4+ |
| Safari | 5+ |
| Safari iOS | 4+ |
Formula 1rem = font-size of :root (16px unless the reader changed the browser default)
How it works
1rem is the font-size of the html element, the root of the page. By default that is 16px, and a reader who raises the default size in the browser settings changes it for every rem on the page at once. So rem is the unit for type and spacing that should respect the reader: one setting scales the whole design, and a stylesheet that never touches the root size gets it for free.
Two details catch people out. In the root element’s own font-size, rem cannot refer to itself, so it reads the browser’s initial value instead: html { font-size: 2rem } gives 32px at the default size and 48px when the reader’s default is 24px. And in a media query rem behaves like em: it means the initial size and ignores whatever your CSS sets on html.
Two root sizes were measured with the reader’s default raised to 24px. html { font-size: 62.5% }, the old trick that makes 1rem 10px for easier arithmetic, gave 15px: a percentage is taken of the reader’s default, so the setting survives. The cost moves elsewhere: a body rule has to set the text size again, and every rem on the site reads as tenths. html { font-size: 16px } gave 16px: the pixel value replaces the reader’s default instead of scaling it, and the setting is gone for every rem on the page.
Try it
Measure the font-relative units
Pick a font and a size. The page measures 1em, 1ex, 1ch, 1cap and 1lh in that font, here, in your browser.
| Unit | Measured | Share of the font size |
|---|---|---|
1em | ||
1ex | ||
1ch | ||
1cap | ||
1lh |
The same as a table
| Unit | Measures |
|---|---|
1em | The font size of the element. |
1ex | How tall the letter x is in this font. |
1ch | The width of the digit zero in the font. |
1cap | The height of a capital letter in the font. |
1lh | The line height of the element. |
What changes it
- Moves with
- the reader's text size
- page zoom
- Stays put under
- the window size
- the font in use
- the query container
- phone toolbars
- the writing mode
Reach for it when
Type sizes and the spacing between blocks, so that one browser setting scales the whole page.
Watch out
Setting the root size in pixels (say, 16px on html) switches off the reader's preference: rem then stays at 16px whatever the browser setting says. Leave the root alone or use a percentage.
Before
html { font-size: 16px; }
h1 {
font-size: 32px;
margin-block-end: 24px;
}After
h1 {
font-size: 2rem;
margin-block-end: 1.5rem;
}