About px, rem and em
What px, rem and em are
px is a fixed CSS pixel. It is not a device pixel: on a high-density screen it covers two or three. rem is relative to the font size of the root element, <html>. em is relative to the font size of the current element, or of its parent when used in font-size itself.
How to convert px to rem
rem = px ÷ root font size. Browsers use 16px by default, so 0.25rem = 4px, 1rem = 16px, 1.5rem = 24px and 13px = 0.8125rem. To go back, multiply: 2rem × 16 = 32px.
| px | rem | Tailwind unit |
|---|---|---|
| 4px | 0.25rem | 1 |
| 8px | 0.5rem | 2 |
| 12px | 0.75rem | 3 |
| 16px | 1rem | 4 |
| 20px | 1.25rem | 5 |
| 24px | 1.5rem | 6 |
| 32px | 2rem | 8 |
| 48px | 3rem | 12 |
| 64px | 4rem | 16 |
Why rem helps accessibility
People with low vision often raise the default font size in their browser settings, say from 16px to 20px. Sizes in rem grow with that setting. Sizes in px ignore it, so text set in px stays small. Page zoom scales both. Use rem for font sizes and spacing; keep px for 1px borders.
em compounds
Because em follows the parent, nested values multiply. A list at 0.8em inside another list at 0.8em ends up at 16 × 0.8 × 0.8 = 10.24px. rem avoids this. em is still handy for padding that should scale with the text of a button.
The Tailwind spacing scale
In Tailwind one spacing unit is 0.25rem, which is 4px. So p-1 = 4px and p-4 = 1rem = 16px. For a size off the scale, use an arbitrary value in brackets, such as p-[13px] or w-[0.8125rem]. Tailwind v4 builds the scale from one --spacing variable, so any multiple of 0.25 works, like w-13 or w-2.75; v3 has a fixed list of steps.
Changing the root font size
Some sites set font-size: 62.5% on <html> so that 1rem = 10px and the math is easy. Then you divide by 10, not 16. The catch: Tailwind classes and third-party components shrink too, so p-4 becomes 10px.
What this tool does
It converts a width and a height at once between Tailwind units, px and rem, always with a 16px root. A bare number is read as Tailwind units; add px or rem to say otherwise. em is treated as rem. Paste a CSS block such as width: 127.563px; height: 96.546px; and both values are pulled out. Results keep up to six decimals, and the class to copy is an arbitrary rem value, such as w-[7.972688rem].
Frequently asked questions
- How many pixels is 0.25rem?
- 4px, with the default 16px root font size (0.25 × 16). It is also one Tailwind spacing unit, as in p-1.
- How do I convert px to rem?
- Divide the pixel value by the root font size, which is usually 16. For example, 24px ÷ 16 = 1.5rem. To convert rem to px, multiply by 16.
- Is 1rem always 16px?
- No. 16px is only the browser default. It changes when the site sets a font size on the html element, or when the user changes the default font size in the browser settings.