CSS 中的 Em 与 Rem 单位?

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.

   Difference between em and rem      .father {         paddding: 0px;         font-size: 16px;      }      .son {         margin: 1em;         font-size: 1em;      }   
This is the Father element
This is Son element

登录后复制在上面的输出中,有一个父元素和一个子元素。在输出中,子元素的大小与父元素相同

rem单位

单位rem相对于元素根的字体大小,即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.

   The difference between em and rem units      html {         font-size: 18px;      }      .son {         font-size: 2rem;         margin: 1.5rem;      }      .father {         font-size: 13px;      }   
This is parent
This is Child in rem unit system

登录后复制

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单位相对于根元素进行计算,这两种单位都属于相对单位,它们有助于使网站具有响应式布局。这些单位有助于设置特定元素的大小。

以上就是CSS 中的 Em 与 Rem 单位?的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2864119.html

(0)
上一篇 2025年3月10日 17:25:15
下一篇 2025年2月23日 00:27:12

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • SASS 中的 @extend 指令是什么?

    SASS 允许开发人员编写更具可读性的代码并以更好的方式对其进行操作。它包含多个指令,例如@media、@content、@include、@mixin、@extend等,提供了一些功能,以便开发人员可以编写比普通CSS更好的代码。 在本教…

    2025年3月10日
    200
  • 使用 CSS 将文本长度限制设置为 N 行

    有时候,开发者需要根据HTML元素的尺寸来截断文本。例如,div元素的宽度是100px,它只能容纳一些字符。因此,我们需要使用CSS来截断文本。 然而,我们可以使用JavaScript截断文本,但这可能会引起问题。例如,我们可以在100px…

    2025年3月10日
    200
  • 使用 CSS 进行相对定位

    相对定位更改 HTML 元素相对于其正常显示位置的位置。因此,“left:20”会向元素的 LEFT 位置添加 20 个像素。 您可以使用两个值top和left以及属性用于将 HTML 元素移动到 HTML 文档中的任意位置。 向左移动 &…

    2025年3月10日
    200
  • 使用数据表分页

    我们可以使用分页技术以较小的块显示大量数据。例如,亚马逊和 Flipkart 等在线商店列出了数百万种产品。因此,如果他们不使用分页技术来显示数据,用户需要滚动网页末尾才能查看最后一个产品。现在,想想需要多少滚动才能到达数百万种产品中的最后…

    2025年3月10日
    200
  • 使用 CSS 为元素添加阴影

    使用 CSS box-shadow 属性为使用 CSS 的元素添加阴影。您可以尝试运行以下代码来实现 box-shadow 属性: 示例 现场演示          h2 { box-shadow: 10px 10px; height: 5…

    2025年3月10日
    200
  • CSS padding-left 属性

    padding-left 指定元素的左内边距。它设置元素的左填充。这可以取以%长度表示的值。 示例                         This is a paragraph with a specified left padd…

    2025年3月10日
    200
  • 使用HTML和CSS创建水平滚动捕捉

    To create a horizontal scroll snap, we will make use of the scroll−snap−type to produce the snap effect. The properties …

    2025年3月10日
    200
  • CSS 中的 2D 变换函数

    2d 变换函数用于对元素应用 2d 变换,可以旋转、移动、缩放和倾斜。 翻译 – 沿 x 和 y 轴移动元素。 缩放 – 在 x y 方向调整元素大小。 旋转 – 将元素移动一定程度。 倾斜 -沿 x y…

    2025年3月10日
    200
  • CSS 语音媒体属性之前的休息

    CSS rest-after 属性对于语音媒体在元素之前设置暂停很有用。 让我们看一个 rest-before 语音媒体属性的示例 h1 {   rest-before: 15ms;} 登录后复制 时间以毫秒为单位设置暂停。 以上就是CSS…

    2025年3月10日
    200
  • 如何在 CSS 中使用 font-optical-sizing 属性?

    在学习如何使用font-optical-sizing属性之前,我们首先要看一下CSS中的font属性以及为什么需要将font-optical-sizing作为一个单独的属性。 网页上文本的样式由CSS中的font属性控制,它是许多其他属性的…

    2025年3月10日
    200

发表回复

登录后才能评论