跪拜 Guibai
← All articles
Frontend · Interview · Vue.js

Mobile REM Adaptation: The Conversion Pipeline Behind rootValue and Dynamic Root Font Size

By 某哈压力大 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Developers who confuse rootValue with the html font-size will ship layouts that break on resizing or waste time chasing a phantom conversion bug. Knowing the two-stage pipeline lets you configure rootValue from the source CSS scale and predict exactly how each rem will render at any viewport width.

Summary

The common mobile adaptation scheme of postcss-pxtorem plus a dynamic root font size works in two separate stages. At build time, the PostCSS plugin converts any eligible px value into rem by dividing by a configured rootValue, typically 37.5 for a 375-based design. At runtime, a script or library like amfe-flexible sets the html font-size to the layout viewport width divided by 10. The browser then computes every rem declaration from that font size, so a card declared as 4rem grows to 165.6px on a 414px viewport and shrinks to 150px on 375px. The two numbers equal each other only at the baseline width; change the viewport and the html font-size moves while rootValue stays fixed.

The article stresses that rootValue is not the html font-size and that the 10 multiplier is a project convention, not a rem rule. The chosen rootValue must match the dimension scale actually written in the source CSS, whether that is 375, 750, or a halved 750. The plugin never re-runs when the window resizes, so all responsiveness comes from the runtime root font size. The piece also warns that propList: ["*"] does not convert every px in the project, that px inside media query conditions is controlled by mediaQuery, and that inline styles and JavaScript numbers stay untouched. Font scaling, layout overflow, and readability need separate attention, because rem only handles size relationships.

Takeaways
— postcss-pxtorem converts source px to rem at build time by dividing by rootValue; it does not run when the window resizes.
— The root font size is set at runtime as layout viewport width / 10, and the browser recalculates rem sizes whenever it changes.
— Displayed size in CSS px equals source px value divided by rootValue, then multiplied by the current root font size.
— For a 375-based design, rootValue is typically 37.5; for a 750-based design, it is typically 75, but only if source px values are written in that scale.
— If a 750 design is halved before writing into source, rootValue stays 37.5; the decision depends on the source scale, not the design image width or DPR.
— The viewport meta tag with width=device-width and initial-scale=1.0 is needed to set the layout viewport to the device's CSS pixel width.
— propList: ["*"] does not convert all px: inline styles, JavaScript numbers, and media query conditions are excluded by default, and exclude, selectorBlackList, and minPixelValue still apply.
— amfe-flexible 2.2.1 sets root font size based on clientWidth / 10 and listens to resize and pageshow, but it does not set the viewport meta tag.
— Using rem does not automatically make fonts scale with the user's browser default font size when root font size is a fixed viewport-derived px value.
— rem only handles size relationships; layout issues like overflow from fixed heights, long text, and font readability still need Flexbox, Grid, wrapping, and dedicated font evaluation.
— The 10 in viewport width / 10 is a project convention; other conventions require adjusting both rootValue and the root font size rule in tandem.
Conclusions

The article's sharpest point is that rootValue and the root font size are two different numbers that happen to coincide at the design baseline; conflating them leads to debugging the wrong layer of the stack.

The choice between 37.5 and 75 is a source-code convention issue, not a device-pixel-ratio matter, which is a recurring source of misunderstanding on interview questions and in real projects.

Treating rem as a viewport unit rather than a root-font-size unit is a common mental model error; the dynamically updated root font size is what creates the responsiveness, and that distinction is easy to miss when reading code.

Concepts & terms
rem
A CSS unit equal to the computed font size of the root element (html). 1rem equals that font size, so if html has font-size: 16px, 1rem is 16px. It is not a viewport-based unit; scaling with the viewport requires the root font size to change.
rootValue
A configuration option in postcss-pxtorem that acts as the divisor when converting source px values to rem. For example, rootValue: 37.5 turns 150px into 4rem. It is applied at build time and is not the same as the html font-size set at runtime.
postcss-pxtorem
A PostCSS plugin that converts px units in CSS to rem units according to a specified rootValue configuration. It runs during development or production builds and does not change the browser's root font size.
dynamic root font size
A runtime JavaScript logic that sets the html element's font-size based on the layout viewport width, typically viewport width divided by 10. This is what makes rem values scale proportionally when the viewport resizes.
From the discussion
Featured comments
组合搭配函数

The separation of rootValue and root font size is spot on; the two 37.5 values look identical but aren't the same thing. For the 750 design draft, choosing 75 or 37.5, I used to make assumptions without thinking.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗