讲师中心 微信公众号
AI工具推荐 视频效率加速

CSS 中的 Em 与 Rem 单位?

落枫姑娘_4510

落枫姑娘_4510

发布时间:2023-08-27 13:13:10

|

1021人浏览过

|

来源于tutorialspoint

转载

css 中的 em 与 rem 单位?

在使用CSS属性设置元素大小时,您可能会注意到两个选项,一个是绝对单位,另一个是相对单位。绝对单位是相同的,是固定的,可以使用cm、mm、px来设置大小。另一方面,相对单位是相对于其他东西的,可以是父元素或任何其他元素。

在本教程中,我们将研究CSS中em和rem单位之间的比较。

The Em unit

em单位使得可以改变相对于其父元素的字体大小的元素的大小。这意味着如果父元素的大小发生变化,子元素的大小也会发生变化。

Let’s look at an example to understand what does the em unit do.

立即学习“前端免费学习笔记(深入)”;

Example

在这个例子中,我们将把子元素的字体大小改为35px。子元素的边距也将改为50px。

在这里,我们首先创建了一个父元素,然后将其大小设置为17.5像素,子元素的字体大小设置为1em,这意味着子元素的字体大小将是父元素的两倍,元素的边距将保持不变。让我们来看一下代码的输出结果。

Note − The usage of the font-size property. The font-size is relative to the father (parent) element when it is used on other properties.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Difference between em and rem</title>
   <style>
      .father {
         paddding: 0px;
         font-size: 16px;
      }
      .son {
         margin: 1em;
         font-size: 1em;
      }
   </style>
</head>
<body>
   <div class="father">
      This is the Father element
      <div class="son">
         This is Son element
      </div>
   </div>
</body>
</html>
在上面的输出中,有一个父元素和一个子元素。在输出中,子元素的大小与父元素相同

rem单位

单位rem相对于元素根的字体大小,即html元素。如果<html>没有指定字体大小,则使用浏览器的默认值,不考虑父元素,只考虑根元素。

Example

We will be keeping the font size of the child element to 50px and then setting the margin of the element to 40px. The size of the rem unit will be relative for all declarations unlike em.

In the following example, we first used the root element and then created a parent element and the child element. We then set the font size of the root element to 18px and the font-size of the parent to 15px. The size of the font of the child element was then set to 1.5rem which means double the size of the root element and not the parent element. Let’s look at the output to see what the rem unit does.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>The difference between em and rem units</title>
   <style>
      html {
         font-size: 18px;
      }
      .son {
         font-size: 2rem;
         margin: 1.5rem;
      }
      .father {
         font-size: 13px;
      }
   </style>
</head>
<body>
   <div class="father">
      This is parent
      <div class="son">
         This is Child in rem unit system
      </div>
   </div>
</body>
</html>

You can see in the above output that the child element is not the double of the parent element but it is the double of the root element.

The unit em, is relative to the font-size of its nearest parent and it can lead to the compounding effect. The rem unit, is relative to the HTML root font size but it does not lead to the compounding effect.

CSS中的Em与Rem单位

The units include em, vh, vw, and rem. The relative units are also known as scalable units and plays an important role in the responsiveness of the website. The em and the rem units are both scalable and relative. The em unit is relative to the font size of the parent element in the HTML document and the rem unit, is relative to the font root of the whole document.

Conclusion

em单位和rem单位之间的区别在于em单位相对于父元素进行计算,而rem单位相对于<html>根元素进行计算,这两种单位都属于相对单位,它们有助于使网站具有响应式布局。这些单位有助于设置特定元素的大小。

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

二狗PPT
二狗PPT Hot

一款AI演示文稿工具,主要用于专为中式职场打造的AI PPT生成工具,适合需要提升相关任务效率的用户。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

咔片AIPPT

一款在线AI演示文稿制作工具,可根据主题和内容需求辅助生成PPT结构与页面,提高演示材料制作效率。

PixTV
PixTV Hot

PixTV是一款面向AIGC内容创作的AI视频生成工具。

讯飞绘文

讯飞绘文是一款由科大讯飞推出的一站式 AIGC 内容运营平台。

Laper
Laper Hot

Laper是专为编剧、导演和制片人推出的 AI 原生剧本创作工具。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

超级简历WonderCV

一款AI办公效率工具,主要用于免费求职简历模版下载制作,应届生职场人必备简历制作神器,适合需要提升相关任务效率的用户。

相关专题

更多
c++怎么把double转成int
c++怎么把double转成int

本专题整合了 c++ double相关教程,阅读专题下面的文章了解更多详细内容。

3868

2025.08.29

C++中int、float和double的区别
C++中int、float和double的区别

本专题整合了c++中int和double的区别,阅读专题下面的文章了解更多详细内容。

724

2025.10.23

margin在css中是啥意思
margin在css中是啥意思

在CSS中,margin是一个用于设置元素外边距的属性。想了解更多margin的相关内容,可以阅读本专题下面的文章。

857

2023.12.18

Revel框架模板渲染与表单验证
Revel框架模板渲染与表单验证

本专题围绕Revel框架视图层开发,介绍模板语法、变量输出、布局复用、表单提交、参数绑定与验证规则,帮助开发者完成安全规范的页面交互。

0

2026.10.16

Revel框架MVC路由与控制器
Revel框架MVC路由与控制器

本专题聚焦Revel框架MVC核心机制,讲解路由定义、控制器Action、参数绑定、请求响应和反向路由,并结合常见场景梳理接口请求处理流程。

0

2026.10.16

Revel框架入门与安装配置
Revel框架入门与安装配置

本专题从Revel框架环境搭建讲起,系统介绍命令行工具安装、项目创建、目录结构、配置文件及热代码重载,帮助开发者快速完成首个Go Web应用。

0

2026.10.16

Linux 新手先学哪些命令
Linux 新手先学哪些命令

Linux新手命令专题汇集终端基础、文件目录操作、文本查看、软件安装和权限管理等入门文章,帮助按实际任务练习常用命令并建立命令行操作习惯。

0

2026.10.15

MySQL 忘记密码怎么办
MySQL 忘记密码怎么办

MySQL忘记密码专题汇集不同版本与系统下的root密码重置方法,覆盖MySQL 5.7、8.0、Mac、Linux及本地集成环境,并提示恢复权限与重新登录的检查步骤。

0

2026.10.15

Vue.js 中 ref 和 reactive 的区别
Vue.js 中 ref 和 reactive 的区别

Vue.js中ref和reactive的区别专题汇集两种响应式API的定义、访问方式、对象解构、侦听与性能取舍等文章,帮助结合实际状态类型选择合适写法。

0

2026.10.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn