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
| Unit | Relative to | Example | Use for |
|---|---|---|---|
px | Nothing (fixed) | border: 1px | Borders, small fixed details |
% | The parent element | width: 50% | Widths inside a container |
em | The font size of this element (or the parent, for font-size) | padding: 1em | Spacing that grows with the text of a component |
rem | The font size of the root (<html>), usually 16px | font-size: 1.5rem = 24px | Font sizes and most spacing |
vw | 1% of the screen width | width: 100vw | Full-width sections |
vh | 1% of the screen height | height: 100vh | Full-height sections |
dvh | 1% of the current visible height | min-height: 100dvh | Full-height sections on phones |
ch | The width of the "0" character | max-width: 65ch | Readable 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
| Way | Example | Notes |
|---|---|---|
| Name | red, navy, white | About 140 names. Fine for quick tests. |
| Hex | #1f5fbf | Red, green, blue in pairs, from 00 to ff. Most common. |
| Short hex | #fff | Same as #ffffff |
| rgb | rgb(31 95 191) | Red, green, blue from 0 to 255 |
| hsl | hsl(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
pxfor all font sizes, which ignores the user's settings. - Nesting
emsizes and getting text that is far too big or small. 100vhsections that are cut off on phones.- Using
opacitywhen 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
| Word | Meaning |
|---|---|
| px | A fixed pixel unit |
| rem | Relative to the root font size |
| em | Relative to the element's font size |
| vw / vh | 1% of the screen width / height |
| clamp() | A value between a minimum and a maximum |
| Hex | A colour code like #1f5fbf |
| Alpha | How 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.