Learn Colour in CSS

The color property colours text: h1 { color: blue; }. The same colour can be written as a name (blue), a hex code (#0000ff), rgb(0, 0, 255) or hsl(240, 100%…

In your friend's design, the heading colour is written as #663399. In earlier lessons you wrote blue, a plain colour name. Both can be the value of the color property, which sets the colour of text.

The value of color can be an English colour name (blue) or a number code: hex (such as #663399), rgb() or hsl(). All four ways can give exactly the same colour.

Colouring text with color

This rule colours the heading with a colour name:

h1 {
  color: rebeccapurple;
}

The result: every <h1> heading turns purple. rebeccapurple is one of about 140 colour names built into CSS. All of them are English and written without spaces: red, blue, yellow, white, lightblue. If the rule is already there, like h1 { color: black; } in the first exercise, just change the value.

Property names are English too, in American spelling: color, not colour. The browser skips a misspelled property silently, so the text keeps its old colour.

color only colours the letters. The box behind the text is coloured with background-color, in the next lesson.

One colour, four ways to write it

These four lines write one colour in four ways, so pick any one:

color: rebeccapurple;      /* color name */
color: #663399;            /* hex */
color: rgb(102, 51, 153);  /* rgb */
color: hsl(270, 50%, 40%); /* hsl */

Before you read on, try to guess: is rgb(255, 255, 255) black or white? White, because red, green and blue are all full.

When to use which:

Transparency and currentColor

⚠️ The colour doesn't show, or is hard to read?

TL;DR: The color property colours text: h1 { color: blue; }. The same colour can be written as a name (blue), a hex code (#0000ff), rgb(0, 0, 255) or hsl(240, 100%, 50%). The fourth number in rgba() or hsla() sets transparency, from 0 (see-through) to 1 (solid). currentColor means the element's own text colour, and you can use it in other properties. Spell the property color, not colour.

⚡ A colour for the letters

Write:

h1 {
  color: rebeccapurple;
}

Meaning: The color property colours the letters, not the box behind them. Here every <h1> heading turns purple (rebeccapurple is the name of a purple colour). Colour names are English and written without spaces (lightblue), and the property is spelled color, not colour.

⚡ One colour, four ways to write it

Write:

color: rebeccapurple;      /* color name */
color: #663399;            /* hex */
color: rgb(102, 51, 153);  /* rgb */
color: hsl(270, 50%, 40%); /* hsl */
Guess first: is rgb(255, 255, 255) black or white?

White. The three numbers in rgb() are the amounts of red, green and blue light, from 0 to 255. Here all three are full.

Meaning: These four lines give exactly the same colour, so pick any one. A hex code (the second line) always starts with #. Without the #, the browser ignores the colour.

⚡ Following the text colour

Guess first: a border whose colour is set to currentColor turns what colour?

The same as that element's text colour. currentColor means "this element's color value".

Meaning: Another property set to currentColor, such as a border's colour, follows the element's text colour. When color changes, that property changes too.

What you will learn