Lesson 14 of 52CSS foundations
Colors and Units
Write colours in hex, rgb, hsl and oklch, and choose between px, rem, em, %, vw and dvh so your pages scale properly.
Almost every CSS declaration you write has a value: a colour, a size, a distance. CSS gives you several ways to write each one, and the choice matters more than it first appears. A size in px ignores the reader's font settings, while the same size in rem respects them. A colour in hsl or oklch is far easier to tweak by hand than a hex code. This lesson covers the colour formats and units you will meet in real stylesheets, and gives you simple rules for when to use which.
Colour formats
A screen mixes red, green and blue light to make every colour. Most CSS colour formats are just different ways of describing that mix.
| Format | Example | Notes |
|---|---|---|
| Named | tomato, navy, white | 148 keywords; handy for quick tests |
| Hex | #2563eb, #fff | Red, green, blue in base 16, 00 to ff each |
rgb() | rgb(37 99 235) | Red, green, blue as numbers from 0 to 255 |
hsl() | hsl(221 83% 53%) | Hue (angle), saturation, lightness |
oklch() | oklch(0.55 0.2 260) | Lightness, chroma, hue, matched to human vision |
Hex is the most common format you will see in design files. #2563eb means red 25, green 63, blue eb in hexadecimal (base 16, where a to f stand for 10 to 15). A three-digit hex like #fff is shorthand for #ffffff.
HSL describes a colour the way people think about it. The hue is an angle on a colour wheel: 0 is red, 120 is green, 240 is blue. Saturation goes from grey (0%) to vivid (100%). Lightness goes from black (0%) through the pure colour (50%) to white (100%). Want a darker version of your brand blue for a hover state? Keep the hue and saturation and lower the lightness. That is hard to do in hex.
OKLCH is the modern choice. It has the same idea as HSL (lightness, colourfulness, hue) but its lightness matches how bright colours actually look to people. In HSL, yellow and blue at "50% lightness" look wildly different in brightness; in OKLCH, two colours with the same lightness look equally bright, which makes building accessible palettes much easier. oklch() works in all current versions of Chrome, Edge, Firefox and Safari.
The older comma syntax, rgb(37, 99, 235), still works everywhere. Modern CSS also allows the space-separated form shown in the table.
Preview
Try this: change the hue 221 to 150 in all three hsl lines and you get a matching green set. Then change the last swatch's hue from 260 to 30 and see an orange of the same perceived brightness.
Match each colour value to what it describes.
Contrast
Whatever format you use, text must stand out from its background. The accessibility guidelines most teams follow (WCAG) ask for a contrast ratio of at least 4.5 to 1 for normal body text and 3 to 1 for large text. Light grey text on a white background is the most common failure. You do not have to calculate this yourself: the colour picker in the browser devtools shows the contrast ratio of any text colour and marks whether it passes.
Transparency and opacity
Every colour format can carry an alpha value, which sets how see-through it is, from 0 (invisible) to 1 (solid). In modern syntax it goes after a slash: rgb(0 0 0 / 0.5) or hsl(221 83% 53% / 40%). Hex can take it as two extra digits: #00000080 is black at about 50%.
The opacity property is different. It fades the whole element, including its text, border and every child. A card with opacity: 0.5 makes its text hard to read too. If you only want a see-through background, use an alpha colour on background instead.
Gradients
A gradient is a smooth blend between colours. In CSS it counts as an image, so you use it with background or background-image, not background-color.
background: linear-gradient(135deg, #f97316, #db2777);
background: radial-gradient(circle at top, #fde68a, transparent 60%);
background: conic-gradient(#22c55e 0 40%, #e5e7eb 0);linear-gradient blends along a line at an angle. radial-gradient spreads out from a point. conic-gradient sweeps around a centre like a clock hand, which makes a quick pie chart. If text sits on a gradient, check the contrast at both ends.
Preview
Try this: change 135deg to to right, then add a third colour like #7c3aed. Then change the alpha 0.25 to 0.6 and compare with adding opacity: 0.5 to .hero p, which fades the text as well.
Absolute and relative units
A unit says what a number means. 16 on its own means nothing to the browser; 16px does. Units come in two families.
Absolute units have a fixed size. The only one you will use on screens is px. A CSS pixel is not a physical dot on your display; it is a reference size the browser maps to the screen, so 16px looks about the same on a phone and a laptop.
Relative units are calculated from something else:
| Unit | Relative to | Typical use |
|---|---|---|
rem | The root (html) font size, 16px by default | Font sizes, spacing |
em | The current element's font size | Padding that scales with a button's text |
% | The parent (for width) | Fluid widths |
vw, vh | 1% of the viewport width or height | Full-screen sections |
dvh | 1% of the dynamic viewport height | Full-height layouts on phones |
ch | The width of the "0" in the current font | Line length |
rem matters for accessibility. Many people raise their browser's default font size because they find small text hard to read. Sizes in rem grow with that setting. Sizes in px ignore it. So set font sizes in rem: 1rem is the normal body size, 1.5rem is 24px at the default setting.
em is relative to the element's own font size (or the parent's, when you use it for font-size itself). That makes it compound: an em inside an em inside an em multiplies.
The root starts at 16px. Every rem in the page is measured from here.
The browser default font size is 16px. How many pixels wide is the padding on each side of .btn?
.btn {
font-size: 1.5rem;
padding: 0 1em;
}The viewport and dvh
The viewport is the visible area of the browser window. 100vw is its full width and 100vh its full height. On phones, 100vh has a catch: mobile browsers show and hide their address bar as you scroll, and 100vh is based on the larger size, so a "full screen" section can hide its bottom edge behind the toolbar. The dynamic unit dvh follows the visible height as it changes, and svh uses the smallest size. These work in all current major browsers.
calc() and clamp()
calc() does maths with mixed units, something no single unit can do: width: calc(100% - 2rem) means "the full width minus a 2rem gutter". Put spaces around + and -.
clamp(min, preferred, max) picks the preferred value but keeps it between a minimum and a maximum. It is perfect for headings that should grow with the screen without becoming tiny on a phone or enormous on a monitor:
h1 { font-size: clamp(2rem, 1rem + 4vw, 4rem); }Preview
Try this: resize the browser window and the heading scales smoothly between its limits. Change 5vw to 10vw and it hits the maximum sooner. Then change calc(100% - 2rem) to calc(100% - 8rem).
Which unit when
You do not need every unit. A short rule of thumb covers most pages:
- Font sizes:
rem, withclamp()for large headings. - Spacing (padding, margin, gap):
rem, oremwhen it should scale with a component's own text. - Borders and hairlines:
px. A 1px line should stay 1px. - Widths:
%or let layout decide; cap withmax-widthinremorch. - Full-height sections:
min-height: 100dvh.
- hex colour
- A colour written as red, green and blue in base 16, like #2563eb.
- HSL
- A colour format using hue (an angle), saturation and lightness.
- OKLCH
- A colour format whose lightness matches human perception, useful for consistent palettes.
- alpha
- The transparency part of a colour, from 0 (invisible) to 1 (solid).
- rem
- A unit equal to the root element's font size, 16px by default.
- viewport
- The visible area of the browser window; vw, vh and dvh are measured from it.
- clamp()
- A CSS function that picks a preferred value but keeps it between a minimum and a maximum.