Home Web Front-end CSS Tutorial Is em in css relative to the parent element or the size of the current element? (code example)

Is em in css relative to the parent element or the size of the current element? (code example)

Oct 24, 2018 pm 04:58 PM

What this article brings to you is about whether em in CSS is relative to the size of the parent element or the current element? (Code sample) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

em is a commonly used relative unit in CSS, so it is necessary to pay attention to some pitfalls.

1em is equal to the font size of the current element, unless you are setting font-size

There are many articles saying that 1em is equal to the font size of the parent element! This statement is actually inaccurate. Look at the following example:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            font-size: 16px;
        }
        div {
            font-size: 32px;
            padding-bottom: 2em;
            background-color: aquamarine;
        }
    </style>
</head>

<body>
    <div></div>
</body>
</html>
Copy after login

will be stretched by padding-bottom, and the height of padding-bottom is 64px, not 32px! This proves that 1em is equal to the font size of the current element (with one exception, discussed below).

What is the relationship between font size and length? Isn't the font a square? In fact, the font size is defined as the width of M.

Why do some people mistakenly think that 1em is equal to the font size of the parent element? This is because if you use em units when setting font-size, font-size will still inherit the default value, so 1em is still equal to the font size of the parent element. This is a special case only when setting font-size! This special case is easy to understand, after all, I am setting the font size of the current element! How can we use the font size being set at the moment as the unit! Isn't this a paradox!

For example, if this paradox really happens, the following situation will occur: the fruit shop owner says to you: "I will pack them for you as many kilograms of oranges as you want," but you say to the boss: "The quantity I want is 2 times the quantity I ultimately want" (analogous to setting font-size: 2em). At this time, the fruit shop owner is probably going to collapse. How many oranges will he pack for you?
In order to avoid this happening, if you use relative units when you specify the quantity, then this unit must not be relative to the quantity you specify at the moment. You can say to your boss: "The quantity I want is twice what the last customer bought" (similar to setting font-size: 2em). After you buy the oranges, you can say to the boss: "I want some more apples, twice as many as the oranges I just bought" (similar to setting padding-bottom: 2em).

Except for this special case, when setting other css properties, 1em is equal to the font size of the current element .

In the above example, using em when setting font-size can prove the existence of this special case:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            font-size: 16px;
        }
        div {
            font-size: 2em;  /* 仅仅这一行改变了! */
            padding-bottom: 2em;
            background-color: aquamarine;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>
Copy after login

The final height is still 64px, because in When setting font-size, 1em == 16px; when setting padding-bottom, 1em is equal to 32px.

What happens if you use em as the font-size on the root element? It has no parent element! It doesn't matter, for inherited properties (including font-size), the default value on the root element is initial. For most browsers, the initial value of font-size is 16px. Therefore, when setting the font-size on the root element, its value is still 16px, and 1em is equal to 16px

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        html {
            /* 2*16px=32px */
            font-size: 2em;
        }
        div {
            /* 2*32px=64px */
            font-size: 2em;
            /* 2*64px=128px */
            padding-bottom: 2em;
            background-color: aquamarine;
        }
    </style>
</head>

<body>
    <div></div>
</body>
</html>
Copy after login

The above is the detailed content of Is em in css relative to the parent element or the size of the current element? (code example). 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles