On this page11 sections
  1. Learning Objectives
  2. 1. Text Colour and Background Colour
  3. 2. Colour Names
  4. 3. Hex Colours
  5. 4. RGB and HSL
  6. 5. Transparency
  7. 6. Contrast
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 003: Colours

Category1: CSS Basics
Lecture003 of 057
Estimated video length10 min
TargetCSS3 · any modern browser
LevelBeginner

Colour is the quickest way to give a page personality. CSS lets you write the same colour in several ways: a name, a hex code, RGB or HSL. Each has its uses, and transparency is only one extra number away.


Learning Objectives

  • Set text colour with color and fill colour with background-color
  • Write colours as names, hex, rgb() and hsl()
  • Add transparency with alpha values
  • Keep text readable with good contrast

1. Text Colour and Background Colour

Example

html
<style>
  .banner { color: white; background-color: darkslateblue; padding: 12px; }
</style>

<p class="banner">White text on a dark blue background.</p>
  • color: the text colour
  • background-color: the fill behind the element

2. Colour Names

CSS knows about 140 names: red, tomato, gold, teal, navy, skyblue…

Example

html
<p style="color: tomato;">tomato</p>
<p style="color: seagreen;">seagreen</p>
<p style="color: royalblue;">royalblue</p>

Names are easy to read, but there aren't many, and brand colours rarely match one exactly.


3. Hex Colours

A hex colour is # followed by red, green and blue values in hexadecimal, from 00 (none) to ff (full).

output
#ff6600
 │ │ └── blue  00
 │ └──── green 66
 └────── red   ff   → a bright orange

Example

html
<style>
  .a { background: #ff6600; }
  .b { background: #0468b5; }
  .c { background: #222; }   /* short form of #222222 */
  div { color: white; padding: 8px; margin: 4px 0; }
</style>

<div class="a">#ff6600</div>
<div class="b">#0468b5</div>
<div class="c">#222</div>

Hex is the most common format. Design tools such as Figma give you colours as hex.


4. RGB and HSL

rgb(red, green, blue): each value from 0 to 255:

css
color: rgb(255, 102, 0);   /* the same orange as #ff6600 */

hsl(hue, saturation, lightness):

  • hue: a position on the colour wheel, 0–360 (0 red, 120 green, 240 blue)
  • saturation: 0% grey → 100% vivid
  • lightness: 0% black → 50% normal → 100% white

Example: HSL makes shades of one colour easy: keep the hue, change the lightness:

html
<style>
  div { padding: 8px; margin: 3px 0; }
</style>

<div style="background: hsl(210, 80%, 25%); color: white;">hsl(210, 80%, 25%)</div>
<div style="background: hsl(210, 80%, 45%); color: white;">hsl(210, 80%, 45%)</div>
<div style="background: hsl(210, 80%, 70%);">hsl(210, 80%, 70%)</div>
<div style="background: hsl(210, 80%, 92%);">hsl(210, 80%, 92%)</div>

5. Transparency

Add a fourth alpha value from 0 (invisible) to 1 (solid):

Example

html
<style>
  .photo { background: url(https://picsum.photos/id/1018/500/200) center / cover; padding: 40px 16px; }
  .caption { background: rgb(0 0 0 / 0.55); color: white; padding: 10px; }
</style>

<div class="photo">
  <p class="caption">Semi-transparent black lets the photo show through.</p>
</div>

You can also write rgba(0, 0, 0, 0.55) or a hex with alpha, such as #00000088.


6. Contrast

Light grey on white looks stylish but can be hard to read, especially on a phone in sunlight. Keep a clear difference between text and background.

Example

html
<p style="color: #ccc; background: white;">Hard to read</p>
<p style="color: #333; background: white;">Easy to read</p>

Common Beginner Mistakes

1. Forgetting the #. color: ff6600; doesn't work. Write #ff6600.

2. Writing it the British way. The property is color, never colour, even though this lecture's title says "Colours".

3. Poor contrast. Check that text stands out clearly from its background.

4. Opacity vs alpha. opacity: 0.5 fades the whole element, text included. To fade only the background, use an alpha colour.


Practice Questions

  1. Which property sets the text colour? Which sets the fill?
  2. In #00ff00, which colour channel is at full strength?
  3. What does #fff mean?
  4. In HSL, which number would you change to make a colour lighter?
  5. Write a semi-transparent white background using rgb().
  6. Why use an alpha colour instead of opacity for a see-through background?

Practice Answers

  1. color for text, background-color for the fill.
  2. Green.
  3. White: the short form of #ffffff.
  4. The third one, lightness.
  5. background: rgb(255 255 255 / 0.5); (or rgba(255, 255, 255, 0.5))
  6. opacity also fades the text and children; an alpha colour fades only the background.

Key Takeaways

  • color = text, background-color = fill.
  • Colours can be names, hex (#ff6600), rgb() or hsl().
  • Alpha (/ 0.5 or rgba) adds transparency without fading the text.
  • Always keep strong contrast between text and background.