Differences of font sizes such as pxptemrem in CSS

Source: Internet
Author: User
In CSS, you can set the font size in many different ways. Generally, these units are divided into two categories: absolute units (absolute) and relative units (relative ). In most cases, the absolute unit is a fixed value relative to some actual measurements, that is, once they are set, the font size of other elements will not change. The relative unit does not have a fixed measurement value, but is determined by the size of the parent element. Their sizes change according to the change of the relevant elements. This article describes the usage and differences of font sizes in css. Common Unit

The following is a simple arrangement of these units:

Unit Type Description
Px Absolute 1 Viewport Pixel
Pt Absolute 1pt = 1/72 inch
Pc Absolute 1 pc = 12pt
% Relative Font size relative to the parent Element
Em Relative Font size relative to the parent Element
Rem Relative (That is, root em) font size relative to html tags
Keyword Relative Xx-small, x-small, small, medium, large, x-large, xx-large
Vw Relative Equivalent to 1/100 of the Viewport width.
Vl Relative Equivalent to 1/100 of the Viewport height.
Vmin Relative Equivalent to 1/100 of the relatively small length in the Viewport height and width
Vmax Relative Equivalent to 1/100 of the relatively large length in the Viewport height and width

Here we mainly focus on these units:px,pt,%,em,remAndvw.

What are the differences between them?

It is hard to understand the differences between these units in terms of concept, so we will use some examples to illustrate them.

Example 1. default settings

When you do not set the font size, HTML provides a default size setting. In most browsersAndThe default font size in the label is 100%. No concept? Let's look at this equation:

100% = 1em = 1rem = 16px = 12pt

Still don't understand? For example, if you give

Set the font size to 100%.

Set to 16px. The two images are displayed on the screen.

The font size is the same, and the font size set in several different units is listed. It can be seen that the font size is the same:

ChangeThe absolute unit and relative unit are obviously different. IfIf it is set to html {font-size: 200%}, all

. The effect is as follows:

This is the main advantage of the relative unit. With this feature of the relative unit, you can design a truly responsive page. All you have to do is modifyFont size.

Example 2. rem and em (or %)

Em (or %) requires the font size of the parent element to calculate the size:

html {   font-size: 100% /* =16px */}body {  font-size: 2em; /* =32px */}p {  font-size: 1em; /* =32px */  /* font-size: 0.5em; =16px */}

Because

YesAndYes, So

The em and % in will be twice the previous one.

When you add an em unit to an element, consider the font size of all parent elements. As you can see, this can easily confuse people.
Using rem can solve this problem well. Rem only needs to calculateDoes not need to consider the parent element. The following code is used:

html {   font-size: 100% /* =16px */}body {  font-size: 2rem; /* =32px */}p {  font-size: 1rem; /* =16px */}

Using rem gives you the same scaling capability as em/%, but you don't have to consider the complex nesting relationships.

Example 3. Viewport width

Vw is a new unit proposed in CSS3. The Viewport width is used to calculate the font size. In this way, a more flexible responsive font can be designed.
Although this unit seems very suitable for responsive design, I personally am not very keen on it. In the process of using vw, I cannot properly control the font size, either too large or too small.

My approach

When I write this article, I only use px as the unit. This is because most browsers allow users to zoom in pages.
However, I found this method with some limitations. Although my font size looks okay on small and medium screens, it will be optimized on the screen better. Although users can set their own magnified attributes, we hope to reduce the user's work as much as possible.
My solution is to use rem and px as the standby unit.

html {  font-size: 62.5%; /* sets the base font to 10px for easier math */}body {  font-size: 16px;  font-size: 1.6rem;    /* sets the default sizing to make sure nothing is actually 10px */}h1 {  font-size: 32px;  font-size: 3.2rem;}

You can scale up the font size as follows:

@media screen and (min-width: 1280px) {  html {    font-size: 100%;  }}

This scheme uses px as the standby unit because rem does not support IE8 and earlier versions. There is a problem with this solution, that is, when the base font size is changed like above, it does not play a role in the backup font size. However, I don't think this is a big problem, because the ability to match the size of a large device is just a icing on the cake, not a core function.

Summary

There are several common font settings: px, %, em, and rem.

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.