Most Common CSS Units Of Measure Explained
Tags: css, webdev, beginners, webdesign
Published: 2018-09-27 14:44:07

---
title: Most Common CSS Units Of Measure Explained
published: true
description: There are a lot of things we use every day but don't really understand how they work. Well the CSS units of measure don't have to be a part of that group because you can get an explanation now.
tags: #css, #webdev, #beginners, #webdesign
---

When you look through CSS and you start seeing the different units of measure, it's helpful to know exactly what they do. To help you better understand how the CSS units of measure work, here are some explanations and examples of how the most commonly used ones work.

<h2>px</h2>
Pixels (px) are the most commonly used unit of measure because there are many standards in web design based around the pixel. Just for your own knowledge, a px  maps to one pixel on your screen. It's super small, but the point of having this reference is to know exactly how much physical space an element or text will fill.

When you are using pixels, keep in mind that these units change relative to the size of the screen. 12px is going to look a little different depending on whether it's on a phone or a laptop . Pixels form the base measurement of websites. The default root size of a web page is set to 16px by all of the popular browsers.

```css
div {
    height: 32px;
}
```

<h2>%</h2>

Percentages get a lot of use in responsive design and are pretty straightforward. The one thing you have to remember is that the size is relative to the parent element. If you have a &lt;h2&gt; nested in a &lt;div&gt; and you give it a width using the % unit of measure, then the &lt;h2&gt; width will be relative to the size of the &lt;div&gt;. It'll look something like this:

```css
div {
    width: 1250px;
}

h2 {
    width: 35%;
}
```

Now you know that the width of the &lt;h2&gt; will be 35% of the &lt;div&gt; width. Percentages are more simple than the em and rem units, but percentages aren't typically used for properties like font-size or border so there are more limited uses of it.

<h2>em</h2>

This is relative to the font-size value of the element. So say you have a &lt;p&gt; inside of a &lt;div&gt; and these are the styles you have:


```css
div {
    font-size: 3em;
}

p {
    width: 2em;
}
```

The &lt;div&gt; will have a font size of 3 times the font-size of the &lt;body&gt;. So that will give it a default of 48px. That number comes from the default font size on browsers which is 16px like we discussed earlier. With the &lt;div&gt; being 3 times that, you get the 48px.

That means your &lt;p&gt; is going to have a width that is 2 times bigger than the font-size of the &lt;div&gt;. So your &lt;p&gt; would have a width of 96px.
That's how the em unit of measure works. Remember that the font-size value of the containing element determines the size of a child element using  em.

<h2>rem</h2>

This unit of measure is really similar to the em unit of measure except it is easier to understand. The rem unit of measure is based off of the font-size of the root element. Unless you change the &lt;body&gt; font-size, this base size will be 16px.

That means everything that has the rem unit behind it is just a multiple of 16px. So for example, let's say you have a sidebar and a main container on your page. Here's your CSS:

```css
.sidebar {
    width: 2.4rem;
}

.main-container {
    width: 7.2rem;
}
```
Since you used rem, you should be able to easily calculate how big your elements will be. This is a big advantage over em because you don't have to keep up with the font-size of every element and it still does a little scaling when the page is looked at on different devices.


I hope that this quick explanation helped you understand the common CSS units of measure better!

These are just the most commonly used CSS units of measure, not the only ones. There are at least 11 others. The problem with some of the others are their limited browser support.

Two of the other units of measure that are gaining some popularity are the viewport measures. The viewport height and viewport width, vh and vw, make it even easier to make responsive layouts because they are both relative to the size of the screen. But that's a discussion for another day.

<hr>

Hey! You should follow me on Twitter because reasons: https://twitter.com/FlippedCoding