为什么 px 和 em 在这种情况下表现不同?
在我的段落中,使用 1em 作为左边距,字体大小为 xx-large,比使用 16px 给我更多的边距。当 1em 等于 16px 时,这怎么可能呢?
Using 1em as margin-left, on my paragraph with fontsize xx-large, gives me more margin than using 16px. How is that possible when 1em equals 16px?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
em
使用所应用元素的font-size
,因此其px
大小不是恒定的。您可以使用
rem
单位,它始终使用浏览器的默认font-size
(16px)。em
usesfont-size
of element applied, so itspx
size is not constant.You can use
rem
unit, it always uses defaultfont-size
of browser(16px).您混淆了
em
和rem
。正如其他人已经写的那样,em
使用元素的 font-size ,而rem
使用基本 fontsize 。可以将其定义为html
标记的字体大小。如果未定义,它将回退到浏览器设置,大多数情况下默认为 16px(但用户也可以在浏览器设置中更改)。You are confusing
em
andrem
. As others already wrote,em
uses the element's font-size, whereasrem
uses the basic fontsize . That can be defined as font-size for thehtml
tag. If it's not defined, it falls back to the browser setting, which by default is 16px most of the time (but can also be changed by the user in the browser settings).EM 使用元素字体大小。因为您在段落中指定了
font-size: xx-large
,所以字体大小不是 16px,而是 32px。EM uses the Element fontsize. Because you have given
font-size: xx-large
on your paragraph the fontsize is not 16px but 32px.使用
em
单位作为边距将使用其所应用到的元素中的font-size
。除非正好是16px
,否则它不会匹配明确的16px
边距。Using the
em
unit for margin will use thefont-size
from the element it is applied to. Unless that is exactly16px
, it will not match an explicitly16px
margin.