Background Colour — CSS

background-color colours an element's background, the box behind the text. color colours the letters. An example: h1 { background-color: yellow; }. The colour…

Imagine writing with a blue marker on yellow paper. You choose the ink colour and the paper colour separately. CSS works the same way. The color property sets the ink colour, which is the text colour. The background-color property sets the paper colour, which is the element's background: the box behind its text.

Colouring the background

This rule gives the heading a green background and black text:

h1 {
  background-color: #12d994;
  color: #000000;
  padding: 8px;
}

The green background stretches across the page, because an <h1> fills a whole line. The value of background-color is written the same way as the value of color: a colour name (yellow), hex, rgb() or hsl().

One rule can hold many declarations. In the exercise, the rule h1 { color: black; } is already there. Add background-color inside the same curly braces, before the }.

Text and background need contrast

Contrast is the difference in lightness between the text and its background. Black text on a dark background is hard to read, because the contrast is low. If the background is dark, use light text, such as color: white;. The accessibility guideline (WCAG) asks for a contrast ratio of at least 4.5:1 for normal text (a bigger number means more contrast). DevTools (the developer tools inside the browser) can calculate it while you pick a colour.

transparent and the background shorthand

⚠️ When the background doesn't change, or the text vanishes

TL;DR: background-color colours an element's background, the box behind the text. color colours the letters. An example: h1 { background-color: yellow; }. The colour value is written the same way as for color. Keep enough contrast (difference in lightness) between text and background. The default for background-color is transparent: the background is see-through, so you see the parent's background. background is a shorthand: one property for every part of the background at once. background: red; sets every part you don't mention back to its default value, so a background image is removed. To change only the colour, use background-color.

⚡ Ink and paper

Write:

h1 {
  background-color: #12d994;
  color: #000000;
  padding: 8px;
}

Meaning: Think of ink and paper. color is the ink colour (the letters), and background-color is the paper colour (the box behind the text). Here the <h1> heading gets a green background and black text. padding: 8px; adds 8 pixels of space between the text and the edge of the background. A dark background needs light text, so the words can still be read.

⚡ A see-through background

Guess first: a paragraph with background-color: transparent inside a blue box looks what colour?

Blue. transparent means see-through, so you see the background of the blue box that wraps it.

Meaning: transparent is the default for background-color. So an element whose background you don't set shows the background of its parent.

⚡ Changing only the colour

Meaning: background is a shorthand: one property that sets every part of the background at once. Every part you don't mention goes back to its default value. So background: red; makes the background red, but it also removes a background image. If you only want to change the colour, use background-color.

What you will learn