CSS size unit px em rem conversion and detailed

Source: Internet
Author: User

CSS size unit px em rem conversion and detailed

PX Features
1. IE cannot adjust the font size using PX as the unit;
2. Most of the foreign sites can be adjusted because they use EM or REM as font units;
3. Firefox can adjust PX and em,rem, but more than 96% of Chinese netizens use IE browser (or kernel).

px Pixels (Pixel). Relative length units. Pixel px is relative to the display screen resolution. (quoted from CSS2.0 manual)

EM is the relative length unit. The font size relative to the text within the current object. If the font size of the current inline text is not artificially set, the default font size is relative to the browser. (quoted from CSS2.0 manual)

The default font height for any browser is 16px. All non-adjustable browsers are compliant with: 1EM=16PX. So 12px=0.75em,10px=0.625em. To simplify the conversion of font-size, you need to declare font-size=62.5% in the body selector in the CSS, which makes the EM value 16px*62.5%=10px, so 12px=1.2em, 10px=1em, That means you just have to divide your original PX value by 10 and then put EM in as the unit.


EM features
1. The value of EM is not fixed;
2. Em inherits the font size of the parent element.

So when we are writing CSS, we need to pay attention to two points:
1. Declare font-size=62.5% in the body selector;
2. Divide your original PX value by 10 and then replace it with EM as the unit;
3. Recalculate the EM values of the enlarged fonts. Avoid duplicate declarations of font size.

This is to avoid the 1.2 * 1.2 = 1.44 phenomenon. For example, when you declare a font size of 1.2em in #content, you can only have 1em when declaring the font size of p, not 1.2em, because this em is not em, it becomes 1em=12px because it inherits the font height of #content.

REM Features
REM is a new relative unit of CSS3 (root em, root em), and this unit has aroused widespread concern. What is the difference between this unit and EM? The difference is that when you use REM to set the font size for an element, it is still relative size, but relative to the HTML root element. This unit is a combination of relative size and absolute size of the advantages in one, through it can only modify the root elements in proportion to adjust all font size, but also to avoid the size of the font-layer composite chain reaction. Currently, in addition to IE8 and earlier versions, all browsers support REM. For browsers that do not support it, the workaround is simple enough to write an absolute unit statement. These browsers ignore font sizes that are set with REM. Here is

An example:
p {font-size:14px; font-size:.875rem;}

Note: Choosing which font units to use is primarily up to your project, and if your user base is using the latest version of the browser, it is recommended to use REM, if you want to consider compatibility, use PX, or both.

Here you can see whether the various browsers are compatible with REM, http://caniuse.com/#search =rem

REM is relative to the root element

General browser default font size is 16px, below is the default 16px and REM conversion relationship:

| px | rem |
------------------------
| 12 | 12/16 =. 75 |
| 14 | 14/16 =. 875 |
| 16 | 16/16 = 1 |
| 18 | 18/16 = 1.125 |
| 20 | 20/16 = 1.25 |
| 24 | 24/16 = 1.5 |
| 30 | 30/16 = 1.875 |
| 36 | 36/16 = 2.25 |
| 42 | 42/16 = 2.625 |
| 48 | 48/16 = 3 |
-------------------------

For ease of calculation, the font-size value of 62.5% is often set in the

The equivalent of setting Font-size to 10px in

| px | rem |
-------------------------
| 12 | 12/10 = 1.2 |
| 14 | 14/10 = 1.4 |
| 16 | 16/10 = 1.6 |
| 18 | 18/10 = 1.8 |
| 20 | 20/10 = 2.0 |
| 24 | 24/10 = 2.4 |
| 30 | 30/10 = 3.0 |
| 36 | 36/10 = 3.6 |
| 42 | 42/10 = 4.2 |
| 48 | 48/10 = 4.8 |
-------------------------

Since REM is a property of CSS3, the first thing many people are concerned about is the browser's support for him, and I truncated a caniuse compatibility table for REM properties:

CSS size unit px em rem conversion and detailed

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.