The CSS text size units you should know are PX, EM, PT, and csspx.

Source: Internet
Author: User

The CSS text size units you should know are PX, EM, PT, and csspx.
Summary: Here we reference Jorux's article "CSS needs to be rewritten for 95% of Chinese websites". The question is a bit scary, but it is indeed a defect in the production of domestic webpages. I have never been clear about the relationship and features between px and em. I have learned a lot since I saw it. In general, px is used to define the word body ,...

  

 

Here we reference Jorux's article "CSS needs to be rewritten for 95% of Chinese websites". The question is a bit scary, but it is indeed a defect in the production of domestic Web pages. I have never been clear about the relationship and features between px and em. I have learned a lot since I saw it. In general, px is used to define the font, so the font amplification function of the browser cannot be used, and most foreign websites can use it in IE. Because:

 

  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 ).

 

Pixel (Pixel) is the unit of relative length, and Pixel is relative to the display screen resolution. (Derived from the CSS2.0 manual)

Em is the unit of relative length, relative to the font size of the text in the current object. If the font size of the text in the row is not set manually, the default font size is relative to that of the browser. (Derived from the CSS2.0 manual)

 

The font unit uses em to support font scaling in IE6. Press ctrl + scroll wheel on the page, and the font in px is not reflected by the website.

  PxIt is an absolute unit and does not support IE scaling.

  EmIs the relative unit, and the text in the webpage can be scaled up or down. Use em for line-height and vertical height. Ensure integrity during scaling.

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, so that 12px = 1.2em, 10px = 1em, that is, you only need to divide your original px value by 10, and then change to em as the unit.

Em has the following features:

  1. The em value is not fixed;

2. em will continue the font size of the parent element.

Therefore, when writing CSS, note the following:

  1. Declare Font-size = 62.5% in the body selector; (Font-size = 63%; For ie6 compatibility)

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. Avoid repeated statement of 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 is changed to 1em = 12px because the font of # content is higher.

Except for the 12 PX Chinese character, the 12 PX (1.2em) Chinese Character obtained by the above method is not equal to the font size defined by 12 PX directly in IE, but a little larger. This problem has been solved by Jorux. You only need to change 62.5% to 63% in the body selector to display it normally. The reason may be that when IE processes Chinese characters, the floating point value accuracy is limited. I don't know if there are any other explanations.

 

  Unit pt description

In the printed layout, point is an absolute value, which is equal to 1/72 inch. It can be measured using a ruler, physical inches. But in css, pt does not mean so. Because our display is divided into pixels, a single pixel can only have one color (to simplify it, we will not discuss the next pixel anti-sawtooth technology). It should be displayed on the screen, you must first convert the length in pt units to the length in pixels. The conversion media is DPI (in fact, the so-called DPI here, is the term used in operating systems and browsers, I .e., PPI, pixels per inch, which is different from DPI in scanners, printers, and digital cameras ).

 

For example, in any operating system, the default DPI of Firefox is 96, so 9pt = 9*1/72*96 = 12px.

Therefore, although "I" and "1pt" in "DPI" are equal to 1/72 inch ", they do not represent physical inches, but the two units are equal to each other, in this case, it is reduced in the multiplication.

How can we calculate the actual physical length? Take out a ruler and measure the visible width of your monitor (11.2992 inch here). divide it by the horizontal resolution (1024 pixels here). The physical length of each pixel is obtained.

 

Now we can answer this question: how much space does the 9pt font occupy on the webpage? The answer is: 9*1/72*96*11.2992/1024 = 0.1324 inch = 0.3363 cm.

Although pt is an absolute unit, it is only for the output device. It is a very useful font unit in word and abits. Regardless of the display resolution, the results are the same on paper.

However, Web pages are mainly used for screen display, rather than printing. Px can accurately represent the position and size of an element on the screen.

Of course. When dpi is 96, 9pt = 12px.

 

  With px, em, %, and pt exchange tables

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.