Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the context of em?

In this example:

THE CSS

h1 { font-size: 2em; } .smaller { font-size: 0.5em; } 

THE HTML

<h1>Hi, I am a <span class="smaller">toad</span></h1> 

Will the word "toad" be 0.5 times 16px (the browser's standard font-size) or will it be 0.5 times 2em (the font-size of the h1)?

like image 251
Robin Cox Avatar asked Mar 15 '13 11:03

Robin Cox


People also ask

What is the use of em?

Definition and Usage The <em> tag is used to define emphasized text. The content inside is typically displayed in italic. A screen reader will pronounce the words in <em> with an emphasis, using verbal stress.

What does em mean in code?

<em>: The Emphasis element. The <em> HTML element marks text that has stress emphasis.

What is the difference between I and em?

The main difference between these two tag is that the <em> tag semantically emphasizes on the important word or section of words while <i> tag is just offset text conventionally styled in italic to show alternative mood or voice. Below is the code to show this difference between the two : Example-1 : HTML.

What is em in HTML CSS?

The em is simply the font size. In an element with a 2in font, 1em thus means 2in. Expressing sizes, such as margins and paddings, in em means they are related to the font size, and if the user has a big font (e.g., on a big screen) or a small font (e.g., on a handheld device), the sizes will be in proportion.


2 Answers

It's equal to the computed value of the ‘font-size’ property of the element on which it is used. Inheritance runs down the document tree.

To answer your question, it would be 0.5 times the 2em, which in turn would be 2 times whatever the h1's parent's computed font-size is. phew.

It's also important to note that if you use em on other CSS properties, for example, width or height, the result will be calculated from the computed font-size of whatever element you apply the width or height to, etc...

The following article describes the use and context of the em unit rather well in my opinion, along with some other reading material and resources... rem units may interest you somewhat also.

  1. http://www.impressivewebs.com/understanding-em-units-css/
  2. http://snook.ca/archives/html_and_css/font-size-with-rem
  3. http://caniuse.com/rem

You may also like to check out this fiddle to see how it acts a little clearer:

http://jsfiddle.net/HpJjt/3/

like image 159
Seer Avatar answered Sep 20 '22 12:09

Seer


The em unit denotes the font size of the element, except when used in the value of the font-size property, where it denotes the font size of the parent element. In this sense, in the case presented, the context is the parent element.

In the case presented, the font size of the word “toad” is thus equal to the font size of the parent of the h1. No specific value for it can be inferred for it from the data given.

When font sizes is computed, the font size of the parent of h1 has been computed when this construct will be dealt with; let’s call it s. First the font size of h1 is computed, multiplying s (the font size of the parent) by 2. Then the font size of the span element is computed, multiplying its parent’s font size by 0.5, yielding s. Theoretically, rounding errors could cause a minimal deviation in such processes, but multiplication by 2 and 0.5 can be assumed to be exact in computers.

like image 43
Jukka K. Korpela Avatar answered Sep 20 '22 12:09

Jukka K. Korpela