The Rem plugin implements the rem adaptive layout for mobile pages, allowing the font size to be dynamically adjusted according to the screen size, making web pages adaptively displayed on screens of different sizes.
The plugin provides the following capabilities:
px
units in CSS to rem
.You can install the plugin using the following command:
You can register the plugin in the rsbuild.config.ts
file:
Name | Type | Default | Description |
---|---|---|---|
enableRuntime | boolean |
true |
Whether to generate runtime code to calculate and set the font size of the root element |
inlineRuntime | boolean |
true |
Whether to inline the runtime code to HTML. If set to false , the runtime code will be extracted into a separate convert-rem.[version].js file and output to the dist directory |
rootFontSize | number |
50 |
The root element font size |
maxRootFontSize | number |
64 |
The root element max font size |
widthQueryKey | string |
'' |
Get clientWidth from the url query based on widthQueryKey |
screenWidth | number |
375 |
The screen width for UI design drawings (Usually, fontSize = (clientWidth * rootFontSize) / screenWidth ) |
excludeEntries | string[] |
[] |
To exclude some page entries from injecting runtime code, it is usually used with pxtorem.exclude |
supportLandscape | boolean |
false |
Use height to calculate rem in landscape |
useRootFontSizeBeyondMax | boolean |
false |
Whether to use rootFontSize when large than maxRootFontSize |
pxtorem | object |
|
postcss-pxtorem options |
By default, rootFontSize is 50. So the CSS styles value are converted according to the ratio of 1rem : 50px
.
By default, Rsbuild converts all CSS properties from px to rem. If you want to convert only the font-size
property, you can setting pxtorem.propList
is ['font-size']
.
pxtorem.propList in addition to specifying which attributes need to be converted, you also can specify which elements are not converted through !
:
If you only want some individual CSS properties not to be converted to REM, you can also refer to the following writing method:
More info about postcss-pxtorem。
The formula for calculating the font size of the page root element is:
In a mobile browser with a screen width of 390, the default value for rootFontSize is 50 and the screenWidth of the UI design is 375.
The calculated font size for the root element of the page is 52 (390 * 50 / 375
).
At this point, 1 rem is 52px, 32px (0.64 rem) in the CSS style, the actual size in page is 33.28 px.
In the desktop browser, the page rootFontSize obtained from the calculation formula is often too large. When the calculated result large than the maxRootFontSize, the maxRootFontSize will used as the page rootFontSize.
In the desktop browser with a screen width of 1920, the calculated rootFontSize is 349, which exceeds the default maxRootFontSize of 64. 64 is used as the current root element font value.
The actual rootFontSize in effect for the page is calculated dynamically based on the current page. It can be seen by printing document.documentElement.style.fontSize
or obtained by window.ROOT_FONT_SIZE
.
By default, the actual rootFontSize of the page will be dynamically calculated based on the situation of the current page. If you want to specify the actual rootFontSize of the page, you can turn off the enableRuntime
configuration and set it in Customized html template and inject document.documentElement.style.fontSize = '64px'
by yourself.
.css
file to see if the value of the corresponding property is converted from px to rem.document.documentElement.style.fontSize
.