The units in which length is measured can be either absolute such as pixels, points and so on, or relative such as percentages (%) and em units.
Specifying CSS units is obligatory for non-zero values, because there is no default unit. Missing or ignoring a unit would be treated as an error. However, if the value is 0, the unit can be omitted (after all, zero pixels is the same measurement as zero inches).
Relative length units specify a length relative to another length property. Relative units are:
The em and ex units depend on the font size that's applied to the element.
A measurement of 1em is equal to the computed value of the font-size property of the element on which it is used. It may be used for vertical or horizontal measurement.
For example, if font-size of the element set to 16px and line-height set to 2.5em then the calculated value of line-height in pixel is 2.5 x 16px = 40px.
The exception occurs when em is specified in the value of font-size property itself, in that case it refers to the font size of the parent element.
So, when we specify a font size in em, 1em is equal to the inherited font-size. As such, font-size: 1.2em; makes the text 1.2 times larger than the parent element's text.
Let's understand what this code was all about. The default size for the fonts in all modern browsers is 16px. Our first step is to reduce this size for the entire document by setting the body font-size to 62.5%, which resets the font-size to 10px (62.5% of 16px).
This is to round off the default font-size for easy px to em conversion.
The ex unit is equal to the x-height of the current font.
The x-height is so called because it is often equal to the height of the lowercase 'x', as illustrated below. However, an ex is defined even for fonts that do not contain an 'x'.
Absolute length units are fixed in relation to each other. They are highly dependent on the output medium, so are mainly useful when the output environment is known. The absolute units consist of the physical units (in, cm, mm, pt, pc) and the px unit.
Absolute physical units such as in, cm, mm, etc. should be used for print media and similar high-resolution devices. Whereas, for on-screen display such as desktop and lower-resolution devices, it is recommended to use the pixel or em units.