# CSS Units

> The units for sizes in CSS, like px, rem, em, % and vw. Each measures against something different.

**Field:** Software Development  
**Category:** Web Development  
**Also known as:** CSS Length Units, px em rem, rem Unit, Viewport Units

## Think of it like

It is like measuring with your hand or with a ruler. One grows with you, one does not.

## Definition

CSS units say how big a length is, and what it is measured against. px is a fixed length. rem is a share of the root text size, which is often 16px. em is a share of the text size around it. % is a share of the parent box. vw and vh are hundredths of the window's width and height.

## Example

Jack sets his text in rem. A reader who turns up the browser's text size now gets bigger text all over his site.

## Example code

```css
h1 { font-size: 2rem; }   /* twice the root size */
.side { width: 25%; }     /* a quarter of the parent */
.hero { height: 50vh; }   /* half the window height */
```

Three units, each set by its own thing: the root text, the parent box and the window.

## Why it matters

Units decide how a page reacts to big screens, small screens and bigger text. Fixed sizes break first.

## Common mistake

Using em inside em inside em. Each level multiplies, so text grows bigger and bigger by surprise.

## Three principles

- **Fixed or relative.** px stays, the rest follow something.
- **rem for text.** It follows the reader's text setting.
- **vw and vh.** They follow the window size.

## Related terms

- [Cascading Style Sheets (CSS)](https://learn.benibix.com/term/development/css)
- [CSS Box Model](https://learn.benibix.com/term/development/css-box-model)
- [Media Query](https://learn.benibix.com/term/development/media-query)
- [Responsive Web Design](https://learn.benibix.com/term/design/responsive-web-design)
- [Pixel](https://learn.benibix.com/term/design/pixel)

## Taught in

- [The CSS Box Model](https://learn.benibix.com/learn/development-web/css-box-model): HTML, CSS and JS, 8/14

![Five bars, one per unit. 16px is a fixed length. 2rem is twice the root text size. 2em is twice the text size of the parent, so it grows inside bigger text. 50% is half the width of the parent box. 50vw is half the width of the browser window.](https://learn.benibix.com/diagrams/dev5-css-units.svg)

---

Source: https://learn.benibix.com/term/development/css-units
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
