CSS Reference ยท Units and Values
The rem unit
Reference for the CSS rem unit: how it follows the root element's font size, why it does not compound like em, and how to respect user font preferences.
rem stands for "root em". One rem is the computed font-size of the root element, which is <html> in an HTML document. Browsers default that size to 16px, and users can change the default in their settings, so 1.5rem is normally 24px but becomes larger for readers who asked for bigger text.
Unlike em, rem does not depend on where the element sits in the tree. A 2rem margin means the same size in a heading, in a nested list and in a footer. That predictability makes rem the usual choice for font sizes, spacing scales and breakpoints.
html { font-size: 100%; } h2 { font-size: 1.75rem; margin-block: 2rem 0.75rem; }
Try it
Syntax
<length> = <number>rem
- Reference: the computed
font-sizeof the root element. - Applies to: any property that accepts a
<length>, includingfont-size. - Animatable: yes, as part of a length.
- In media queries: equals the browser's initial font size, not the value set on
html.
Values
| Value | Description |
|---|---|
1rem |
The root font size (16px by default). |
0.875rem |
87.5% of the root size, 14px by default. |
2.5rem |
2.5 times the root size, 40px by default. |
Usage notes
- Leave the root alone, or use a percentage.
html { font-size: 100% }keeps the user's preference. Settinghtml { font-size: 16px }overrides it and weakens accessibility. - Avoid the "62.5%" trick. It makes
1remequal 10px for easy math but forces you to restore body text size everywhere. Modern tooling makes the math easy anyway. - When rem is not what you want. For spacing inside a component that should follow the component's own text size, use
em. - Changing the root font size re-computes every
remin the page, so a component-level change cannot be done withremalone. - Zoom and text-only zoom. Both scale
remvalues, because the root font size scales. - Breakpoints.
@media (min-width: 40rem)respects font preferences; apxbreakpoint does not.
Browser support
Supported in every browser.
Related
emโ relative to the element's font sizepxโ the pixel unitch,lh,rlhand friends โ other font-relative unitsfont-sizeโ sets the base size- Lesson: CSS Units