rem详解及使用方法 - beidan
May 22, 2016 pm 05:19 PM好像有一段时间没有写博客了……今天刚好总结一下rem的使用方法
首先,先说一个常识,浏览器的默认字体高都是16px。步入正题-----〉
- 兼容性:
目前,IE9+,Firefox、Chrome、Safari、Opera 的主流版本都支持了rem。
就算对不支持的浏览器,应对方法也很简单,就是多写一个绝对单位的声明。这些浏览器会忽略用rem设定的字体大小。
- 使用%单位方便使用
- 使用方法
注意,rem是只相对于根元素htm的font-size,即只需要设置根元素的font-size,其它元素使用rem单位设置成相应的百分比即可;
例子:
<span style="color: #008080;">1</span> <span style="color: #000000;">/*16px * 312.5% = 50px;*/ </span><span style="color: #008080;">2</span> html{font-size: 312.5%;}
<span style="color: #008080;">1</span> <span style="color: #000000;">/*50px * 0.5 = 25px;*/ </span><span style="color: #008080;">2</span> <span style="color: #000000;">body{ </span><span style="color: #008080;">3</span> <span style="color: #000000;"> font-size: 0.5rem; </span><span style="color: #008080;">4</span> <span style="color: #000000;"> font-size\0:25px; </span><span style="color: #008080;">5</span> }
一般情况下,是这样子使用的
<span style="color: #008080;">1</span> <span style="color: #000000;">html{font-size:62.5%;} </span><span style="color: #008080;">2</span> <span style="color: #000000;">body{font-size:12px;font-size:1.2rem ;} </span><span style="color: #008080;">3</span> p{font-size:14px;font-size:1.4rem;}
- 优点
用一个东西肯定要知道它的好处啦,由于其他字体大小都是基于html的,所以在移动端做适配的时候,可以使用这样的方法
<span style="color: #008080;"> 1</span> <span style="color: #000000;">@media only screen and (min-width: 320px){ </span><span style="color: #008080;"> 2</span> <span style="color: #000000;"> html { </span><span style="color: #008080;"> 3</span> <span style="color: #000000;"> font-size: 62.5% !important; </span><span style="color: #008080;"> 4</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 5</span> <span style="color: #000000;">} </span><span style="color: #008080;"> 6</span> <span style="color: #000000;">@media only screen and (min-width: 640px){ </span><span style="color: #008080;"> 7</span> <span style="color: #000000;"> html { </span><span style="color: #008080;"> 8</span> <span style="color: #000000;"> font-size: 125% !important; </span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> } </span><span style="color: #008080;">10</span> <span style="color: #000000;">} </span><span style="color: #008080;">11</span> <span style="color: #000000;">@media only screen and (min-width: 750px){ </span><span style="color: #008080;">12</span> <span style="color: #000000;"> html { </span><span style="color: #008080;">13</span> <span style="color: #000000;"> font-size: 150% !important; </span><span style="color: #008080;">14</span> <span style="color: #000000;"> } </span><span style="color: #008080;">15</span> <span style="color: #000000;">} </span><span style="color: #008080;">16</span> <span style="color: #000000;">@media only screen and (min-width: 1242px){ </span><span style="color: #008080;">17</span> <span style="color: #000000;"> html { </span><span style="color: #008080;">18</span> <span style="color: #000000;"> font-size: 187.5% !important; </span><span style="color: #008080;">19</span> <span style="color: #000000;"> } </span><span style="color: #008080;">20</span> }
这样子就能做到仅仅改变html的字体大小,让其他字体具有“响应式”啦。
又是午睡时间,如果本文有不正确的地方,请指出^_^

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?

How do I use HTML5 form validation attributes to validate user input?

What is the purpose of the <iframe> tag? What are the security considerations when using it?

How to efficiently add stroke effects to PNG images on web pages?

What is the purpose of the <meter> element?

What are the best practices for cross-browser compatibility in HTML5?

What is the purpose of the <datalist> element?

What is the purpose of the <progress> element?
