为什么 px 和 em 在这种情况下表现不同?

发布于 2025-01-13 21:31:54 字数 82 浏览 5 评论 0原文

在我的段落中,使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回眸一遍 2025-01-20 21:31:54

em 使用所应用元素的 font-size,因此其 px 大小不是恒定的。
您可以使用rem单位,它始终使用浏览器的默认font-size(16px)。

em uses font-size of element applied, so its px size is not constant.
You can use rem unit, it always uses default font-size of browser(16px).

疧_╮線 2025-01-20 21:31:54

您混淆了 emrem。正如其他人已经写的那样, em 使用元素的 font-size ,而 rem 使用基本 fontsize 。可以将其定义为 html 标记的字体大小。如果未定义,它将回退到浏览器设置,大多数情况下默认为 16px(但用户也可以在浏览器设置中更改)。

You are confusing em and rem. As others already wrote, em uses the element's font-size, whereas rem uses the basic fontsize . That can be defined as font-size for the html 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).

挽清梦 2025-01-20 21:31:54

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.

姜生凉生 2025-01-20 21:31:54

使用 em 单位作为边距将使用其所应用到的元素中的font-size。除非正好是 16px,否则它不会匹配明确的 16px 边距。

p {
  margin: 0;
  font-size: 20px;
}

.em {
  margin-left: 1em;
}

.px {
  margin-left: 16px;
}
<p class="em">1</p>
<p class="px">2</p>

Using the em unit for margin will use the font-size from the element it is applied to. Unless that is exactly 16px, it will not match an explicitly 16px margin.

p {
  margin: 0;
  font-size: 20px;
}

.em {
  margin-left: 1em;
}

.px {
  margin-left: 16px;
}
<p class="em">1</p>
<p class="px">2</p>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文