PX 转 REM 转换器

这个 PX 转 REM 转换器可将像素值转换为响应式 CSS 的 REM 单位。REM 单位相对于根元素字体大小进行缩放(默认 16px),使布局能随用户的字体偏好自适应。输入像素值,即时获取对应的 REM 值,支持自定义基础字体大小。

PX to REM Converter

Convert pixel (px) values to rem units based on the root element font size

px
px
rem

Frequently Asked Questions

PX转REM的公式是什么?

PX转REM的公式为:REM = 像素值 ÷ 基础字体大小。浏览器的默认基础字体大小为16px。例如,将24px转换为REM:24 ÷ 16 = 1.5rem。其他常见转换:12px = 0.75rem、14px = 0.875rem、16px = 1rem、20px = 1.25rem、32px = 2rem、48px = 3rem。如果您的项目使用不同的基础字体大小,请在公式中替换该值。

浏览器的默认基础字体大小是多少?

所有主流Web浏览器(Chrome、Firefox、Safari、Edge)的默认基础字体大小均为16px。这意味着在没有任何CSS覆盖的情况下,1rem等于16px。这个16px的默认值从Web早期就是标准,也是大多数PX转REM转换器默认使用的值。一些开发者用"html { font-size: 62.5%; }"覆盖这个值,将基础设为10px,使转换更简单(1rem = 10px),但如果处理不当可能会导致无障碍问题。

PX和REM有什么区别?

PX(像素)是绝对CSS单位,无论用户设置或父元素如何都以固定大小渲染。REM(根em)是基于根元素字体大小的相对单位,通常是<html>元素。如果根字体大小为16px,则1rem等于16px。关键区别在于,当用户更改浏览器默认字体大小以提高无障碍性时,REM会相应缩放,而PX保持固定。这使REM成为响应式和无障碍Web设计的首选单位。

为什么应该使用REM而不是PX?

REM单位随用户字体偏好缩放,提高了视障用户的无障碍性。当用户增大浏览器默认字体大小时,基于REM的布局按比例缩放,而基于PX的布局保持固定。REM还提高了可维护性:在一个地方更改根字体大小,整个设计按比例缩放。此外,REM单位与媒体查询结合,使创建适应不同屏幕尺寸的响应式布局更加容易。WCAG 2.1指南建议使用相对单位进行文本大小调整。

REM和EM有什么区别?

REM(根em)相对于根元素的字体大小(<html>元素),而EM相对于其父元素的字体大小。这意味着REM无论嵌套深度如何都产生一致的值,但EM随每级嵌套而叠加。例如,如果父元素有font-size: 1.5em,子元素也有font-size: 1.5em,子元素的实际大小是1.5 × 1.5 = 2.25倍根大小。使用REM,1.5rem始终等于1.5倍根大小。REM通常因其可预测性而受到青睐。

什么时候应该使用PX而不是REM?

对于不管字体大小变化都应保持不变的值,使用PX。常见情况包括:边框(1px边框应保持细)、阴影以及某些缩放后看起来奇怪的装饰元素。媒体查询断点通常用PX写以保持一致性,尽管一些开发者更喜欢用EM写断点。对于所有排版、间距(margin和padding)以及像容器max-width这样的布局属性,REM通常是更好的选择,以提高无障碍性和响应性。

如何更改基础字体大小?

要更改基础字体大小,在CSS中的html元素上设置font-size属性。例如,"html { font-size: 62.5%; }"将基础设为10px(16px默认值的62.5%),使转换更容易:1rem = 10px,1.6rem = 16px。也可以使用"html { font-size: 18px; }"来设置更大的基础。使用基于百分比的值时要谨慎,因为它们尊重用户偏好,而固定像素值会覆盖它们。为了无障碍性,建议使用百分比,这样增大浏览器字体大小的用户不会受到影响。

16px等于多少REM?

使用所有主流浏览器(Chrome、Firefox、Safari、Edge)默认的16px根字体大小时,16px等于1rem。计算为16px ÷ 16px = 1rem。如果您将根字体大小更改为不同的值,例如10px(一种常见做法),则16px将等于1.6rem(16 ÷ 10 = 1.6)。始终检查CSS根字体大小以确保准确转换。

REM如何提高无障碍性?

REM通过尊重用户字体大小偏好来提高无障碍性。当视障用户将浏览器默认字体大小从16px增大到20px时,所有基于REM的文本和间距都按比例缩放。设置为2rem的标题将从32px增长到40px,保持设计层次结构。使用固定PX值时,无论用户设置如何,文本大小都保持不变,实际上忽略了他们的无障碍需求。WCAG 2.1指南建议使用相对单位进行文本大小调整,确保所有用户都能阅读内容。

About PX to REM Converter

The PX to REM Converter is an essential tool for modern web development that helps developers and designers convert pixel (px) values to relative em (rem) units. Our PX to REM Converter ensures precise calculations for responsive and accessible web designs that scale properly across different devices and user's preferences. Whether you're working on a new project or maintaining existing code, this PX to REM Converter streamlines your workflow.

Why Use Our PX to REM Converter?

The PX to REM Converter simplifies the process of converting pixel measurements to REM units. REM (Root EM) is a relative unit in CSS that references the font size of the root element (html). By default, 1rem equals 16px in most browsers, but our PX to REM Converter allows you to customize this base value for your specific needs. Using REM units instead of pixels creates more flexible and maintainable stylesheets.

How to Use the PX to REM Converter

  1. Enter your pixel value in the PX to REM Converter's input field
  2. Set your desired root font size in the PX to REM Converter (default is 16px)
  3. The PX to REM Converter will instantly calculate and display the equivalent REM value
  4. Copy the converted REM value to use in your CSS stylesheets

PX to REM Conversion Formula

The PX to REM Converter uses this simple formula for accurate conversions:

REM = pixels ÷ root font size

Example calculations using our PX to REM Converter:
• With 16px base: 24px = 24 ÷ 16 = 1.5rem
• With 16px base: 32px = 32 ÷ 16 = 2rem
• With 10px base: 24px = 24 ÷ 10 = 2.4rem

PX to REM Conversion Table (16px Base)

Reference table for common pixel values and their REM equivalents using the default 16px base font size:

PXREM (16px base)PXREM (16px base)
1px0.0625rem28px1.75rem
2px0.125rem32px2rem
4px0.25rem36px2.25rem
8px0.5rem40px2.5rem
10px0.625rem48px3rem
12px0.75rem56px3.5rem
14px0.875rem64px4rem
16px1rem72px4.5rem
18px1.125rem80px5rem
20px1.25rem96px6rem
24px1.5rem

Common Use Cases for PX to REM Converter

  • Converting font sizes from pixels to REM for better text scaling
  • Transforming margin and padding values for consistent spacing
  • Adapting container dimensions for responsive layouts
  • Setting up responsive breakpoints using REM units

Benefits of Using PX to REM Converter

  • Instant and accurate pixel to REM conversions
  • Better accessibility through relative unit usage
  • Simplified responsive design implementation
  • Consistent scaling across your entire website
  • Time-saving calculations for multiple values

Professional Tips for Using PX to REM Converter

  • Use the PX to REM Converter for all font-size declarations to maintain consistency
  • Convert spacing units (margin, padding) using the PX to REM Converter for better proportions
  • Combine REM values from the PX to REM Converter with other relative units for flexible layouts
  • Keep your root font size consistent throughout your project for predictable scaling
  • Save commonly used conversions from the PX to REM Converter for quick reference

Related Tools