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?
bodyis not the document root element — that’s a very common misconception. The parent element ofbodyishtml, whose default font size matches the browser’s default font size setting (typically16px).1This applies even if you set a
font-sizevalue in ems on bothbodyandhtml. So if you did this:Then, assuming a default font size of
16pxas set by the user,htmlwill have a font size of32px(twice the default font size) andbodywill have a font size of64px(twice of its parent,html).1 To be precise, the
htmlelement’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.