什么是“em"?如果在 em 中指定文档的字体大小? [英] What is an "em" if the font-size of the document is specified in ems?

查看:39
本文介绍了什么是“em"?如果在 em 中指定文档的字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 CSS 中,em 是基于文档字体大小的相对单位.那么,如果文档本身的字体大小以em为单位,那么em究竟是什么?假设我们说:

In CSS, an em is a relative unit based on the font-size of the document. So, what exactly is an em then, if the font-size of the document itself is measured in ems? Suppose we say:

<style type = "text/css">
body
{
    font-size: 1em;
}
</style>

因此,现在递归定义了 em.那么浏览器是如何处理的?

So, an em is now recursively defined. So how is this handled by the browser?

W3C 文档说:

'em' 单位等于 'font-size' 的计算值使用它的元素的属性.例外是当'em' 出现在 'font-size' 属性本身的值中,其中case 是指父元素的字体大小.它可能会被使用用于垂直或水平测量.(这个单位有时也在印刷文本中称为四边形.)

The 'em' unit is equal to the computed value of the 'font-size' property of the element on which it is used. The exception is when 'em' occurs in the value of the 'font-size' property itself, in which case it refers to the font size of the parent element. It may be used for vertical or horizontal measurement. (This unit is also sometimes called the quad-width in typographic texts.)

但是如果元素是document.body,那么没有父元素怎么办?

But what if the element is document.body, so there is no parent element?

推荐答案

body 不是文档根元素 — 这是一个非常常见的误解.body 的父元素是 html,其默认字体大小匹配浏览器的默认字体大小设置(通常为 16px).1

body is not the document root element — that's a very common misconception. The parent element of body is html, whose default font size matches the browser's default font size setting (typically 16px).1

即使您在 both bodyhtml 的 em 中设置了 font-size 值,这也适用.所以如果你这样做:

This applies even if you set a font-size value in ems on both body and html. So if you did this:

html, body { font-size: 2em; }

然后,假设用户设置的默认字体大小为 16pxhtml 的字体大小将为 32px(两倍于默认字体大小)和 body 的字体大小为 64px(其父字体的两倍,html).

Then, assuming a default font size of 16px as set by the user, html will have a font size of 32px (twice the default font size) and body will have a font size of 64px (twice of its parent, html).

1 准确地说,html 元素的默认字体大小是 初始值,medium,根据规范对应于由用户.

1 To be precise, the html element's default font size is the initial value, medium, which according to the spec corresponds to the preferred default font size as set by the user.

这篇关于什么是“em"?如果在 em 中指定文档的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆