← Learning Hub
Beginner

Colours and Backgrounds

Colours can be written as names (tomato), hex codes (#ff6347) or RGB values (rgb(255, 99, 71)). Hex codes are the most common in real projects.

A hex code like #ff6347 is three pairs of numbers for red, green and blue light, each from 00 (none) to ff (full). #000000 is black, #ffffff is white. You don't need to calculate them - colour pickers in VS Code, Figma or Google ("color picker") give you the code.

Colour sets the mood of a website: blue feels trustworthy (banks), green feels fresh (food, money), red grabs attention (sales). Most good sites use just one or two main colours, plus greys for text and backgrounds. Too many colours make a page look messy.

background-color fills an element with colour. background can also hold a gradient, where one colour blends into another.

Example
<!DOCTYPE html>
<html>
  <head>
    <style>
      .box { padding: 20px; margin: 10px 0; color: white; border-radius: 10px; }
      .a { background-color: #16a34a; }
      .b { background-color: rgb(37, 99, 235); }
      .c { background: linear-gradient(90deg, #f97316, #db2777); }
      .d { background-color: rgba(0, 0, 0, 0.5); }
    </style>
  </head>
  <body>
    <div class="box a">Hex colour</div>
    <div class="box b">RGB colour</div>
    <div class="box c">Gradient</div>
    <div class="box d">Semi-transparent black (rgba)</div>
  </body>
</html>
▶ Try it yourself
Make sure text has enough contrast with its background. Light grey text on white is hard to read, especially on a phone in sunlight.
Key points
  • Colours: names, hex (#ff6347) or rgb().
  • background can be a colour, a gradient or an image.
  • Keep enough contrast between text and background.

📝 Test yourself

1. Which is a valid hex colour?
2. What does the "a" in rgba() control?
3. Fill in the property that fills an element with colour:
: yellow;