Choosing the right CSS unit for your next front-end project
Tags: css, html, webdev, beginners
Published: 2022-05-22 12:54:00

Hey guys, In this article, I will help you understand the different CSS units such as (em, rem, px) to help you choose the right CSS units for your next front-end projects and I will also help you understand how choosing the right CSS unit can help your website's accessibility

The units we will talk about in this article are the **Emphemeral unit** (**em**), **Root Emphemeral unit** also known as **root em** (**rem**) and **Pixels** (**px**).
For a long time designers and developers have always used the px unit which is very easy to understand because your 10px is always 10px no matter what

Em and rem units are relative but the difference is what they are relative to. Let's get into it


## Table Of Contents


1. 
[Introduction](#intro)

2. 
[What is a CSS Unit](#cssunits)

3. 
[Absolute Length Units](#absunits)

4. 
[Relative Length Units](#rlunits)

5. 
[The Difference between px, rem, and em](#diff)

6. 
[REM](#rem)

7. 
[Changing the size of the root element](#root)

8. 
[Accessibility](#access)

9. 
[EM](#em)

10. 
[Conclusion](#end)


### Introduction <a name="intro"></a>
CSS has several options for what units to use when sizing various CSS properties. Learning all your CSS unit options can be the key to a styling that's easy to manage and looks great on any screen.

### What is a CSS Unit? <a name="cssunits"></a>
A CSS unit determines the size of a property you’re setting for an element or its content. For example, if you want to set the property margin of a paragraph, you would give it a specific value. This value includes the CSS unit.

Let’s look at an example:


{% embed https://gist.github.com/Damkols/28f28abe9cc7d0689ce6ca3f29806dfb %}

In this case, the margin is a property, 20px; is the value, and px is the CSS unit.

### Absolute Length Units <a name="absunits"></a>
Absolute length units are based on an actual physical unit and are generally considered to be the same size across devices. However, depending on your screen size and quality, or settings in your browser or OS, there may be some exceptions.

Here are some common absolute length units in CSS:
Centimeters (**cm**), Millimeters (**mm**), Inches (**in**), Points (**pt**), Picas (**pc**), Pixels (**px**) are one of the most common length units in CSS.

In CSS, 1 px is formally defined as 1/96 of an inch. All other absolute length units are based on this definition of a px.

[Read more about Absolute units and their values here](https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Values_and_units)

### Relative Length Units <a name="rlunits"></a>

Relative length units are relative to another element's size or settings. For example, the relative font size of an element may be calculated using the parent element's font size.

Here are some common relative length units: **rem**, **em**

[Read more about Relative units and their values here](https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Values_and_units)


### The Difference between px, rem and em <a name="diff"></a>

Px is a CSS unit commonly used on websites, px is non-scalable, it is an absolute unit.
Changing the value of another element does not affect the value of the absolute units. The value assigned is fixed regardless of user preferences. 
 
Rem and em are scalable and responsive units that are interpreted by the browser in equivalent px units, they are relative units. Changing the value of the parent or root element affects the value of the relative units, they scale with the device.

So what differentiates the two? The difference is in how the browser derives the values. To view the calculated values, go to the Chrome Developer Tools and open the Computed tab. 

The calculated px value of the rem unit is relative to the font size of the root element (HTML).
However, this is affected by inheritance through the setting of the font size in the browser, unless overridden by a non-inherited px unit.

The calculated px value of the em unit is relative to the font size of the element to be styled.
This is also affected by values ​​inherited from parent elements unless explicitly overridden by a px unit that is not subject to inheritance. 
 


### REM <a name="rem"></a>
Rem is always relative to the root element, the default size of the root element is 16px.

Let's look at an example:

{% embed https://gist.github.com/Damkols/b03f80f56c9f992e1c6434642e89b578 %}

{% embed https://gist.github.com/Damkols/8a1ac63752491f46cf600cc195c2fe0f %}

![1rem-paragraph-gets-converted-to-16px-which-is-the-fontsize-of-the-root-element](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ot6w3ggwdl801vo3lsd9.png)

A font size of 1rem is applied to the p tag, and the font size of the p tag is computed to 16px when we inspect the element in the dev tools.

{% embed https://gist.github.com/Damkols/e4d59c6f7d192763e5dae1fbf14064fa %}


![2rem-paragraph-gets-converted-to-32px-which-is-twice-the-fontsize-of-the-root-element](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z19cg8p3xikrzndy4up6.png)

Here a  font size of 2rem is applied to the p tag, and the font size of the p tag is computed to 32px,

### Changing the size of the root element <a name="root"></a>
In a case where the default size of the root element is changed

Let's look at an example:

If the font size of the root element is changed to 10px and the p-tag is given a font size of 1rem.

{% embed https://gist.github.com/Damkols/aa2700d6306f8acc8b3178d41a078210 %}


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zq3duq16dmk22k4wzmf6.png)

In this case, the p tag will have a font size of 10px because the default size of the root element has changed.

This is what rem units being relative to the root element means.


### Accessibility <a name="access"></a>


What's great about using rem units is that the user can control the size of the font from their browser settings.

![paragraph-fontsize-is-computed-to-10px-after-the-fontsize-of-the-root-html-was-changed-to-10px](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25yslsvl1wldxkw8mb3y.png)

If the browser setting is changed to larger text, the rem units font adjusts accordingly and the same applies when the browser setting is changed to smaller text.


![2rem-paragraph-is-now-40px-after-we-changed-the-fontsize-on-chrome-browser-to-a-Larger-size](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7puyz9omux3ew3jhi4c0.png)

A font size of 2rem applied to the p-tag is computed to 40px after we changed the font size of the chrome browser to a larger size.


![2rem-paragraph-is-now-24px-after-we-changed-the-fontsize-on-chrome-browser-to-a-smaller-size](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmua06gfezqj23007r5c.png)

Here the font size of 2rem applied to the p-tag is computed to 24px after we changed the font size of the chrome browser to a smaller size

This makes using rem units great for accessibility


### EM <a name="em"></a>
Em units are relative to their parent element and if they do not have any parent element, em units work exactly like rem units in the sense that they are relative to the root element


Lets take a look at an example of a box container with a p-tag in it:

{% embed https://gist.github.com/Damkols/0f3e2953f41fad97d4f1bc6c8da2b8f1 %}

{% embed https://gist.github.com/Damkols/4a55ae84fc9b147ff0b7d05475f6eb3e %}


The box container is given font-size of 30px, then we set a font size of 1em on the p-tag


![1em-paragraph-with-a-parent-element-of-30px-gets-converted-to-30px](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdkf600l36p6pes0qg9l.png)

The p tag when we inspect it in the Chrome dev tools is given a computed font-size value of 30px.


This shows that the p tag, in this case, is relative to its parent element which is the box container.

{% embed https://gist.github.com/Damkols/f45431fc197753633785e0347bcd3948 %}

If we were to remove the styling on the box container and the p-tag has the same styling.

![1em-unit-without-a-parent-element-dictating-its-fontsize-behaves-like-rem-units](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gi5i2vtn6vmnmwinevu9.png)

Inspecting the p element in the dev tools shows that the p-tag now has a font-size of 16px.

This shows the relativity of the em units to the root element when it does not have a parent element, also shows the similarities between the em and rem units.

Another thing to note when applying em units to elements with a parent container is, the element won't scale with the browser settings like it will when using rem units.



### Conclusion <a name="end"></a>
So that's it for this article, there's a ton more on CSS we haven't talked about.

To learn more about other CSS units, make sure to check the following resources 👇:

MDN web docs: https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Values_and_units

W3Schools: https://www.w3schools.com/cssref/css_units.asp

Give these resources a read.

As always thanks for giving it a read, give it a like 👍, share it with others too, and if you still got any questions then drop them down in the comments. THANKS FOR READING! 💖

if you enjoyed reading this as much as I enjoyed writing it, then Like and Share this with your friends and feel free to follow me on [Twitter](https://mobile.twitter.com/kolsCodes) 👨‍💻.

[![Buy Me A Coffee](https://cdn.buymeacoffee.com/buttons/default-yellow.png)](https://www.buymeacoffee.com/damolausmaN)