Skip to content
UniKit

CSS px to rem converter

Batch-convert px to rem across a CSS block: configurable root font size and decimals, reverse rem→px, skip 1px, exclude or whitelist properties by prefix. Real declaration parsing keeps comments, content strings, url() and media queries intact, with replacement counts.

Runs in your browserEvery computation happens in your browser — your data never leaves this device.

CSS code

CSS text only: pull the CSS out of style="..." attributes or JS strings before pasting.

Conversion options

Off by default: breakpoints usually stay in px. Also covers @supports / @container preludes.

Converted CSS

Only units inside declaration values are changed; comments, strings and url() stay untouched.

Please enter the CSS to convert.

What this tool does

  • Batch-convert every px in a CSS block to rem with a configurable root font size and decimals
  • Keep 1px borders and media-query breakpoints intact while scaling the lengths that actually need it
  • Run the reverse direction to turn rem back into px and check the implementation against the design

Example

Input

/* 设计稿 16px */
.card { margin: 16px; content: "16px"; }

Output

/* 设计稿 16px */
.card { margin: 1rem; content: "16px"; }

With the default root font size of 16 the margin becomes 1rem, while the 16px inside the comment and the content string stay untouched (1 converted, 2 untouched here).

Frequently asked questions

Does it convert px inside style="..." attributes or JS strings?

No. Only CSS text is parsed: pasting HTML makes style="margin:16px" a string that is skipped, and px inside JavaScript template strings is ignored too. Extract the CSS snippet first if you need it converted.

Are px inside comments, content strings and url() changed?

No. Comments, strings and url() are replaced with equal-length blanks before the CSS is split into declarations, and only declaration values are converted. So /* 16px */, content: "16px" and url(sprite-16px.png) stay as-is and are counted as untouched.

Why did the px inside @media not change?

At-rule preludes (@media, @supports, @container) are left alone by default because breakpoints usually stay in pixels. Tick "Also convert px inside @media" to convert them too: 768px becomes 48rem at a 16px root.

How do I keep 1px borders and border properties?

Enable "Skip 1px" to keep every ±1px value, and use "Exclude properties" for prefix matching: border also excludes border-width, border-radius and border-color. "Only convert these properties" does the opposite, e.g. font-size, margin. When both are set, the only-list wins.

How many decimals should I use? What is 1px in rem?

Decimals range from 0 to 8 and default to 4: at a 16px root, 1px = 0.0625rem, 8px = 0.5rem and 20px = 1.25rem. With 2 decimals 1px rounds to 0.06rem, which accumulates visible error, so 4 is recommended.

Keywords:postcssrem

Related tools