search
HomeWeb Front-endCSS TutorialIntroduction to CSS length unit em

Introduction to CSS length unit em

Oct 12, 2018 pm 05:36 PM
cssemunit of length

This article brings you an introduction to the CSS length unit em. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Why use em

em is also a unit in CSS, similar to px. Many people will wonder why em is still used after having px, and em is more troublesome to use than px.

em is mainly used in flexible layout. Here is a small chestnut to illustrate the power of em

nbsp;html>
    <meta>
    <meta>
    <meta>
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        ul {
            list-style: none;
        }
        
        .top {
            height: 80px;
            background-color: black;
        }
        
        .main {
            width: 960px;
            background-color: #ccc;
            margin: 0 auto;
            overflow: hidden;
        }
        
        .left {
            width: 30%;
            height: 100%;
            background-color: aqua;
            border: 1px solid red;
            float: left;
            font-size: 16px;
            line-height: 18px;
        }
        
        .right {
            width: 60%;
            height: 100%;
            font-size: 1em;
            line-height: 1.125em;
            background-color: deeppink;
            border: 1px solid red;
            float: right;
        }
    </style>
    <p></p>
    <p>
        </p><p>
            </p>
                    
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •                 
  • em测试用例,没有em的情况
  •             
                 

            

                    
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •                 
  • em测试用例有em的强大之处
  •             
             

This imitates the general layout of a web page and gives the difference between using em and px. .

Under normal circumstances, em and px look no different. As shown below

Next, hold down the ctrl key and continuously press the ‘-’ (minus sign) key to continuously reduce the page size. There will be a very obvious difference when the size is reduced to 25% (the Chrome browser is used here. If other browsers do not have this situation, you can manually change the font size in the browser. You can see it when increasing the font size. A similar situation occurs) As shown in the figure below

You can see that the left side using px has completely collapsed, and the specific text cannot be seen. The text on the right using em can still be clearly seen. The main reason for this phenomenon is that em is a relative size, and using em will not have much impact on the page's enlargement or reduction.

Since it is a relative size, there will be a reference size. The reference size of em is the font size of the current element. This will raise another question. Since the font size of the current element is used as a reference, what is used as a reference when the current font is in em units? At this time, the font size of its parent element is used as a reference.

So when the entire page uses em as the font size, 1em in the page is the browser’s default font size of 16px;

2. em Specific usage:

1. Set body{font-size: 1em}

After setting the font size of the body, Since the body font inherits the browser's default 16px, then as long as em is used on all web pages, then 1em=16px;

2. Start calculating the specific size of the element

(1) If the font size of the element is inherited from the upper layer, that is, 16px, then 1em=16px inside the element; therefore, the calculation method is as follows

Required em value = current element The px value/the font size value px of the parent element (usually 16px)

For example: 1px=1/16=0.0625em, 18px=18/16=1.125em

(2) If the font size of the element is set by yourself

The em value of the font size of the current element = the font size of the current element px/the font size of the parent element px

Other em values ​​required by the current element = px value of the current element/font size of the element itself px

The following is passed a Example for detailed explanation

nbsp;html>
    <meta>
    <meta>
    <meta>
    <title>Document</title>
    <style>
        body {
            font-size: 1em;
        }
        
        .son1 {
            font-size: 2em;
            height: 5em;
            width: 5em;
            border: 0.0625em solid red;
            background-color: aqua;
            margin: 0 auto;
        }
        
        .son2 {
            font-size: 32px;
            height: 160px;
            width: 160px;
            border: 2px solid red;
            background-color: aqua;
            margin: 100px auto;
        }
    </style>
    <p>
        </p><p>aaa son1</p>
        <p>aaa son2</p>
    
  

Open the box model of the .son1 box displayed in the browser, and the detailed web page result diagram is as follows

 

It can be found that .son1 and .son2 are exactly the same. The font size of

.son1 is 2em and the corresponding px is 2*16=32px; (In turn, you can verify the formula The em value of the font size of the current element (2) = Current element font size px (32)/parent element font size px (16) )

height and width are 5em corresponding px is 5*32=160px; (In turn, you can verify the formula Other em values ​​required by the current element(5)=px value of the current element160/ The font size of the element itself px32

Note: In ie5/6, you need to add html { font-size: 100%} to ensure elastic layout (but currently ie5/6 basically does not have it, and this is based on the article The Incredible Em & Elastic Layouts with CSS As I learned, I haven’t tried to find out what the specific problem is. Write it down first and use it when encountering similar situations in the future)

3、rem的使用

rem使用方法和em类似,不过rem是相对于根元素的大小(即html的字体大小),而不是自身的大小。2中的栗子中的.son1 的相关带em的属性全改为rem 代码如下

nbsp;html>
    <meta>
    <meta>
    <meta>
    <title>Document</title>
    <style>
        .son1 {
            font-size: 2rem;
            height: 5rem;
            width: 5rem;
            border: 0.0625rem solid red;
            background-color: aqua;
            margin: 0 auto;
        }
        
        .son2 {
            font-size: 32px;
            height: 160px;
            width: 160px;
            border: 2px solid red;
            background-color: aqua;
            margin: 100px auto;
        }
    </style>
    <p>
        </p><p>aaa son1</p>
        <p>aaa son2</p>
    

结果图为:

因为.son1 中的单位全改为rem,参考对象为html字体的大小即为16px,所以.son1字体大小为2*16=32px  ,宽度和高度为5*16=80px,边框为1px

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问 CSS基础视频教程

相关推荐:

CSS在线手册

div/css图文教程

The above is the detailed content of Introduction to CSS length unit em. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
Weekly Platform News: Web Apps in Galaxy Store, Tappable Stories, CSS SubgridWeekly Platform News: Web Apps in Galaxy Store, Tappable Stories, CSS SubgridApr 14, 2025 am 11:20 AM

In this week's roundup: Firefox gains locksmith-like powers, Samsung's Galaxy Store starts supporting Progressive Web Apps, CSS Subgrid is shipping in Firefox

Weekly Platform News: Internet Explorer Mode, Speed Report in Search Console, Restricting Notification PromptsWeekly Platform News: Internet Explorer Mode, Speed Report in Search Console, Restricting Notification PromptsApr 14, 2025 am 11:15 AM

In this week's roundup: Internet Explorer finds its way into Edge, Google Search Console touts a new speed report, and Firefox gives Facebook's notification

The Power (and Fun) of Scope with CSS Custom PropertiesThe Power (and Fun) of Scope with CSS Custom PropertiesApr 14, 2025 am 11:11 AM

You’re probably already at least a little familiar with CSS variables. If not, here’s a two-second overview: they are really called custom properties, you set

We Are ProgrammersWe Are ProgrammersApr 14, 2025 am 11:04 AM

Building websites is programming. Writing HTML and CSS is programming. I am a programmer, and if you're here, reading CSS-Tricks, chances are you're a

How Do You Remove Unused CSS From a Site?How Do You Remove Unused CSS From a Site?Apr 14, 2025 am 10:59 AM

Here's what I'd like you to know upfront: this is a hard problem. If you've landed here because you're hoping to be pointed at a tool you can run that tells

An Introduction to the Picture-in-Picture Web APIAn Introduction to the Picture-in-Picture Web APIApr 14, 2025 am 10:57 AM

Picture-in-Picture made its first appearance on the web in the Safari browser with the release of macOS Sierra in 2016. It made it possible for a user to pop

Ways to Organize and Prepare Images for a Blur-Up Effect Using GatsbyWays to Organize and Prepare Images for a Blur-Up Effect Using GatsbyApr 14, 2025 am 10:56 AM

Gatsby does a great job processing and handling images. For example, it helps you save time with image optimization because you don’t have to manually

Oh Hey, Padding Percentage is Based on the Parent Element's WidthOh Hey, Padding Percentage is Based on the Parent Element's WidthApr 14, 2025 am 10:55 AM

I learned something about percentage-based (%) padding today that I had totally wrong in my head! I always thought that percentage padding was based on the

See all articles

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools