What does em mean in css

青灯夜游
Release: 2023-01-04 09:34:20
Original
19353 people have browsed it

em is a relative length unit in CSS, relative to the font size of the text in the current object; if the current font size of the inline text has not been manually set, it is relative to the browser's default font size. It can be used to set width, height, line-height, margin, border and other styles.

What does em mean in css

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

There are many units in CSS, the commonly used px is the absolute unit, and em is the relative unit. Under the premise of responsiveness and mobile terminals, using em can more conveniently and quickly adjust a series of attributes such as font size, width, margin, and border of web documents and HTML elements at once. It can be said that in some aspects, using em as a Units are more flexible than px.

1. What is em


1. The length of em

em It is a relative unit in CSS, and its unit length is determined based on the vertical length of the element's text. It can be used to set width, height, line-height, margin, padding, border and other styles.

1em=元素中文本的1个垂直高度
Copy after login

According to the above rules: if the size of the text in the element is 16px, then 1em=16px; if the size of the text in the element is 20px, then 1em=20px...

2.em and HTML text size default value

The text in the browser generally defaults to 16px, that is, by default:

1em=16px
Copy after login

How to change this What about settings? Just explicitly set the font-size of the body element. eg:

body {
    font-size: 24px;
    width: 10em;    /* 10em = 24px * 10 = 240px */
}
Copy after login

3.em and inheritance

In CSS, if an element does not set font-size, then its font-size value is its parent element The font-size value is easy to understand, it is just a simple inheritance. eg:

<style>
    body {
        font-size: 12px;
    }
    div {
        /* 该元素没有设置font-size,因此继承了父元素的font-size: 12px; */
        width: 10em;    /* 10em = 12px * 10 = 120px */
    }
</style>
<body>
    <div></div>
</body>
Copy after login

It should be noted that the child element p inherits the font-size of the parent element body, so in fact, the sentence "font-size: 12px;" is implicit in p's style sheet. Now if you explicitly set font-size for a child element, the child element will calculate the absolute length of em according to its own font-size. eg:

<style>
    body {
        font-size: 12px;
    }
    div {
        font-size: 20px;
        width: 10em;    /* 10em = 20px * 10 = 200px */
    }
</style>
<body>
    <div></div>
</body>
Copy after login

Note that in the above example, the font-size of p uses px as the unit, so what if you want to use em? It should be noted that if em is used as the unit when setting font-size, then em will be the relative value of the font-size of its parent element. eg:

<style>
    body {
        font-size: 12px;
    }
    div {
        font-size: 2em; /* 2em = 12px * 2 = 24px */
        width: 10em;    /* 10em = 24px * 10 = 240px */
    }
</style>
<body>
    <div></div>
</body>
Copy after login

The font-size of the child element p is determined based on the font-size of its parent element body, so 2em = 12px * 2, = 24px; and the width of p is relative to its own font- The size is determined, so 10em = (12px * 2) * 10 = 240px. So it’s not surprising that 2em=24px and 10em=240px in p.

In fact, not only the width, but also the font-size em in the child elements are determined based on the font-size of the parent element, and all other ems are determined based on their own font-size.

<style>
    body {
        font-size: 16px;
    }
    div {
        font-size: 1.25em;  /* 1.25em = 16px * 1.25 = 20px */
        width: 10em;    /* 10em = 20px * 10 = 200px */
        height: 5em;    /* 5em = 20px * 5 = 100px */
        border: 0.05em solid #000;  /* 0.05em = 20px * 0.05 = 1px */
        margin: 0.25em; /* 0.25em = 20px * 0.25 = 5px */
        padding: 0.75em;    /* 0.75em = 20px * 0.75 = 15px */
        line-height: 1.5em; /* 1.5em = 20px * 1.5 = 30px */
    }
</style>
<body>
    <div></div>
</body>
Copy after login

(Learning video sharing: css video tutorial)

2. Calculate the correct em


# based on px

##1. Use the PXtoEM calculator

Use the online tool PXtoEM (http://pxtoem.com/) to easily and quickly calculate the required em value based on px.


2. Manually calculate em

The calculation formula for converting px to em can be deduced in reverse from the following example

<style>
    div {
        font-size: 16px;
        width: 2em; /* 2em = 16px * 2 = 32px */
    }
</style>
Copy after login

px = Reference text size* em => em = px / Reference text size

3. Notes

In the above formula , "Reference text size" requires special attention:

  • If the element itself sets font-size, then the reference text size is its own font-size

  • If the element itself does not set font-size, then the reference text size is the font-size of the parent element

  • When setting font-size for the element, if em is used as the unit , then the reference text size is the font-size of the parent element


For more programming-related knowledge, please visit:

Programming Video! !

The above is the detailed content of What does em mean in css. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!