Skip to content
Kuza Ndoto Academy

Colours

Beginner → IntermediateChapter 1 · Page 4 of 224 min readUpdated 15 Nov 2026

Colour sets the mood of a website and guides the eye to what matters. CSS lets you write colours in several ways: named keywords, hex codes, rgb(), hsl() and newer models such as oklch(), each with optional transparency.

Where colours are used

The most common colour properties are color (text), background-color, border-color and outline-color. Text colour is inherited, so setting color on body colours all text on the page by default.

body {
  color: #222222;
  background-color: white;
}

Named colours

CSS understands about 140 colour names, such as red, teal, gold, tomato and rebeccapurple. They are easy to read but limited: you can't fine-tune them. Two special keywords are worth knowing: transparent (fully see-through) and currentColor, which means "whatever the element's text colour is". currentColor is handy for borders and icons that should match the text.

Hex codes

A hex colour starts with # followed by three pairs of hexadecimal digits for red, green and blue. Each pair runs from 00 (none) to ff (full, which is 255 in decimal).

  • #ff0000 is pure red; #000000 is black; #ffffff is white.
  • When each pair repeats a digit you can shorten it: #ff8800 becomes #f80.
  • An optional fourth pair sets transparency: #ff880080 is that orange at about 50% opacity (80 hex = 128, and 128 / 255 is about 0.5).

Hex is the format you'll see most in design tools such as Figma, so it is worth getting comfortable with.

rgb()

rgb() takes the same three channels as decimal numbers from 0 to 255, or percentages. Modern CSS separates them with spaces and adds transparency after a slash:

.banner {
  background-color: rgb(255 136 0);         /* solid orange, same as #ff8800 */
}
.overlay {
  background-color: rgb(0 0 0 / 60%);       /* black at 60% opacity */
}

You will also meet the older comma style, rgba(0, 0, 0, 0.6). It still works everywhere; rgb() and rgba() are now simply two names for the same function.

hsl(): the designer-friendly model

hsl() describes a colour the way people think about it:

  • Hue: an angle on the colour wheel from 0 to 360 degrees. 0 is red, 120 is green, 240 is blue.
  • Saturation: 0% is grey, 100% is the most vivid version of the hue.
  • Lightness: 0% is black, 100% is white, 50% is the "pure" colour.

The big advantage is that you can make a lighter or darker shade just by changing one number. hsl(220 60% 50%) is a medium blue; hsl(220 60% 30%) is a dark navy of the same hue, and hsl(220 60% 92%) is a pale tint for backgrounds.

0° red 60° yellow 120° green 180° cyan 240° blue 300° magenta hue 220° Hex #3366cc RGB rgb(51 102 204) HSL hsl(220 60% 50%) With alpha rgb(51 102 204 / 50%)
Figure 1. The same medium blue written four ways. In HSL the hue is an angle on the colour wheel (220° sits between cyan and blue); the last row adds 50% transparency.

Transparency: alpha vs opacity

There are two ways to make something see-through, and they behave differently:

  • An alpha channel in a colour, such as rgb(0 0 0 / 50%), affects only that one colour. A card with a semi-transparent background keeps fully solid text.
  • The opacity property, such as opacity: 0.5, fades the whole element, including its text, borders and children.

Modern colour: oklch() and color-mix()

Newer colour functions are supported in all current major browsers. oklch(L C H) uses lightness, chroma (colourfulness) and hue, and is designed so that equal changes in lightness look equal to the human eye, which makes building palettes easier. color-mix() blends two colours: color-mix(in srgb, #3366cc 70%, white) gives a lighter blue. You don't need these yet, but you'll see them in modern codebases.

Try it

body {
  background-color: hsl(40 60% 96%);
  color: hsl(220 30% 20%);
}

h1 {
  color: #3366cc;
}

a {
  color: rgb(180 83 9);
}

button {
  background-color: hsl(220 60% 50%);
  color: white;
  border: 3px solid hsl(220 60% 30%);
  padding: 0.5rem 1rem;
}

button:hover {
  background-color: hsl(220 60% 40%);
}

The page gets a warm off-white background with dark slate text. The heading is medium blue, the link is a burnt orange, and the button is blue with a darker blue border; hovering over it darkens it slightly because only the lightness value changed.

Tip

Always check contrast between text and background. Light grey text on white may look stylish but is hard to read on a phone in bright sunlight. Browser DevTools show a contrast ratio when you inspect a colour; aim for at least 4.5:1 for normal text.

Key points

  • Use color for text and background-color for backgrounds; text colour is inherited.
  • Hex (#3366cc) and rgb() describe red, green and blue channels.
  • hsl() makes shades easy: keep the hue, change the lightness.
  • An alpha value affects one colour; opacity fades the whole element.
Up next · Getting StartedCSS Unitspx, %, em, rem, vw, vh and when to use each.
→

Tip: use the ← and → keys to move between pages.