Lab

Lab 05

PX to REM converter, both waysPixels in,rem out.

Type a value in px or rem and get the other one at once, at any root font size. Copy a common size with one click, or paste a block of CSS and get every px value back in rem. It all runs in your browser.

Formula: rem = px / 16. Browsers use 16px unless the user or your CSS changes it.

Convert a block of CSS

Paste CSS on the left. Every px value turns into rem on the right, at the root size above.

7 values converted

.card {
  padding: 1.5rem 2rem;
  margin-bottom: 3rem;
  border: 1px solid #e5e5e5;
  border-radius: 0.75rem;
}

.card h2 {
  font-size: 1.75rem;
  line-height: 2.25rem;
  letter-spacing: -0.0313rem;
}

A px to rem converter that also converts your CSS

This px to rem converter works both ways at any root font size. Type in either field and the other one updates. Below it, paste a block of CSS and every px value is rewritten in rem, ready to copy. Nothing leaves your browser.

  1. 01

    Set the root size

    Leave the root font size at 16px, the browser default, unless your CSS sets a different size on the html element. Every result uses this number.

  2. 02

    Convert a value

    Type in Pixels to get rem, or in Rem to get pixels. Press Copy for the rem value, or click any size in the common sizes grid to copy it.

  3. 03

    Convert a block of CSS

    Paste CSS in the left box. The right box shows the same CSS with px replaced by rem, and how many values changed. 1px borders stay in px unless you untick the box.

How to convert px to rem

Divide the pixel value by the root font size: rem = px / 16. So 24px is 1.5rem, 14px is 0.875rem and 40px is 2.5rem. To go back, multiply: px = rem * 16.

The root font size is the font-size of the html element. Browsers set it to 16px by default, and people can change it in their browser settings. That is the whole point of rem: when a reader sets a larger default font, every size in rem grows with it, and sizes in px do not.

What the CSS converter changes

It finds every number followed by px and divides it by the root size. 0px becomes 0. Values inside url() and inside comments are left alone. By default 1px stays as it is, because hairline borders and dividers should stay thin when the text grows.

Media queries are converted too: a min-width: 768px becomes 48rem. Media queries in rem or em follow the reader's font size too, which is usually what you want, but check the result if your breakpoints must match exact device widths.

When to use rem and when px is fine

Use rem for font sizes, and for spacing that should grow with the text, such as padding inside buttons and the gaps between paragraphs. Use px for borders, outlines, shadows and other details that should stay fixed.

For type that also grows with the screen, combine rem with vw in a clamp(). The CSS clamp() generator in the Lab writes that for you.

Frequently asked questions

How many px is 1rem?

1rem equals the root font size, which is 16px in every major browser by default. If your CSS sets html { font-size: 62.5% }, 1rem becomes 10px. If a reader changes the default font size in their browser, 1rem follows their setting, which is why rem is better for text than px.

What is the difference between rem and em?

rem is relative to the root element's font size, so it means the same thing everywhere on the page. em is relative to the font size of the element itself, or its parent for the font-size property, so it compounds when you nest elements. Use rem for most sizes and em for things that should scale with their own text, such as icon size inside a button.

Should I set the root font size to 62.5%?

It makes the maths easy, because 1rem becomes 10px, but it also means every component must set its own font size, or text falls to 10px. Most teams now leave the root at 100% and use a converter or a design token scale instead. This tool works with either, just set the root size field.

Does the converter store or send my CSS?

No. The conversion is a few lines of JavaScript that run in your browser. The CSS you paste is never uploaded, logged or saved. Close the tab and it is gone.

Need this built into your site?

I build tools like this one, and the sites around them. Tell me what you need and I will reply with a plan and a fixed price.