What is an “em” if the font-size of the document is specified in ems?

一曲冷凌霜 提交于 2019-11-26 23:21:05

问题


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>

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

The W3C docs say:

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.)

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


回答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

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; }

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).


1To 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.




回答2:


It will take the browsers' default value of 16px if no parent element has defined it.

Also see this: http://pxtoem.com/




回答3:


It'll be relative to whatever the browser's default font-size is, which WILL be specified in a physical size, e.g. '10pt'.



来源:https://stackoverflow.com/questions/10470727/what-is-an-em-if-the-font-size-of-the-document-is-specified-in-ems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!