好像有一段时间没有写博客了……今天恰好总结一下rem的使用方法css
首先,先说一个常识,浏览器的默认字体高都是16px。步入正题-----〉html
目前,IE9+,Firefox、Chrome、Safari、Opera 的主流版本都支持了rem。算法
就算对不支持的浏览器,应对方法也很简单,就是多写一个绝对单位的声明。这些浏览器会忽略用rem设定的字体大小。浏览器
注意,rem是只相对于根元素htm的font-size,即只须要设置根元素的font-size,其它元素使用rem单位设置成相应的百分比便可;字体
例子:spa
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 }
通常状况下,是这样子使用的code
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的,因此在移动端作适配的时候,可使用这样的方法htm
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 }
这样子就能作到仅仅改变html的字体大小,让其余字体具备“响应式”啦。blog
又是午睡时间,若是本文有不正确的地方,请指出^_^rem