Most of the domestic designers like to use PX, and most of the foreign sites like to use EM and REM, then what is the difference between the three, and what are the pros and cons of each?
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;}
Attention:
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 will find a Px,em,rem unit conversion tool
Address: http://pxtoem.com/
Foreign article:
To set the font in the page, we know there are two common, px and em.
Px
In Web page production, we generally use "px" to set our text, because he is more stable and accurate. However, there is a problem with this approach, when the user in the browser to browse our web page, he changed the browser font size (although the average person will not change the browser font size), then use our web page layout is broken, then put forward to use "em" to define the Web page font.
Em
Generally, it's a body
font-size
benchmark.
Common wording:
1 Body{2 font-size:62.5%;3 /*10÷16x100% = 62.5%*/4}5 H1{6 font-size:2.4em;7 /*2.4emx10 = 24px*/8}9 P{Ten font-size:1.4em; One /*1.4emx10 = 14px*/ A} - Li{ - font-size:1.4em; the /*1.4x? = 14px?*/ -}
Why "Li" "1.4em" is not "14px" will be a question mark? When using "Em" as a unit, you need to know the setting of its parent element, because "em" is a relative value, and it is a value relative to the parent element.
Calculation formula: 1÷ The Font-sizex of the parent element needs to convert the pixel value = em value
In this case, "1.4em" can be "14px", or "20px", or "24px", in short is an indeterminate value, then solve the problem, either you know the value of its parent element, or you can use "1em" in any child element.
Rem
REM:W3C Official website description is "font size of the root element", i.e. REM is relative to the root elements.
We only need to determine a reference value in the root element, set the size of the root element of the font, which can be completely based on your own needs, you can also refer to:
Too much to convert the trouble of the classmate, you can also go to http://pxtoem.com/this site to set
Common wording:
{ font-size: 62.5%; /* 10÷16x100% = 62.5% */} { font-size: 1.4rem; /* */} { font-size: 2.4rem; /* 2.4x10px = 24px */}
A basic font size of 62.5% (that is, 10px) is defined in the root element. Setting this value is mainly convenient for calculation, if not set, it will be "16px" as the benchmark). From the results above, we use "REM" as convenient as using "px", and also solve the difference between "px" and "em".
Note: Under Chrome, the default font is 12PX, you can set font-size:625%, and so on
Browser compatibility
IE9 above and other support CSS3 browser is sure to support, if you want to compatible with the low version of IE, it can be considered for the lower version of the IE9 browser, using PX to achieve.
Thoroughly understand the difference between unit px and Em,rem in CSS