Sub topic 3
Adding Semantic Colours to a Website Palette
Assume you know basic CSS, custom properties, and how to read hex or HSL colours. This chapter adds status colours to an existing primary, neutral, and accent palette, then makes the result easier to test and maintain.
Adding success, warning, and error colours
Semantic colours communicate meaning:
- Success confirms a completed action.
- Warning signals risk or something needing attention.
- Error explains that an action failed or input is invalid.
Choose hues that differ from the primary colour and from each other. Do not rely on hue alone. Pair colour with text, an icon, or a state label so colour-vision differences do not hide the meaning.
A status colour usually needs several lightness levels: a strong colour for icons or borders, a light surface colour, and a dark text colour.
:root {
--success-500: hsl(145 65% 36%);
--success-100: hsl(145 60% 92%);
--warning-500: hsl(38 90% 42%);
--warning-100: hsl(38 90% 90%);
--error-500: hsl(4 70% 45%);
--error-100: hsl(4 75% 93%);
}Defining the palette as CSS custom properties
Keep raw palette values separate from semantic roles. Components should use names such as --color-error-text, not --error-500. This lets you change the palette without editing every component.
:root {
--color-success-bg: var(--success-100);
--color-success-text: hsl(145 70% 22%);
--color-warning-bg: var(--warning-100);
--color-warning-text: hsl(38 90% 24%);
--color-error-bg: var(--error-100);
--color-error-text: hsl(4 75% 30%);
}
.alert-error {
background: var(--color-error-bg);
color: var(--color-error-text);
border-inline-start: 0.25rem solid var(--error-500);
}Adapting the palette for dark mode
Dark mode needs different semantic assignments. A light status surface can become too bright, while the original dark text may lose contrast.
@media (prefers-color-scheme: dark) {
:root {
--color-success-bg: hsl(145 35% 18%);
--color-success-text: hsl(145 70% 78%);
--color-warning-bg: hsl(38 35% 18%);
--color-warning-text: hsl(38 90% 78%);
--color-error-bg: hsl(4 35% 20%);
--color-error-text: hsl(4 80% 80%);
}
}Testing and documenting the finished palette
Check every text and background pair with a WCAG contrast checker. Target at least 4.5:1 for normal text and 3:1 for large text. Also test focus indicators, borders used to convey state, and status messages without their colour.
Document each token’s purpose, value, contrast result, and permitted use. Test the interface with colour-vision simulation and confirm that labels and icons preserve meaning.
Exercise
Why should a component use --color-error-text instead of --error-500?
Show answer
The semantic token separates meaning from a raw shade, so the shade can change for contrast or dark mode without changing the component.