Home > Web Front-end > HTML Tutorial > The difference between px, em and rem in CSS Study Notes_html/css_WEB-ITnose

The difference between px, em and rem in CSS Study Notes_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:40:20
Original
1064 people have browsed it

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>
Copy after login

When you do not set a font size for it, use the default size, which is 16px, and the effect is as follows:

em

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; }
Copy after login

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>
Copy after login

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; }
Copy after login

rem

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; }
Copy after login

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.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template