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;
}
h1is the selector: the elements to colour, here every<h1>.coloris the property for text colour, andrebeccapurpleis its value.
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 */
- Hex:
#then six digits. Each digit is a number from 0 to 9 or a letter from a to f. The six digits are read as three pairs: the amounts of red, green and blue light. Each pair goes from00(none) toff(full).#ffffffis white,#000000is black,#ff0000is red. When the two digits in every pair are the same, you can shorten the code:#ffffffbecomes#fff. - rgb(): three numbers for red, green and blue, each from 0 (none) to 255 (full).
- hsl(): three numbers. The first is the hue, a position on the colour wheel, from 0 to 360 (0 red, 120 green, 240 blue). The second is the saturation (how strong the colour is). The third is the lightness (0% black, 100% white). For a lighter or darker version of the same colour, change only the third number.
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:
- A colour name: quick examples and exercises.
- Hex: copying a colour from a design app, like
#663399from your friend. hsl(): making a lighter or darker version of one colour, by changing its third number.oklch(): a set of colours that must all look equally bright. This fifth way is covered later in the Modern Colour Functions lesson.
Transparency and currentColor
rgba(0, 0, 0, 0.5):rgb()plus a fourth number, called alpha. Alpha sets the transparency, from0(see-through) to1(solid).0.5means half transparent.hsla()ishsl()with the same fourth number.rgb()also has a newer form. It writes the numbers with no commas, and the alpha after a slash:rgb(102 51 153 / 50%). The result is the same asrgba(102, 51, 153, 0.5).rgb()andhsl()now accept an alpha directly, sorgba()andhsla()are only other names for them. Hex can be transparent too, with two extra digits at the end:#66339980is half transparent.currentColormeans "this element'scolorvalue". Another property set tocurrentColor, such as a border's colour, follows the element's text colour. Whencolorchanges, that property changes too.
⚠️ The colour doesn't show, or is hard to read?
- The browser ignores
color: ff0000;, because the#is missing. Write#ff0000. - The browser doesn't know
light blue, because colour names are written without spaces. The correct name islightblue. - Light grey text on a white background is hard to read. Pick a text colour that is much darker or much lighter than its background.
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.