To put it simply, px is an absolute unit, and em and rem are relative units. Everyone is familiar with px. Here we mainly discuss em and rem.
Now there is the following paragraph:
<p>当斧头来到树林的时候,好多树都说,至少<span>它的手柄</span>是自己人</p>
When you do not set a font size for it, use the default size, which is 16px, and the effect is as follows:
em refers to the size relative to the parent element, that is to say, for p and span, its size is both 1em, where 1em = 16px.
We change the size of span to 2em, the effect is as follows:
span{ font-size: 2em; }
You can see that the size of span is twice as large as p, assuming that inside span There are also first-level tags:
<p>当斧头来到树林的时候,好多树都说,至少<span>它的<a>手柄</a></span>是自己人</p>
Now we know that the p tag size is 1em and the span size is 2em. If we want the a tag to be the same size as the p tag, what should it be set to?
Because em is a relative tag, it cannot be set to 1em, but 0.5em relative to its parent element span:
a{ font-size: 0.5em; }
Using em shows the relative size very well, but we also find that sometimes when we want to set the size of elements other than the parent element in the child element, we need to calculate the relative size. Rem solves this problem.
rem refers to the font size relative to the html root element.
Still with the example just now, we want to ensure that the font size of the a tag is the same as that of the p tag, and the size of the p tag is exactly 1 times that of the html, so we can also set the size of the a tag For 1rem:
a{ font-size: 1rem; }
The effect is the same.
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.