← Web basics: HTML, CSS and JavaScript

Colors

Color names, hex codes and rgb() for text and backgrounds.

Start this lesson

CSS has two everyday color properties: color for text and background-color for the area behind it. You can write a color in several ways. Names like tomato or teal are easy to read. Hex codes like #2a9d8f give you any of 16 million colors: three pairs of hex digits for red, green and blue, from 00 (none) to ff (full). rgb(42, 157, 143) says the same thing in plain numbers from 0 to 255. Browsers turn every form into the same thing in the end, so pick whichever you find clearest.

.badge {
  color: white;
  background-color: #2a9d8f;
}
.alert {
  color: rgb(120, 0, 0);
  background-color: mistyrose;
}

The badge has white text on a teal-green background given as hex. The alert uses rgb() for a dark red text color on a pale pink named color.

#000000 (or rgb(0, 0, 0)) is black: no light at all. #ffffff (rgb(255, 255, 255)) is white: all three lights at full. Equal amounts of each give grays.

Short hex works too: #f60 is the same as #ff6600.