Chapter 16. Specifying Color in HTML

You can specify the color of certain page elements using HTML tags. There are two methods for specifying colors in web documents: RGB values and color name.

Specifying Color by RGB Values

The most common and precise way to specify a color in HTML is by its numerical RGB (red, green, blue) values. For an explanation of RGB color, see Section 3.3 in Chapter 3.

Once you’ve identified the red, green, and blue values of your chosen color (an image-editing tool helps with this task), you need to translate them to their hexadecimal equivalents in order to enter them into the HTML color tag. These values are written in HTML with the following syntax:

"#RRGGBB"

RR stands for the hexadecimal red value, GG stands for the hexadecimal green value, and BB stands for the hexadecimal blue value. Using these values, you can specify any color from the “true color” space (millions of colors).

Let’s look at an example to put this in context. To set the background color of a document to dark olive green, first find the RGB values of the color (85, 107, 47) and convert them into their hexadecimal equivalents (55, 6B, 2F; explanation of conversion follows). Then the values can be placed in an HTML tag, in this case to change the background color of a page:

<BODY BGCOLOR="#556B2F">

The Hexadecimal System

The hexadecimal numbering system is base-16 (as compared to base-10 for decimal numbers). It uses the following 16 characters:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, ...

Get Web Design in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.