H5 - Using Color

01 - What do you use color for?

With CSS you can give text, backgrounds, links, borders and accents a color.

body {
    background-color: white;
    color: black;
}

a {
    color: blue;
}
  • color controls text color.
  • background-color controls background color.
  • border-color controls border color.

Practice

  • Test this color technique with two or three colors of your own.
  • Check whether the text and background are still easy to read.
02 - Different ways to write color
  • Color names: red, blue, black.
  • Hex values: #ff0000.
  • RGB values: rgb(255 0 0).
  • HSL values: hsl(0 100% 50%).
  • Modern color functions such as oklch().
p {
    color: red;
}

Practice

  • Test this color technique with two or three colors of your own.
  • Check whether the text and background are still easy to read.
03 - Hex, RGB and transparency
p {
    color: #222222;
}

section {
    background-color: rgb(255 230 120);
}

div {
    background-color: rgb(0 0 0 / 50%);
}

The last example uses transparency: the background is black with 50% opacity.

Practice

  • Test this color technique with two or three colors of your own.
  • Check whether the text and background are still easy to read.
04 - HSL: hue, saturation and lightness
  • Hue is the color angle.
  • Saturation is how strong the color is.
  • Lightness is how light or dark the color is.
h1 {
    color: hsl(220 80% 50%);
}

Practice

  • Test this color technique with two or three colors of your own.
  • Check whether the text and background are still easy to read.
05 - Modern CSS color: OKLCH

oklch() is a modern CSS color method. It is useful because colors can be adjusted in a way that often feels more natural to the human eye.

h1 {
    color: oklch(60% 0.18 250);
}

Practice

  • Test this color technique with two or three colors of your own.
  • Check whether the text and background are still easy to read.
06 - Mixing colors with color-mix()

color-mix() lets CSS create a new color by mixing two colors.

section {
    background-color: color-mix(in oklch, blue 10%, white);
    border: 2px solid color-mix(in oklch, blue 35%, white);
}

Practice

  • Test this color technique with two or three colors of your own.
  • Check whether the text and background are still easy to read.
07 - Keeping colors at the top with variables

CSS variables let you store important colors once and reuse them.

:root {
    --background-color: white;
    --text-color: black;
    --accent-color: royalblue;
}

body {
    background-color: var(--background-color);
    color: var(--text-color);
}

a {
    color: var(--accent-color);
}

Practice

  • Test this color technique with two or three colors of your own.
  • Check whether the text and background are still easy to read.
08 - Contrast: text must be readable

A common guideline is: normal text needs a contrast ratio of at least 4.5:1. Large text needs at least 3:1.

/* Good contrast */
p {
    background-color: white;
    color: black;
}

/* Often too little contrast */
p {
    background-color: white;
    color: lightgray;
}

Choose two colors here to check the contrast.

Example text

Contrast: 21:1 Good for normal and large text.

Practice

  • Test this color technique with two or three colors of your own.
  • Check whether the text and background are still easy to read.
09 - Background of the body

You often set the page background on the body. Start with a plain color as fallback. You can also use an image and let it cover the screen.

body {
    background-color: #888888;
}

body {
    background: #888888 url("background.jpg") center center fixed;
    background-size: cover;
}

Practice

  • Test this color technique with two or three colors of your own.
  • Check whether the text and background are still easy to read.
10 - Assignment

Practice

  1. Create an HTML page with headings, paragraphs and a link.
  2. Create an external CSS file.
  3. Choose colors for the body, headings and links.
  4. Use variables at the top of your CSS file.
  5. Check whether the text has enough contrast.
Quiz

Question 1. Which CSS property sets text color?



Correct. color sets text color.
Not quite. The property is color.

Question 2. What is a CSS variable useful for?



Correct. Variables keep repeated values in one place.
Not quite. Variables store reusable values.