rem
Mobile terminal adapter:
In the mobile terminal (mobile terminal, Pad side), uneven size of the apparatus. If you want to finish a set of code that can run properly on all mobile devices, the use of native px
units set up is not enough. For example: iPhone6
the size 375px
, if we want a box that occupies half of the width, then the width of the box will be set 187.5px
, but not all mobile phones are the width 375px
, so it will cause problems. At this time we can rem
solve this problem to.
What is rem
:
em
: The current element font size font size relative to the parent label. such as:<div style="font-size:16px;"> <span style="font-size:2em">你好</span> </div>
div
the font size16px
, and thespan
label is used because2em
, thus twice the parent tag font size, is32px
.rem
: Withem
similar, but this time the reference element is not a parent element, but the root element, which is thehtml
size of the element. such as:<html style="font-size:14px"> <div style="font-size:16px;"> <span style="font-size:2rem">你好</span> </div> </html>
span
label font size ishtml
twice the size of the font tag, that is28px
.
rem
Adaptation principle:
rem
Although it is used to set the font size, but may extend to other properties of the set size. Use rem
we can achieve geometric scaling. For example, I will be the average width of a page is divided into 100
parts unit=windowWidth/100
, then let html
's font-size
equal unit
, then the following problems can be achieved:
- To achieve half the width of the browser, we can
50rem
be achieved (rem
a unit). - Set the font size is
16px
converted intorem
that:(16/unit)rem
.
In the Vue-cli
implementation rem
layout:
In the use of vue-cli
project creation, we can through some third-party packages to facilitate the realization rem
of the layout. To install two packages, postcss-pxtorem
namely: lib-flexible
, . Install command npm install --save-dev postcss-pxtorem lib-flexible
to install. After installing the package, also you need to configure two places:
package.json
:"postcss": { "plugins": { "autoprefixer": {}, "postcss-pxtorem": { "rootValue": 37.5, "propList": [ "*" ], "selectorBlackList": [ "van-*" ] } } }
main.js
:import "lib-flexible"
So that later when writing unit, you do not need to be converted into rem
a direct write px
on it, postcss-pxtorem
automatically we will write px
converted into rem
. And lib-flexible
based on the current size, to adjust html
the font-size
adaptation.