RELATIVE LENGTHS

Relative length units specify a length relative to another length.

Font Relative Lengths

Aside from 'rem' (which refers to the font-size of the root element), the font-relative lengths refer to the font metrics of the element on which they are used. The exception is when they occur in the value of the ‘font-size’ property itself, in which case they refer to the computed font metrics of the parent element (or the computed font metrics corresponding to the initial values of the ‘font’ property, if the element has no parent).

em unit

Equal to the computed value of the font-size property of the element on which it is used.

rem unit

Equal to the computed value of font-size on the root element.

ex unit

Equal to the used x-height of the first available font. [CSS3-FONTS] The x-height is so called because it is often equal to the height of the lowercase "x".

ch unit

Equal to the used advance measure of the "0" (ZERO, U+0030) glyph found in the font used to render it.

Unit Relative To
em font size of the element
rem font size of the root element
ex x-height of the element's font
ch width of the "0" (ZERO, U+0030) glyph in the element's font

Sizing with em

Resizing text in IE has been an ongoing struggle. To get around this, we can use em units.

The technique modifies the base font-size on the body using a percentage.

Using line-height & font-size with em

The rule: h1 { line-height: 1.2em }

On the other hand: h1 { font-size: 1.2em }

Disadvantages using em

The problem with em-based font sizing is that the font size compounds.

The em unit is relative to the font-size of the parent, which causes the compounding issue.

Sizing with rem

The main use for rem is replacing em to protect inner element font sizes from being changed by outer elements.

Thus avoiding the classic nested em scaling problem.

What to do for browsers that don't support rem?

We can specify the fall-back using px.
To do so, we specify the font-size using px units first and then define it again using rem units.