Difference between unit px and em in css

Source: Internet
Author: User

The unit of relative length of pixel (pixel) is relative to the screen resolution of the display (derived from the css tutorial 2.0 manual ).

The unit of relative em length is relative to the font size of the text in the current object. For example, the font size of the text in the current row is not set manually, the default font size relative to the browser (derived from the css2.0 manual ).

The default font height of any browser is 16 PX. All unadjusted browsers match: 1em = 16px. Then 12px = 0.75em, 10px = 0.625em. To simplify font-size conversion, you must declare font-size = 62.5% in the body selector in css, which changes the em value to 16px * 62.5% = 10px, in this case, 12px = 1.2em and 10px = 1em. That is to say, you only need to divide your original px value by 10 and change it to em as the unit.

Em has the following features:

1. The em value is not fixed;

2. em inherits the font size of the parent element.

Therefore, when writing css, pay attention to the following two points:

1. Declare font-size = 62.5% in the body selector;
2. Divide your original px value by 10 and change it to em as the unit;
3. Recalculate the em values of the enlarged fonts to avoid repeated declaration of the font size.

That is, to avoid 1.2*1.2 = 1.44. For example, if you declare in # content that the font size is 1.2em, the font size of p can only be 1em, not 1.2em, because this em is not em, it becomes 1em = 12px because it inherits the font of # content.

Most domestic designers like px, while most foreign websites like em. What are the differences between the two and their advantages and disadvantages?

1. ie cannot adjust the font size that uses px as the unit;

2. The reason why most foreign websites can be adjusted is that they use em as the font unit;

3. firefox can adjust px and em, but more than 96% of Chinese users use IE browser (or kernel ).

The relative length unit of pixel (pixel) is relative to the screen resolution of the display (derived from the css2.0 manual ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.