What is the difference between REM and em and px?

Rahul Kaklotar
2 min readMar 29, 2023

--

What is the difference between REM and em and px?

In CSS, there are three common units used for sizing: rem, em, and px. Here’s a brief explanation of each unit with an example:

  1. Rem (Root em): This unit is relative to the root element (usually the <html> tag) and is useful for creating responsive designs. One rem is equal to the font size of the root element. So if the font-size of the root element is set to 16px, then 1rem is equal to 16px. For example:
html {
font-size: 16px;
}

h1 {
font-size: 2rem; /* 2 * 16px = 32px */
}

2. Em: This unit is relative to the font size of the parent element and can be useful for creating scalable designs. For example:

.parent {
font-size: 16px;
}

.child {
font-size: 0.8em; /* 0.8 * 16px = 12.8px */
}

In this example, the font size of the child element is 0.8 times the font size of the parent element.

3. Px (Pixel): This unit is an absolute unit of measurement and is not relative to anything else. It is commonly used for fixed-size elements or elements that require exact measurements. For example:

.element {
font-size: 14px;
margin-top: 20px;
}

In this example, the font size of the element is set to 14 pixels and the top margin is set to 20 pixels.

In conclusion, the choice of unit depends on the specific needs of the project. Rem is useful for creating scalable designs, em is useful for creating responsive designs, and px is useful for fixed-size elements.

Follow me for more on this type of topic you should learn.

I hope this gave you some fresh ideas for your next writing project. If you found it helpful and want to support more content like this, you’re welcome to buy me a coffee here: Buy Me A Coffee.

Note: All support is voluntary and doesn’t affect what I write — I’ll keep sharing what I love either way!

--

--

Rahul Kaklotar
Rahul Kaklotar

Written by Rahul Kaklotar

I'm software engineer with 6 years of expertise in Javascript, React, TS & Node. Passionate about creating user-friendly websites with a sharp eye for detail.

Responses (2)