• Home
  • CSS
  • Rem, em – length units in CSS

Rem, em – length units in CSS



I must admit that I rarely used such units of length as rem and em so far. I usually used pixels (px), because somehow it was easier for me to associate what size a given element will have.

The rem and em units, however, have one advantage over pixels – they can be set in correlation to parent font size (em) or root font size, which most often is <html> elemet (rem). This gives the ability to manipulate sizes for many elements at once. The default root font value is usually 16px (but it depends on the browser).

Em unit

Using the em unit, we can set the child’s font size to a specific part of the parent’s font size. Example:

See the Pen em by Joanna (@JoannaEl) on CodePen.

As a result, in the example above, the font size of Child1 will be 50% larger than the Parent font, and Child2 will be 50% larger than Child1.

Rem unit

Elements for which the rem unit is used are always correlated with root – most often it is an html element. So we can set the value of the base font in a root by referring to the html element or the :root element.

See the Pen rem by Joanna (@JoannaEl) on CodePen.

In the example above, paragraph 1 has 50% heigth of html font size, and paragraph 2 is 25% smaller than the html font size.

In Rem case, we can just change the value of the “base” font in the ROOT to change size of all fonts on the page – simply and quickly 🙂

Joanna

Leave a Reply

Your email address will not be published. Required fields are marked *