http://blog.csdn.net/huang100qi/article/details/29845359
rem
是CSS3中新增加的一个单位值,他和em
单位一样,都是一个相对单位。不同的是em
是相对于元素的父元素的font-size
进行计算;rem
是相对于根元素html
的font-size
进行计算。这样一来rem
就绕开了复杂的层级关系,实现了类似于em
单位的功能。
Rem的使用
前面说了em
是相对于其父元素来设置字体大小的,这样就会存在一个问题,进行任何元素设置,都有可能需要知道他父元素的大小,在我们多次使用时,就会带来无法预知的错误风险。而rem
是相对于根元素<html>
,这样就意味着,我们只需要在根元素确定一个参考值,这个参考值设置为多少,完全可以根据您自己的需求来定。
假设就使用浏览器默认的字号16px
,来看一些px
单位与rem
之间的转换关系:
| px | rem |
------------------------
| 12 | 12/16 = .75 |
| 14 | 14/16 = .875 |
| 16 | 16/16 = 1 |
| 18 | 18/16 = 1.125 |
| 20 | 20/16 = 1.25 |
| 24 | 24/16 = 1.5 |
| 30 | 30/16 = 1.875 |
| 36 | 36/16 = 2.25 |
| 42 | 42/16 = 2.625 |
| 48 | 48/16 = 3 |
-------------------------
如果你要设置一个不同的值,那么需要在根元素<html>
中定义,为了方便计算,时常将在<html>
元素中设置font-size
值为62.5%
:
- <code class="css" style="display:block; overflow-x:auto; font-family:consolas; padding:0.5em"><span class="tag" style="color:rgb(133,153,0)">html</span> <span class="rules"><span class="rules">{
- <span class="rule"><span class="attribute" style="color:rgb(181,137,0)"><span class="rule"><span class="attribute">font-size</span></span></span><span class="rule">:<span class="value"><span class="value"> <span class="number" style="color:rgb(42,161,152)"><span class="number">62.5</span></span>%;</span></span></span><span class="value"></span></span> <span class="comment" style="margin-bottom:0px!important; display:inline!important; vertical-align:top; color:rgb(88,110,117); font-style:italic"><span class="comment" style="vertical-align:top; margin-bottom:0px!important; display:inline!important">/* 10 ÷ 16 × 100% = 62.5% */</span></span>
- <span class="rule"><span class="rule">}</span></span></span><span class="rule"></span></span>
- </code>
相当于在<html>
中设置font-size
为10px
,此时,上面示例中所示的值将会改变:
| px | rem |
-------------------------
| 12 | 12/10 = 1.2 |
| 14 | 14/10 = 1.4 |
| 16 | 16/10 = 1.6 |
| 18 | 18/10 = 1.8 |
| 20 | 20/10 = 2.0 |
| 24 | 24/10 = 2.4 |
| 30 | 30/10 = 3.0 |
| 36 | 36/10 = 3.6 |
| 42 | 42/10 = 4.2 |
| 48 | 48/10 = 4.8 |
-------------------------
由于rem
是CSS3中的一个属性,很多人首先关注的就是浏览器对他的支持度,我截了一张caniuse对rem
属性的兼容表:
从上图可以清楚的知道,rem
在众多浏览器中都已得到很好的支持,如果您的项目不用考虑IE低版本的话,你就可以放心的使用了,如果您的项目在IE低版本中还占有不少的比例,那么你还在担心使用rem
不能兼容,而不敢使用。其实是没有必要的,可以针对低版本的IE浏览器做一定的处理:
- <code class="css" style="display:block; overflow-x:auto; font-family:consolas; padding:0.5em"><span class="tag" style="color:rgb(133,153,0)">html</span> <span class="rules"><span class="rules">{ <span class="rule"><span class="attribute" style="color:rgb(181,137,0)"><span class="rule"><span class="attribute">font-size</span></span></span><span class="rule">:<span class="value"><span class="value"> <span class="number" style="color:rgb(42,161,152)"><span class="number">62.5</span></span>%;</span></span></span><span class="value"></span></span> <span class="rule"><span class="rule">}</span></span></span><span class="rule"></span></span>
- <span class="tag" style="color:rgb(133,153,0)">body</span> <span class="rules"><span class="rules">{ <span class="rule"><span class="attribute" style="color:rgb(181,137,0)"><span class="rule"><span class="attribute">font-size</span></span></span><span class="rule">:<span class="value"><span class="value"> <span class="number" style="color:rgb(42,161,152)"><span class="number">14</span></span>px;</span></span></span><span class="value"></span></span> <span class="rule"><span class="attribute" style="color:rgb(181,137,0)"><span class="rule"><span class="attribute">font-size</span></span></span><span class="rule">:<span class="value"><span class="value"> <span class="number" style="color:rgb(42,161,152)"><span class="number">1.4</span></span>rem;</span></span></span><span class="value"></span></span> <span class="rule"><span class="rule">}</span></span></span><span class="rule"></span></span> <span class="comment" style="margin-bottom:0px!important; display:inline!important; vertical-align:top; color:rgb(88,110,117); font-style:italic"><span class="comment" style="vertical-align:top; margin-bottom:0px!important; display:inline!important">/* =14px */</span></span>
- <span class="tag" style="color:rgb(133,153,0)">h1</span> <span class="rules"><span class="rules">{ <span class="rule"><span class="attribute" style="color:rgb(181,137,0)"><span class="rule"><span class="attribute">font-size</span></span></span><span class="rule">:<span class="value"><span class="value"> <span class="number" style="color:rgb(42,161,152)"><span class="number">24</span></span>px;</span></span></span><span class="value"></span></span> <span class="rule"><span class="attribute" style="color:rgb(181,137,0)"><span class="rule"><span class="attribute">font-size</span></span></span><span class="rule">:<span class="value"><span class="value"> <span class="number" style="color:rgb(42,161,152)"><span class="number">2.4</span></span>rem;</span></span></span><span class="value"></span></span> <span class="rule"><span class="rule">}</span></span></span><span class="rule"></span></span> <span class="comment" style="margin-bottom:0px!important; display:inline!important; vertical-align:top; color:rgb(88,110,117); font-style:italic"><span class="comment" style="vertical-align:top; margin-bottom:0px!important; display:inline!important">/* =24px */</span></span>
- </code>
这样一来解决了IE低版本的不能兼容的问题,但生出另一个不足地方,就是增加了代码量。必竟鱼和熊掌很多时候不能兼得嘛。