CSS Units Explained: EM Vs REM
With responsive designs everywhere, many of us use em and rem in our designs. Have you ever wondered how they differ and fit our design needs?

Search for a command to run...
With responsive designs everywhere, many of us use em and rem in our designs. Have you ever wondered how they differ and fit our design needs?

What is JavaScript? Discover how this quirky yet powerful language runs in browsers, what makes it tick, and how you can start adding it to your sites

Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters w...

Problem 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? Problem Description The problem is pretty self-explanatory. 215 is 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26....

Problem Starting in the top left corner of a 2*2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner. How many such routes are there through a 20*20 grid? Problem Description We are given a...

Problem The following iterative sequence is defined for the set of positive integers: n β n/2 (n is even) n β 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 β 40 β 20 β 10 β 5 β 16 β 8 β 4 β 2 β ...

In CSS, the numeric value <length> represents the size of the elements. π»π»
This CSS data type, <length>, represents the distance value. Many CSS properties such as width, height, margin, padding, border-width, font-size, text-shadow, box-shadow etc., uses <length>
CSS gives us different units to express <length>.
They fall into two categories: Absolute Units & Relative Units.
Absolute units are physical measurements and will always be the same size. Examples are px, pt etc.
Relative Units are dynamic and depend on other (mostly parent) elements' <length>. Examples of these are em, rem, vh, vw etc. With responsive designs everywhere, they are used to create perfectly scalable layouts.
Many of us already use em and rem in our designs. Have you ever wondered how they differ and whether they fit our design needs? π€π€
We will examine the differences between em and rem in this blog post and explore how they impact our designs.
emThe unit em is always relative to the font-size of the parent element.
Let us take an example ππ»ππ»

In the above example, the font-size of the .child-element is 30px
For the .child-element, the font-size is calculated based on its parent, i.e, .parent-element font-size π€π€
Child element font-size calculation:
parent-element font-size = 20px
Hence in the child element,
1em = 20px
1.5em = 20 * 1.5 = 30px
Hence, the child element's font-size is 30px.
If the parent element's font-size is not specified, it will be looked higher up in the DOM tree. If no font-size is specified until the root element, <html>, then the browser's default of 16px is used.
Therefore, em's are compounding from one level to the other.
Let us take a quick look at the next example.

ππ»ππ» For the HTML above, here is a picture of em calculated down the DOM.

A further interesting fact about em is that when used for properties other than font-size, such as padding, margin, etc., it is relative to the element's own font-size.

For the above example, the padding is calculated based on its own font-size: 30px.
As a result, the top and bottom padding will be 60px (2em * 30px) and the right and left padding will be 30px (1em = 30px).
A live example of how em works. π―π―
This compounding nature of em can lead to unintended consequences in our designs. rem solves this issue.
remFor the sole purpose of solving the compounding problem of em, the rem unit is introduced. rem stands for root element's em.
rem always refers to the root element's (in our case, the HTML element) font size no matter where it is applied. It stays consistent throughout the document and to all the properties.
Here is how rem is calculated down the DOM for the same example as em. ππ»ππ»

Using rem units allows us to avoid the compounding effect of em units.
With rem, it is always based on the font-size of the root element, so there are no surprises.
Also, with other values like margin, padding etc., rem is consistently based on the font-size of the root element
Live example of rem π―π―
Choosing between em or rem always depends on personal preferences. There is no such thing as a better option between them.
If you want a consistent design with no surprises, use rem. If you want to achieve something that is influenced by the parent element, then use em. But extensive testing for different devices and orientations should be done when using em.
My Personal preference is rem over em.
Finally, here is a little comparison between em and rem. ππ»
If I have missed something, let me know in the comments.