CSS units explained
Tags: css, scss, sass
Published: 2021-02-09 21:00:38

---
title: CSS units explained
published: true
date: 2021-02-02 00:00:00 UTC
tags: css, scss, sass
canonical_url: https://www.mariokandut.com/absolute-and-relative-units-in-css/
---

Every property used in CSS has a value type defining the set of values that are allowed for that property. There are multiple options of units to use when determining the size of CSS properties. Learning all your options is key to have _maintainable_ css code and a great user experience on nearly every screen size.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#what-is-a-css-unit)What is a CSS Unit?

**A CSS unit determines the size of a property you’re setting for an element or its content.**

For example:

```
p {
  margin: 16px;
}
```

In this case, `margin` is the CSS property, `16` is the value and `px` (or “pixel”) is the CSS unit.

There are different CSS units available, so the next logical question would be, which one to use and when.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#absolute-units--relative-units)Absolute Units || Relative Units

The two main categories to consider, when evaluating the options are absolute and relative units.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#absolute-units)Absolute Units

**Absolute Units** are the same size regardless of the parent element or window size. This means a property set with a value that has an absolute unit will be always that size, on a mobile phone, on a tablet, on a 4k screen - everywhere!

Absolute units can be **less favourable for responsive sites** , because they don’t scale when the screen size changes.

Hence, they can be very useful when the project doesn't have a responsive requirement, for example desktop-only applications with fixed sizes.

| Absolute Unit | Description | Example |
| --- | --- | --- |
| px | 1/96 of 1 inch (96px = 1 inch) | 

font-size: 12px;

 |
| pt | 1/72 of 1 inch (72pt = 1 inch) | 

font-size: 12pt;

 |
| pc | 12pt = 1pc | 

font-size: 1.2pc;

 |
| cm | centimeter | 

font-size: 0.6cm;

 |
| mm | millimeter (10 mm = 1 cm) | 

font-size: 4mm;

 |
| in | inches | 

font-size: 0.2in;

 |

- **Pixels (px)** are typically the most popular absolute unit for **screens**.
- **Centimeters, millimeters, and inches** are more common for **print** , (print css).

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#relative-units)Relative Units

**Relative Units** are used for **styling responsive sites**. They scale relative to the parent element or to the window size (depending on the unit, REM vs. EM). When using relative units for styling, you avoid having to update all styles for different screen sizes.

[💰: Start your cloud journey with $100 in free credits with DigitalOcean!](https://m.do.co/c/29f238135b61)

Relative units can either be relative to the parent or relative to the root(window).

| Relative Unit | Description |
| --- | --- |
| % | Relative to the parent element’s value for that property |
| em | Relative to the current font-size of the element |
| rem | Relative to the font-size of the root (e.g. the element). “rem” = “root em” |
| ch | Number of characters (1 character is equal to the width of the current font’s 0/zero) |
| vh | Relative to the height of the viewport (window or app size). 1vh = 1/100 of the viewport’s height |
| vw | Relative to the width of viewport. 1vw = 1/100 of the viewport’s width. |
| vmin | Relative to viewport’s smaller dimension (e.g. for portrait orientation, the width is smaller than the height so it’s relative to the width). 1vmin = 1/100 of viewport’s smaller dimension. |
| vmax | Relative to viewport’s larger dimension (e.g. height for portrait orientation). 1vmax = 1/100 of viewport’s larger dimension. |
| ex | Relative to height of the current font’s lowercase “x”. |

There is a controversial discussion going on about the use of **EM vs. REM** on the web development community.

It’s not always clear which of these options is best to use for each type of CSS property.

For example, `%` is usually more appropriate for layout-related properties like `width` than it would be for `font-size`.

Here are some examples of when you would use each relative unit.

**%** : You want a child element to have `%` of the parent’s size (width, height, etc.).

In the example below, the child should have a width of 25% of the parent. If the width of the parent changes, the child width would change as well.

```
.child {
  width: 25%;
}
```

**em** :

`em` is relative to the font-size of its direct or nearest parent. In the example below we want to have the font in the child to be half the size of its parent’s font-size.

```
.child {
  font-size: 0.5em;
}
```

**rem** :

`rem` is only relative to the html (root) font-size.

In the code below, the font-size in the footer should be half the size as in the root element's font.

```
.footer {
  font-size: 0.5rem;
}
```

**ch** : You have a mono-spaced font (the characters are always the same width), and you only have space for 10 characters.

```
.small-text {
  width: 10ch;
}
```

**vh,vw** : vh = viewport height, vw = viewport width; This unit is relative to the viewport/window.

For example, the wrapper class should always have 100vh and the hero class should have a width of half the window.

```
.wrapper {
  height: 100vh;
}
.hero {
  width: 50vw;
}
```

**vmin, vmax** : vmin uses the ratio of the smallest side. That is, if the height of the browser window is less than its width, 1vmin will be equivalent to 1vh. If the width of the browser is less than it’s height, 1vmin is equivalent to 1vw. vmax is the opposite: it uses the largest side. So 1vmax is equivalent to 1vw if the viewport is wider than it is tall; if the browser is taller than it is wide, 1vmax will be equivalent to 1vh.

```
.min-width {
  width: 100vmin;
}
.max-width {
  width: 100vmax;
}
```

**ex** : The ex unit is used rarely. Its purpose is to express sizes that must be related to the x-height of a font. The x-height is, roughly, the height of lowercase letters such as a, c, m, or o. Fonts that have the same size (and thus the same em) may vary wildly in the size of their lowercase letters, and when it is important that some image, e.g., matches the x-height, the ex unit is available. It’s generally a good measure of a font’s mid-section, so if you want the double the line-height of a lowercase letter.

```
.double-lowercase {
  line-height: 2ex;
}
```

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#tldr)TL;DR

- Use relative units, when your project requires responsiveness.
- Do not use print values for the web, besides in `print.css`.
- Read the official documentation with detailed information [W3C CSS Values and Units Module](https://www.w3.org/TR/css-values-4/).

Thanks for reading and if you have any **questions** , use the **comment** function or **send me a message** **[@mariokandut](https://www.twitter.com/mariokandut)**.

If you want to know more about **[CSS](https://www.mariokandut.com/tags/css)**, have a look at these [Gatsby Tutorials](https://www.mariokandut.com/tags/css).

References (and Big thanks):[W3C](https://www.w3.org/Style/Examples/007/units.en.html), [Jess Mitchell](https://www.digitalocean.com/community/tutorials/css-css-units-explained), [thenewcode](http://thenewcode.com/1137/MinMaxing-Understanding-vMin-and-vMax-in-CSS)