color
Sets the text colour and the default colour of borders, decorations and SVG icons that use currentcolor.
- Initial value
canvastext (browser dependent)- Inherited
- Yes
- Animatable
- yes, as a colour
- Applies to
- all elements
- JavaScript
element.style.color = "#a78bfa"
Example
#:root {
--text: #e5e5e5;
--muted: #a3a3a3;
--accent: #a78bfa;
}
body { color: var(--text); }
.muted { color: var(--muted); }
a { color: var(--accent); }<p style="margin: 0 0 6px; color: #e5e5e5;">The page’s main text.</p>
<p style="margin: 0 0 6px; color: #a3a3a3;">Secondary text in a muted colour.</p>
<a href="#" style="color: #a78bfa;">Link in the accent colour</a>Definition and usage
#The color property sets an element’s text colour. It is inherited, so setting it on body colours the whole document until some element changes it.
A colour can be written in many ways: by name (tomato), in hexadecimal (#a78bfa), with the rgb() and hsl() functions, and in modern browsers also in the oklch() and lab() colour spaces, which give more predictable lightness.
The color value is also available through the currentcolor keyword. Borders, underlines and shadows use it by default, and SVG icons with fill="currentColor" take on the colour of the surrounding text.
Syntax
#color: <color>;Values
#name
One of the 140 colour names recognised by CSS.hexadecimal
Hexadecimal notation: two digits each for red, green and blue.rgb()
Red, green and blue components in the range 0-255.hsl()
Hue, saturation and lightness. It is easy to darken or lighten the colour.oklch()
A modern colour space in which the same lightness looks equally bright for every hue.
Values in practice
#tomato
.demo { color: tomato; }#38bdf8
.demo { color: #38bdf8; }rgb(52 211 153)
.demo { color: rgb(52 211 153); }hsl(280 90% 70%)
.demo { color: hsl(280 90% 70%); }oklch(80% 0.15 80)
.demo { color: oklch(80% 0.15 80); }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Check the contrast between text and background. For regular text, the WCAG guidelines recommend a ratio of at least 4.5:1.
- Keep colours in CSS variables. Changing the theme then means swapping a few values instead of searching the whole stylesheet.
Related entries
#- background-colorSets the background colour of an element.
- opacitySets the transparency of the whole element together with its contents.
- border-colorSets the color of a border.
- accent-colorChanges the accent colour of form controls: checkboxes, radio buttons, sliders, and progress bars.
- rgb()Writes a colour as a mix of red, green, and blue light, with optional transparency. It also covers hex notation.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.