rem (font size of the root element) refers to the unit of font size relative to the root element. Simply put, it is a relative unit. When you see rem, you will definitely think of the em unit. Em (font size of the element) refers to the unit of font size relative to the parent element. They are actually very similar, except that one calculation rule relies on the root element and the other relies on the parent element for calculation.
As mentioned above, rem is adapted through the root element. The root element in the web page refers to htmlWe set the font size of html You can control the size of rem,for example.
<span style="font-size: 14px;">html{<br> font-size:20px;<br>}<br>.btn {<br> width: 6rem;<br> height: 3rem;<br> line-height: 3rem;<br> font-size: 1.2rem;<br> display: inline-block;<br> background: #06c;<br> color: #fff;<br> border-radius: .5rem;<br> text-decoration: none;<br> text-align: center; <br>}</span>
We control the font size of our dom elements by changing the size of font-size in HTML.
In order to adapt to the compatibility of different resolutions, the font-size in HTML can be dynamically generated through js. We can also set it through media query for mainstream models. For example, here is the code below. If you don’t know much about media query, you can study http://www.w3cplus.com/content/css3-media-queries
<span style="font-size: 14px;">html{font-size: 20px;}<br>@media only screen and (min-width: 320px){<br> html{font-size: 20px !important;}<br>}<br>@media only screen and (min-width: 350px){<br> html{font-size: 22.5px !important;}<br>}<br>@media only screen and (min-width: 365px){<br> html{font-size: 23px !important;}<br>}<br>@media only screen and (min-width: 375px){<br> html{font-size: 23.5px !important;}<br>}<br>@media only screen and (min-width: 390px){<br> html{font-size: 24.5px !important;}<br>}<br>@media only screen and (min-width: 400px){<br> html{font-size: 25px !important;}<br>}<br>@media only screen and (min-width: 428px){<br> html{font-size: 26.8px !important;}<br>}<br>@media only screen and (min-width: 432px){<br> html{font-size: 27.4px !important;}<br>}<br>@media only screen and (min-width: 481px){<br> html{font-size: 30px !important;}<br>}<br>@media only screen and (min-width: 569px){<br> html{font-size: 35px !important;}<br>}<br>@media only screen and (min-width: 641px){<br> html{font-size: 40px !important;}<br>}</span>
. Of course, in setting html When font-size is used, we may also see this writing method, html { font-size: 62.5% }. This is mainly to facilitate the conversion between em and px. The initial value of em is 1em=16px. Obviously, if this is the case, for example, 1.2em = 19.2px, but we rarely see the size represented by 19.2px when setting it. , that is, when using px to express the size, the value does not have decimal places. When body{font-size: is set 62.5%;}, 1em = 16px*62.5% = 10px, 1.2em = 12px, isn’t this much simpler and more accurate~~.
# I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
##Commonly used code organization summary
Detailed explanation of H5 Canvas use cases
The above is the detailed content of REM relative unit use case sharing. For more information, please follow other related articles on the PHP Chinese website!