首先,先说一个常识,浏览器的默认字体高都是16px
- 使用方法
注意,rem是只相对于根元素htm的font-size,即只需要设置根元素的font-size,其它元素使用rem单位设置成相应的百分比即可;
1 /*16px * 312.5% = 50px;*/
2 html{font-size: 312.5%;}
1 /*50px * 0.5 = 25px;*/
2 body{
3 font-size: 0.5rem;
4 font-size\0:25px;
5 }
一般情况下,是这样子使用的
1 html{font-size:62.5%;}
2 body{font-size:12px;font-size:1.2rem ;}
3 p{font-size:14px;font-size:1.4rem;}
- 优点
用一个东西肯定要知道它的好处啦,由于其他字体大小都是基于html的,所以在移动端做适配的时候,可以使用这样的方法
1 @media only screen and (min-width: 320px){
2 html {
3 font-size: 62.5% !important;
4 }
5 }
6 @media only screen and (min-width: 640px){
7 html {
8 font-size: 125% !important;
9 }
10 }
11 @media only screen and (min-width: 750px){
12 html {
13 font-size: 150% !important;
14 }
15 }
16 @media only screen and (min-width: 1242px){
17 html {
18 font-size: 187.5% !important;
19 }
20 }