Units in CSS

Source: Internet
Author: User

One, length units

In CSS, there are two types of length units, the absolute length units and the relative length units, respectively.

Absolute length Unit

Absolute length units are divided into in (inches), cm (cm), mm (mm), pt (LB), PC (PICA). Where in (inches), cm (cm), mm (mm) are the same as the units commonly used in practice. Focus on PT (LB), PC (PICA).

PT (LB): is a common unit in standard printing, and the length of 72pt is 1 inches.
PC (PICA): This is also a printing unit, the length of the 1pc is 12 lbs.

Absolute length units, although easy to understand, are seldom used in the design of Web pages.

Relative length Units

The relative length unit is the unit of length that is used most. including EM, ex, PX, the following are introduced separately.

EM is the value that defines the size of the text, which is the value of the Font-size property in the text. For example, if you define an element with a text size of 12pt, then 1em is 12pt for this element. The actual size of the unit EM is affected by the font size.

Ex and EM are similar, referring to the height of the letter x in the text, because the x height of the different fonts is different, so the actual size of the ex is affected by two factors such as font and font size.

PX is the commonly said pixel, which makes the most of the length units used in the web design. Divide the display into very small squares, each of which is a pixel. On the surface it seems to be easy to understand, in fact, the size of the PX is affected by the resolution of the screen, that is, the way the screen is divided into their respective ways. For example, a font of 100px size, if the display uses a resolution of 800x600 pixels, then the width of each word is 1/8 of the screen. If you set the display's resolution to 1024x768 pixels, it is also a 100px font with a width that is 1/10 of the width of the screen.

pros and cons:

PX: pixel-based units. Pixels are a useful unit, Because in any media you can guarantee that the difference of one pixel is really visible.

em: Commonly used to measure the length of a common unit (for example, margins and padding of element turnover), when used to specify the font size, the EM unit refers to the parent element's font size.
For example
<div style= "font-size=12px";
    <span style= "Fontsize=2em" > the word here is 24px</ Span>
</div>
If you use EM to specify a fill, the width of the fill is relative to the font size of the div element.

PT is often used in the printing industry, the meaning of the pound. Also used for page printing and typesetting.

The three px pixels above are pixel units, and EM is relative units, and PT is the absolute unit. Their respective benefits are that the PX can be used on the computer screen to achieve the desired effect, and it can achieve the desired effect on the printer and other high-resolution devices.
The advantages of EM are many, for example, on a page, you give the font size of a parent element, so that you can resize all the elements proportionally by adjusting an element. It can be scaled freely, for example, to make a scalable style sheet. The
PT is a fixed-length unit of measure that can be measured with a measuring device. Absolute units are limited because they are not scalable, and are usually used only when they are known to be used in that output medium. In most cases, relative units are best used.
So there should be no best, usually with PX and em this match is better. All hands, non-duplicated!!!

Suggestions:

Usually use PX to define the font, so the browser can not enlarge the font function, and most of the foreign sites are available under IE. Because

1, IE can not adjust those using PX as the unit font size;

2, the majority of foreign sites can be adjusted because of its use of EM as a font unit;

3, Firefox can adjust px and em, 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 has the following characteristics:

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, the body selector declaration font-size=62.5%;

2. Divide your original PX value by 10, then replace with EM as unit;

3. Recalculate the EM value of the enlarged font. 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.

However, 12px Kanji exception, is obtained by the above method 12px (1.2em) size of Chinese characters in IE does not equate directly with 12px defined font size, but slightly larger. This problem Jorux has been solved, just in the body selector to change 62.5% to 63% can be displayed normally. The reason may be that when IE processes Chinese characters, the accuracy of the value of floating point is limited. I wonder if there is any other explanation.


Units in CSS

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.