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"
On this page

Example

#
CSS
:root {
  --text: #e5e5e5;
  --muted: #a3a3a3;
  --accent: #a78bfa;
}

body { color: var(--text); }
.muted { color: var(--muted); }
a { color: var(--accent); }
Preview

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

#
Syntax
color: <color>;

Values

#
  • tomato

    name

    One of the 140 colour names recognised by CSS.
  • #38bdf8

    hexadecimal

    Hexadecimal notation: two digits each for red, green and blue.
  • rgb(52 211 153)

    rgb()

    Red, green and blue components in the range 0-255.
  • hsl(280 90% 70%)

    hsl()

    Hue, saturation and lightness. It is easy to darken or lighten the colour.
  • oklch(80% 0.15 80)

    oklch()

    A modern colour space in which the same lightness looks equally bright for every hue.

Values in practice

#

tomato

One of the 140 colour names recognised by CSS.
.demo { color: tomato; }

#38bdf8

Hexadecimal notation: two digits each for red, green and blue.
.demo { color: #38bdf8; }

rgb(52 211 153)

Red, green and blue components in the range 0-255.
.demo { color: rgb(52 211 153); }

hsl(280 90% 70%)

Hue, saturation and lightness. It is easy to darken or lighten the colour.
.demo { color: hsl(280 90% 70%); }

oklch(80% 0.15 80)

A modern colour space in which the same lightness looks equally bright for every hue.
.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

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.