Skip to the content

Lesson 18 of 296

Units and colours

What you will learn

  • Absolute and relative units
  • px, %, em, rem, vw, vh and ch, and when to use each
  • Responsive sizes with min(), max() and clamp()
  • How to write colours: names, hex, rgb, hsl
  • Transparency
On this page

Absolute and relative units

  • Absolute units are always the same size: px (pixels).
  • Relative units depend on something else: the parent, the root font size, or the screen.

The units you will use

UnitRelative toExampleUse for
pxNothing (fixed)border: 1pxBorders, small fixed details
%The parent elementwidth: 50%Widths inside a container
emThe font size of this element (or the parent, for font-size)padding: 1emSpacing that grows with the text of a component
remThe font size of the root (<html>), usually 16pxfont-size: 1.5rem = 24pxFont sizes and most spacing
vw1% of the screen widthwidth: 100vwFull-width sections
vh1% of the screen heightheight: 100vhFull-height sections
dvh1% of the current visible heightmin-height: 100dvhFull-height sections on phones
chThe width of the "0" charactermax-width: 65chReadable line length for text

rem or em?

em multiplies when elements are nested:

.box { font-size: 1.5em; }
<div class="box">
  Text is 24px
  <div class="box">Text is 36px (1.5 × 24)</div>
</div>

rem always uses the root size, so it never multiplies. That makes it more predictable.

Rule of thumb: use rem for font sizes and spacing. Use em when a size should grow with its own component's text, for example the padding of a button.

Why not px for font sizes?

Users can choose a bigger default text size in their browser settings. rem sizes follow that choice. px font sizes ignore it. Using rem is better for accessibility.

vh on phones

On phones, the address bar appears and disappears while you scroll, so the visible height changes. 100vh can be taller than what the user actually sees. 100dvh (dynamic viewport height) follows the visible height. svh (smallest) and lvh (largest) also exist.

Readable line length

Very long lines of text are tiring to read. A common rule is about 45 to 75 characters per line:

.article { max-width: 65ch; }

Responsive sizes: min(), max() and clamp()

.container { width: min(100% - 2rem, 1200px); }

min() uses the smaller value: full width minus a gap on phones, but never more than 1200px on big screens.

h1 { font-size: clamp(1.75rem, 4vw, 3rem); }

clamp(MIN, PREFERRED, MAX): the heading grows with the screen (4vw), but never goes below 1.75rem or above 3rem. One line, no media queries.

calc() does maths with mixed units:

.sidebar { width: calc(100% - 250px); }

Colours

WayExampleNotes
Namered, navy, whiteAbout 140 names. Fine for quick tests.
Hex#1f5fbfRed, green, blue in pairs, from 00 to ff. Most common.
Short hex#fffSame as #ffffff
rgbrgb(31 95 191)Red, green, blue from 0 to 255
hslhsl(216 72% 44%)Hue (0 to 360 on a colour wheel), saturation, lightness

Why hsl is useful: to make a lighter or darker version of a colour, change only the last number (lightness). With hex you have to guess new pairs.

--brand:       hsl(216 72% 44%);
--brand-light: hsl(216 72% 90%);

Transparency

Add an alpha value from 0 (invisible) to 1 (solid):

background: rgb(0 0 0 / 0.5);      black at 50%
background: hsl(216 72% 44% / 0.2);
color: #1f5fbf80;                  hex with alpha (80 is about 50%)

opacity: 0.5 is different: it makes the whole element and all its children see-through, including the text. Use an alpha colour if you only want a see-through background.

currentColor

.icon { border: 2px solid currentColor; }

currentColor means "the same as this element's text colour". Icons and borders then change colour automatically with the text.

Common mistakes

  • Using px for all font sizes, which ignores the user's settings.
  • Nesting em sizes and getting text that is far too big or small.
  • 100vh sections that are cut off on phones.
  • Using opacity when only the background should be see-through.
  • Text colours that look fine to you but fail the contrast check (Lesson 13).

How to say it in an interview

"px is fixed. em is relative to the element's font size and compounds when nested, while rem is relative to the root font size, so I use rem for type and spacing because it respects the user's browser settings. Viewport units like vw and dvh are relative to the screen. I use clamp for fluid sizes and hsl or variables for colour systems, and alpha colours when only the background should be transparent."

Key words

WordMeaning
pxA fixed pixel unit
remRelative to the root font size
emRelative to the element's font size
vw / vh1% of the screen width / height
clamp()A value between a minimum and a maximum
HexA colour code like #1f5fbf
AlphaHow see-through a colour is, from 0 to 1

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.