Skip to content
BENIBIX Learn

CSS Units

Development🌐 Web DevelopmentBrowser Code

Also known as CSS Length Units, px em rem, rem Unit, Viewport Units

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

Think of it like

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

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.

Why it matters

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

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 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.
Each unit sizes by its own thing: by none, the root, the parent or the window.

Common mistake

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

Key takeaways

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

Learn it properly