


Is em in css relative to the parent element or the size of the current element? (code example)
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:
nbsp;html> <meta> <meta> <meta> <title>Document</title> <style> body { font-size: 16px; } div { font-size: 32px; padding-bottom: 2em; background-color: aquamarine; } </style> <div></div>
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:
nbsp;html> <meta> <meta> <meta> <title>Document</title> <style> body { font-size: 16px; } div { font-size: 2em; /* 仅仅这一行改变了! */ padding-bottom: 2em; background-color: aquamarine; } </style> <div></div>
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
nbsp;html> <meta> <meta> <meta> <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> <div></div>
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!

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools