Explain it simply
Explain it simply
Every pixel on a screen mixes red, green and blue light. CSS gives you several ways to describe that mix: a name such as tomato (a keyword), a hex code such as #1d4ed8, three light amounts such as rgb(29 78 216), or a position on the colour wheel plus intensity and lightness, such as hsl(224 76% 48%).
Whichever notation you choose, the result matters most: text must be readable against its background for everyone, and an error must be stated in words, not only in red.
Problem → Why → Solution
The problem: it looks elegant in the design file and unreadable on a phone
A signup form writes its help text in a pale grey, #aaaaaa, and marks an invalid field only by turning its border red. On the developer's large monitor it looks clean. Users on a phone in sunlight cannot read the help text, and colour-blind users who cannot tell red from green do not know which field is wrong, so they keep resubmitting the form.
.help-text {
color: #aaaaaa; /* contrast on white: about 2.3:1 */
}
.field-error {
border-color: red; /* the only sign that something is wrong */
}Why does this happen?
Contrast ratio measures the lightness difference between text and background, from 1:1 (identical) to 21:1 (white on black). The WCAG accessibility guidelines require at least 4.5:1 for normal text, and #aaaaaa on white reaches only about 2.3:1. And when colour is the only carrier of information, people who cannot distinguish that colour lose the information completely.
Core concept: colour notations, alpha and contrast
| Notation | Example | When it helps |
|---|---|---|
| Keyword | white, tomato, transparent | Quick tests, plain white or black |
| Hex | #1d4ed8, #fff, #1d4ed8b3 | Colours copied from design tools |
| rgb() | rgb(29 78 216 / 70%) | Controlling each channel and alpha |
| hsl() | hsl(224 76% 48%) | Making lighter or darker shades of one hue |
A hex code writes each channel as two hexadecimal digits (00 to ff), so #1d4ed8 is red 29, green 78, blue 216. With eight digits (#1d4ed8b3) the last two are the alpha (opacity of the colour). In hsl(), hue is an angle on the colour wheel (0 red, 120 green, 240 blue), saturation is intensity, and lightness runs from 0% (black) to 100% (white). Changing only the lightness gives you a darker shade for a hover state, which is why hsl() feels closer to how people think.
In the modern syntax, values are separated by spaces and the alpha comes after a slash: rgb(29 78 216 / 70%). The legacy comma syntax rgba(29, 78, 216, 0.7) still works, and rgba() is simply an alias of rgb(). You cannot mix the two styles: rgb(29, 78, 216 / 0.7) is invalid, so the browser drops the whole declaration.
Alpha and the opacity property are different. background-color: rgb(0 0 0 / 50%) makes only the background see-through, while the text and child elements stay fully solid. opacity: 0.5 fades the whole element, including text, borders, images and children. When you check contrast on a translucent background, measure the colour that results after it blends with what is underneath. WCAG AA asks for 4.5:1 for normal text and 3:1 for large text (about 24px regular or 18.66px bold), and AAA asks for 7:1.
- Pick a notation: hex, rgb or hsl
- Add alpha after a / if needed
- Measure contrast with the background
- State meaning with text or an icon
Notations are just different spellings of a colour; contrast alone decides readability.
General pattern: colour values
selector {
color: keyword; /* white, navy, transparent */
color: #rrggbb; /* or #rgb, or #rrggbbaa */
color: rgb(red green blue); /* 0-255 each */
color: rgb(red green blue / alpha); /* alpha 0-1 or 0%-100% */
color: hsl(hue saturation% lightness%); /* hue in degrees */
color: hsl(hue saturation% lightness% / alpha);
}color sets the text colour, background-color the background and border-color the border; every property that accepts a colour accepts all of these value forms.
Example: four notations and a readable card
This page uses all four notations. Open it, inspect the .card and click the small swatch next to a colour value: the DevTools colour picker shows the contrast ratio and ticks whether it passes AA and AAA.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Colours</title>
<style>
body {
font-family: system-ui, sans-serif;
color: #1f2937; /* hex */
background-color: white; /* keyword */
}
.title {
color: rgb(29 78 216); /* rgb(), space syntax */
}
.card {
padding: 1rem;
color: white;
background-color: rgb(15 23 42 / 85%); /* navy, 85% opaque */
border: 4px solid hsl(190 90% 45%); /* hue saturation lightness */
}
.hint {
color: hsl(220 9% 40%); /* a grey that passes AA */
}
</style>
</head>
<body>
<h1 class="title">Colour notations</h1>
<div class="card">White text on a translucent navy card</div>
<p class="hint">Grey hint text that still passes contrast</p>
</body>
</html>Code breakdown
- Hex and keyword
color: #1f2937; /* hex */#1f2937 is a dark grey with red 31, green 41 and blue 55. Body text often uses something slightly softer than pure black (#000), and this one still has over 14:1 contrast on white.
- Modern rgb()
color: rgb(29 78 216); /* rgb(), space syntax */This is the modern syntax, separated by spaces instead of commas. It is the same colour as #1d4ed8: the hex pairs 1d, 4e and d8 are 29, 78 and 216 in decimal.
- Alpha on the background only
background-color: rgb(15 23 42 / 85%); /* navy, 85% opaque *// 85% makes only the background colour 85% opaque. The white text inside stays fully solid; with opacity: 0.85 instead, the text would fade too.
- hsl() for a border
border: 4px solid hsl(190 90% 45%); /* hue saturation lightness */Hue 190° sits near cyan, saturation 90% is vivid and lightness 45% is medium. For a darker version, change only 45% to 35%.
- A grey that passes
color: hsl(220 9% 40%); /* a grey that passes AA */With 40% lightness this grey reaches about 6:1 on white. Compare it with the problem's #aaaaaa (about 67% lightness) to see where a muted but readable grey ends.
The heading is blue, rgb(29, 78, 216). The card shows white text on navy (85% opaque, so it blends with the white page to about rgb(51, 58, 74), a contrast above 11:1) with a 4px cyan border, rgb(11, 184, 218). The hint text is grey, rgb(93, 99, 111), with about 6:1 contrast on white, which passes AA.currentColor and newer colour functions
The currentColor keyword reuses the element's current color value. Writing border-left: 4px solid currentColor; makes the border follow the text colour whenever it changes, so the colour is controlled in one place. Newer functions such as oklch() spread lightness more evenly for human eyes, and design systems are adopting them. Current major browsers support them, but check MDN for the browsers you target first.
.button {
color: white;
background-color: hsl(224 76% 48%);
}
.button:hover {
background-color: hsl(224 76% 40%); /* same hue, darker */
}
.callout {
color: #166534;
border-left: 4px solid currentColor; /* follows the text colour */
}In real projects
Where this shows up in real projects
Product teams define colours once as meaningful design tokens (CSS variables) such as --color-text and --color-danger, and components use the tokens instead of raw hex codes. Changing the brand colour or adding dark mode then means changing the token values. You will study variables in detail in this course's Variables lesson.
Accessibility checks are part of everyday work too. The DevTools colour picker, Lighthouse audits and tools such as axe flag text with too little contrast. Form errors usually pair a red border with text such as "Email is required" and an icon, and charts add labels or patterns on top of colour. Disabled controls are exempt from WCAG's contrast requirement, but keeping them readable is still kinder to users.
Common mistakes
Common mistakes
1. Using opacity to make only a background translucent
Adding opacity: 0.7 to a card over a hero image also fades the card's text and lowers its contrast. To make only the background translucent, use a colour with alpha, such as background-color: rgb(0 0 0 / 70%).
2. Mixing commas and the slash
rgb(29, 78, 216 / 0.5) and hsl(220, 80%, 50% / 0.5) are invalid, so the browser silently drops the declaration and the colour falls back to the inherited or default value. Choose either spaces with a slash, rgb(29 78 216 / 0.5), or commas only, rgba(29, 78, 216, 0.5).
3. Judging contrast by eye
On a bright, high-quality monitor a pale grey seems perfectly readable. Do not trust your eyes; measure the ratio with the DevTools colour picker and aim for 4.5:1 for normal text.
Best practices
Best practices
- Use one main notation per project (hex or hsl, for example) so the team reads colours consistently.
- Check contrast for every text and background pair in the hover, focus and disabled states, not only the normal one.
- Do not distinguish links by colour alone; keep the underline so colour-blind users still recognise them.
- For text on top of a photo, add a translucent dark overlay and check contrast against the photo's lightest area.
Mini challenge
Mini challenge: three readable status messages
Pick text and background colour pairs for the success, warning and error messages in the starter and style them. Every pair must pass 4.5:1, and meaning must not rely on colour alone.
- Each message's text and background must have at least 4.5:1 contrast (check with the DevTools colour picker)
- Write the backgrounds with the modern space-separated rgb() syntax
- Make the left border match the text colour with currentColor
- Keep the "Success:", "Warning:" and "Error:" words in the HTML; they state the meaning in text
<!-- TODO: wrap in <!DOCTYPE html>, <head> and <body> -->
<style>
/* TODO: .message - padding + a 4px left border in currentColor */
/* TODO: .success - dark green text on a pale green rgb() background */
/* TODO: .warning - dark brown text on a pale yellow rgb() background */
/* TODO: .error - dark red text on a pale red rgb() background */
</style>
<p class="message success"><strong>Success:</strong> Your profile was saved.</p>
<p class="message warning"><strong>Warning:</strong> Your card expires soon.</p>
<p class="message error"><strong>Error:</strong> Email is required.</p>Expected: The three messages show dark text on pale green, yellow and red backgrounds with a matching left border. Each pair exceeds 6:1, and words such as "Error:" carry the meaning for anyone who cannot see the colours.
Show hint
Pair a very light background with a very dark text colour. For example, #166534 text on an rgb(220 252 231) background reaches about 6.5:1.
Show solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Status messages</title>
<style>
.message {
padding: 0.75rem 1rem;
border-left: 4px solid currentColor;
}
.success {
color: #166534;
background-color: rgb(220 252 231); /* about 6.5:1 */
}
.warning {
color: #92400e;
background-color: rgb(254 243 199); /* about 6.4:1 */
}
.error {
color: #991b1b;
background-color: rgb(254 226 226); /* about 6.8:1 */
}
</style>
</head>
<body>
<p class="message success"><strong>Success:</strong> Your profile was saved.</p>
<p class="message warning"><strong>Warning:</strong> Your card expires soon.</p>
<p class="message error"><strong>Error:</strong> Email is required.</p>
</body>
</html>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- Keywords, hex (#rrggbb, #rrggbbaa), rgb() and hsl() are different spellings of the same colours.
- The modern syntax is rgb(29 78 216 / 50%); do not mix it with the comma form.
- Alpha makes one colour translucent; opacity makes the whole element translucent.
- Measure 4.5:1 for normal text and 3:1 for large text, and state meaning with text or an icon, not colour alone.
CSS reference — MDN Web Docs