Sub topic 9
Adding success, warning, and error colours
This chapter assumes you have read the earlier chapters. You should have a primary, a neutral scale and an accent expressed as HSL custom properties. You should also know the WCAG thresholds (4.5:1 for normal text, 3:1 for large text and UI components) and have the contrastRatio function from the contrast chapter. You need basic HTML and CSS.
Why status colours are a separate role
Primary, neutral and accent colours express your brand. Success, warning and error colours express the state of something. A form field is invalid, a payment went through, a disk is almost full. Users learn these meanings from every other product they use: green means fine, amber means careful, red means something is wrong.
Because the meanings come from outside your brand, you do not get to choose them freely. You can adjust the exact hue, saturation and lightness so they sit well beside your palette. You should not make errors blue or success red.
Status colours are also reserved. If the accent is orange and you use it for promotional banners, users may read those banners as warnings. Treat each status colour as a signal that only a status uses.
Choosing the hues
Start from the conventional hue and move only as far as your palette requires.
| Status | Typical hue (HSL) | Common use |
|---|---|---|
| Success | 130 to 160 | Saved, paid, passed, online |
| Warning | 35 to 48 | Needs attention, expiring, reversible risk |
| Error | 0 to 8 | Failed, invalid, destructive action |
| Info (optional) | Your primary hue | Neutral notices and tips |
Info is optional. Most sites reuse the primary colour for informational messages. A fourth status hue adds one more colour for users to learn and rarely pays for itself.
Check each status hue against your brand colours.
- A red or green primary. If your primary is close to hue 0 or hue 145, buttons will look like errors or confirmations. Either move the status hue about 15 to 20 degrees away, or rely more heavily on icons and text.
- An orange or yellow accent. The accent and the warning will compete. Pick an accent hue at least 30 degrees from the warning hue, or use the warning only in alert components.
- Colour-vision deficiency. Red and green are hard to tell apart for many people, especially in the mid-lightness range. Differences in lightness and shape (covered below) are what keep the two readable.
Building a scale for each status colour
You do not need eleven shades per status colour. Three or four roles cover almost every use:
- A light tint for the background of an alert or badge.
- A mid-tone for borders and icons.
- A strong, dark shade for text on the tint.
- A solid fill for buttons and filled badges, with its own text colour.
The code below defines them in HSL, as in the palette chapter. The hue stays constant inside each scale, and lightness does the work.
:root {
/* Success: green */
--success-100: hsl(145 55% 92%);
--success-300: hsl(145 45% 62%);
--success-500: hsl(145 63% 28%);
--success-700: hsl(145 65% 18%);
/* Warning: amber */
--warning-100: hsl(45 100% 92%);
--warning-300: hsl(42 90% 62%);
--warning-500: hsl(40 95% 50%);
--warning-700: hsl(32 90% 22%);
/* Error: red */
--error-100: hsl(4 85% 94%);
--error-300: hsl(4 70% 68%);
--error-500: hsl(4 75% 45%);
--error-700: hsl(4 80% 26%);
}The 500 steps are not all alike, and that is deliberate.
--success-500and--error-500are dark enough for white text. White on them gives roughly 5.7:1 and 5.5:1.--warning-500is a bright amber. Amber is a light colour even at full saturation, so white text on it fails. Pair it with a dark neutral instead.--neutral-900on it gives more than 8:1.
The naming matches the primary scale, so "500 is the solid fill" holds everywhere. The text colour that goes on top changes per status. That is why the next section defines text tokens explicitly instead of assuming white.
Never rely on colour alone
WCAG success criterion 1.4.1 (Use of Color) says colour must not be the only way to convey information. About one in twelve men has some form of colour-vision deficiency, and colour is also unreliable on a washed-out screen or in a printout.
Give each status at least one more cue:
- A distinct icon (a check, a triangle, a circle with an X).
- A text label, or a message that says what happened ("Payment failed", not just a red box).
- For form errors, a message next to the field, linked to it with
aria-describedby.
The markup below shows an alert that works with no colour at all.
<div class="alert alert--error" role="alert">
<svg class="alert__icon" aria-hidden="true" viewBox="0 0 20 20" width="20" height="20">
<circle cx="10" cy="10" r="9" fill="none" stroke="currentColor" stroke-width="2"/>
<path d="M6 6l8 8M14 6l-8 8" stroke="currentColor" stroke-width="2"/>
</svg>
<p><strong>Payment failed.</strong> Check your card number and try again.</p>
</div>Warning and error also need different shapes, not just different colours. A triangle for warnings and a circle for errors is the common convention.
Contrast for status colours
Status colours cause more contrast failures than any other part of a palette. There are three situations to check.
- Text on a tinted background. An error message in
--error-700on--error-100needs 4.5:1. The 700 and 100 steps are far apart in lightness, so this normally passes with room to spare. Run it throughcontrastRatioanyway. - Text on a solid fill. A "Delete" button with white text on
--error-500needs 4.5:1 for the label. This is the case where the warning amber fails with white text. - Borders and icons. These are UI components, so they need 3:1 against whatever sits next to them.
--success-300on--success-100is a pleasant border but may fall below 3:1. If the border is what tells the user a field is invalid, use the 500 step instead. If a text message and an icon also carry the meaning, a decorative border can be lighter.
Test pairs programmatically so the check runs every time the palette changes. This reuses contrastRatio from the contrast chapter.
function channel(value) {
const s = value / 255;
return s <= 0.04045 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
}
function luminance([r, g, b]) {
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
}
function contrastRatio(colorA, colorB) {
const a = luminance(colorA);
const b = luminance(colorB);
return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
}
const white = [255, 255, 255];
const amber500 = [249, 168, 6]; // hsl(40 95% 50%) as RGB, rounded
const ink = [24, 29, 37]; // hsl(220 20% 12%) as RGB, rounded
console.log(contrastRatio(white, amber500).toFixed(2)); // low: fails 4.5
console.log(contrastRatio(ink, amber500).toFixed(2)); // high: passesRun this in Node or a browser console. White on amber comes out near 2:1, which fails even the 3:1 large-text threshold. Dark ink on amber comes out near 8.6:1.
Wiring status colours into semantic tokens
Components should not reference --error-500 directly. Add a semantic layer, as in the earlier tokens chapter, that names each status role by purpose.
:root {
--color-success-bg: var(--success-100);
--color-success-border: var(--success-500);
--color-success-text: var(--success-700);
--color-success-solid: var(--success-500);
--color-success-on-solid: #fff;
--color-warning-bg: var(--warning-100);
--color-warning-border: var(--warning-500);
--color-warning-text: var(--warning-700);
--color-warning-solid: var(--warning-500);
--color-warning-on-solid: var(--neutral-900);
--color-error-bg: var(--error-100);
--color-error-border: var(--error-500);
--color-error-text: var(--error-700);
--color-error-solid: var(--error-500);
--color-error-on-solid: #fff;
}
.alert {
display: flex;
gap: 0.75rem;
padding: 0.75rem 1rem;
border: 1px solid;
border-radius: 0.5rem;
}
.alert--success {
background: var(--color-success-bg);
border-color: var(--color-success-border);
color: var(--color-success-text);
}
.alert--error {
background: var(--color-error-bg);
border-color: var(--color-error-border);
color: var(--color-error-text);
}
.button--danger {
background: var(--color-error-solid);
color: var(--color-error-on-solid);
}The -on-solid tokens make the amber exception explicit. A component author who writes color: var(--color-warning-on-solid) gets dark text without needing to know why.
Each status now has the same set of five tokens: bg, border, text, solid and on-solid. A new status such as "info" means adding five tokens, with no component changes.
How a status colour gets chosen
The diagram summarises the decision for any element that needs to signal state.
Common errors
- Using white text on amber. This is the most common failure. Amber and yellow need dark text.
- Saturating the tint. A tint like
hsl(4 85% 94%)is acceptable. A tint at 70% lightness is not a background. It is a different colour and its text will fail contrast. - Reusing the error red for brand elements. If a red heading looks like an error, users will start to ignore real errors.
- Colour-only form validation. A red border with no message fails 1.4.1 and leaves screen-reader users with no information.
- Forgetting dark mode. Light tints are blinding on a dark page, and dark text shades vanish. Reassign the semantic tokens in the
prefers-color-scheme: darkblock. The usual swap is dark, low-lightness tints for backgrounds and light shades for text. Re-run the contrast check after the swap, because the pairs change. - Making status colours too many. Four roles (success, warning, error, and info through the primary) are enough for most sites. Extra states such as "pending" are usually better served by neutral colours and an icon.
Exercise
Exercise
Your primary colour is hsl(150 60% 35%), a green. Your success colour is hsl(145 63% 28%). Name two problems this creates and one fix for each.
Show answer
First, primary buttons and success messages look almost identical, so users cannot tell an ordinary action from a confirmation. Fix: shift the success hue away from the primary (for example towards 165 or 125), or change the primary hue to a non-green. Second, if both are used on one screen, the colour no longer means "success" because it appears everywhere. Fix: reserve the green for status components only, and always add a check icon and a text label so that meaning does not depend on hue alone.
Exercise
A warning banner uses background hsl(45 100% 92%) and text colour hsl(40 95% 50%). The text is hard to read. Which step of the warning scale should the text use, and which check proves the fix?
Show answer
Use --warning-700 (hsl(32 90% 22%)) for the text. Amber at 50% lightness is almost as light as the pale background, so the two have very low contrast. The check is contrastRatio on the text and background RGB values, which must be at least 4.5:1 for normal-size text.