To convert px to rem, divide the pixel value by the root font size. Browsers commonly use a 16px root, so 24px becomes 24 / 16 = 1.5rem, and 14px becomes 0.875rem. If your site sets a different size on <html>, divide by that number instead.
The formula to convert px to rem
The px to rem formula is rem = px / root font size, and the reverse is px = rem x root font size. A rem unit, per the CSS length reference, "Represents the font-size of the root element (typically )."
The same reference says "A common browser default is 16px, but user-defined preferences may modify this." Mind the second half. The 16 is an assumption about the default, not a constant.
With a custom root the divisor changes: if your stylesheet sets html { font-size: 18px; }, then 36px becomes 2rem.
Common px to rem values at a 16px root
These are the sizes most stylesheets use, all at the 16px default.
| px | rem | Typical use |
|---|---|---|
| 1px | 0.0625rem | Hairlines (usually left as px) |
| 12px | 0.75rem | Captions, small labels |
| 14px | 0.875rem | Secondary text |
| 16px | 1rem | Body text |
| 18px | 1.125rem | Large body text |
| 20px | 1.25rem | Small headings |
| 24px | 1.5rem | Headings, spacing step |
| 32px | 2rem | Large headings |
| 48px | 3rem | Hero text, big gaps |
| 768px | 48rem | Tablet breakpoint |
| 1024px | 64rem | Desktop breakpoint |
To go the other way, multiply. A design token of 1.5rem renders at 1.5 x 16 = 24px for a user on default settings.
Why rem instead of px
Rem values scale with the user's chosen font size, while px values stay fixed. The CSS length reference warns that absolute lengths "can cause accessibility problems because they are fixed and do not scale according to user settings."
WCAG success criterion 1.4.4 asks that text can be resized "up to 200 percent without loss of content or functionality," as the W3C's Understanding Resize Text page explains. One of its listed techniques is using em units for font sizes.
Say a reader sets their browser default to 20px. Your 1.5rem heading now renders at 30px. A heading written as 24px stays at 24px and ignores their choice.
Which values to convert and which to keep in px
Convert a value to rem when it should grow with the reader's font size, and keep px when it shouldn't. That test comes from a detailed write-up on pixels and accessibility, and it is the most useful rule I know for this job.
- Font sizes: convert. This is the main reason rem exists.
- Vertical spacing around text (margins between paragraphs, heading gaps): rem keeps the rhythm in proportion.
- Media query breakpoints: also rem. A larger default font leaves less room, so the narrower layout should kick in earlier.
- Borders and outlines: keep px. The same write-up puts it plainly: "It doesn't really make sense for a border to become thicker."
- Horizontal padding on narrow containers: think first. Scaling it eats line width and leaves fewer words per line.
My take: a blanket find-and-replace of every px in a stylesheet is the wrong move. Convert type, spacing and breakpoints, and leave hairlines alone.
Media queries: rem ignores your html font-size
Rem and em in media queries always use the browser's initial font size, never the font-size you set on html. The Media Queries Level 4 spec says relative length units there "are based on the initial value", not on declared styles.
Suppose you set html { font-size: 62.5%; } and write @media (min-width: 48rem), expecting 480px. The breakpoint still fires at 768px for a default user, because the query ignores your root rule.
For breakpoints, divide by 16 whatever your stylesheet does to html. A user with a 20px default then gets that 48rem breakpoint at 960px, which is what you want.
The 62.5% trick and its cost
Setting html { font-size: 62.5%; } makes 1rem equal 10px at the default size, so 24px becomes 2.4rem with no calculator. A guide on overriding the root font size describes the pattern and the usual companion rule, body { font-size: 1.6rem; }, which brings body text back to 16px.
The cost lands on the whole page:
- Every third-party component that assumes 1rem = 16px renders at the wrong size.
- Your media queries don't follow the trick, as the previous section shows, so you work in two systems at once.
- Any element you forget to size inherits 10px text.
I'd skip it. A helper function gives you easy math without changing what 1rem means.
Convert px to rem automatically with Sass
A Sass function lets you keep writing pixel numbers while the compiled CSS gets rem. Use math.div for the division, because Sass has deprecated / as a division operator. The Sass breaking-change notice on slash division says math.div() works from Dart Sass 1.33.0, and that LibSass and Ruby Sass don't support it.
@use "sass:math";
@function rem($px, $base: 16px) {
@return math.div($px, $base) * 1rem;
}
h2 { font-size: rem(24px); } // 1.5rem
.card { padding: rem(20px); } // 1.25rem
If the build prints a deprecation warning about / for division, an older helper is still dividing with a slash. Swap that line for math.div.
In plain JavaScript, for example in a design-token script, the same idea is one line: const rem = (px, base = 16) => px / base + 'rem';.
Convert a whole stylesheet at once
For an existing stylesheet full of pixel values, a converter does the arithmetic. Paste the CSS into the px to rem converter, which uses a 16px base by default, then review the output before you ship it.
The limitation: a converter changes every px it finds. That includes borders, box shadows and 1px hairlines you probably wanted to keep. Go through the result with the list above and switch those back by hand.
If the CSS came from a design file, check how the designer measured. Our guide to using rem in Figma shows how to get rem values straight from Dev Mode. After converting, a formatter keeps the diff readable; see Prettier vs an online formatter for which fits your workflow. And if the stylesheet is private, read whether online formatters are safe before pasting it anywhere.
FAQ
What is 1px in rem?
With the usual 16px root font size, 1px is 0.0625rem. Divide 1 by 16 to get it.
How do I convert rem back to px?
Multiply the rem value by the root font size. At a 16px root, 1.5rem x 16 = 24px.
Should I set html font-size to 62.5%?
It makes 1rem equal 10px, which simplifies the math, but it changes the root for every component on the page and you then need body { font-size: 1.6rem } to restore 16px text. Dividing by 16 with a helper function avoids that.
Does changing html font-size change rem in media queries?
No. Relative units in media queries are based on the initial font size, so a font-size you set on html does not move rem or em breakpoints.