Table of Contents
1. What is em
2.em and HTML text size default value
3.em and inheritance
2. Calculate the correct em
##1. Use the PXtoEM calculator
2. Manually calculate em
Home Web Front-end CSS Tutorial What does em mean in css

What does em mean in css

Apr 02, 2021 pm 03:17 PM
css em unit

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

See all articles