CSS Basics · Lesson 4 of 11

Colors and Backgrounds

Color names are easy, but there are only about 140 of them. For exact colors, CSS has other ways to write a value.

Hex codes

A hex code is a # followed by six characters: two for red, two for green, two for blue. Each pair goes from 00 (none) to ff (full).

h1 {
  color: #1d4ed8;   /* a strong blue */
}
  • #000000 is black and #ffffff is white.
  • #ff0000 is pure red.
  • When all three pairs are equal you get a gray: #888888.

rgb()

The same three amounts can be written as numbers from 0 to 255:

p {
  color: rgb(55, 65, 81);
}

A fourth number sets how see-through the color is, from 0 (invisible) to 1 (solid): rgb(0 0 0 / 0.5) is half-transparent black.

Backgrounds

color is for text. The property for what is behind it is background-color:

body {
  background-color: #f5f5f4;
}

Contrast

Text must stand out from its background, or people cannot read it. Dark text on a light background, or light text on a dark one. Pale gray text on white looks elegant in a design and is unreadable on a phone in sunlight.

Your Task

  1. Give the body the background color #0f172a (a dark navy) and the text color #f8fafc (near white).
  2. Make the <h1> the color #fbbf24 (amber).
  3. Give elements with the class tag a background of rgb(51, 65, 85).
  • The page background is #0f172a
  • The body text is #f8fafc, and the paragraphs inherit it
  • The heading is #fbbf24
  • The tags have the background rgb(51, 65, 85)