XML technologies / HTML & XHTML / Color specifications

(Excerpt from "HTML & XHTML ─ kurz & gut" by Jennifer Niederst Robbins)

Colors belong to the presentation of (X)HTML and should therefore be defined with Cascading Style Sheets rather than HTML attributes. The following material is included only for completeness.

In both (X)HTML and CSS colors can be specified using numeric values or standardized color names.

RGB values

The most common and precise way to define a color is by its numeric RGB (red, green, blue) values. Image editors such as Adobe Photoshop can determine a color’s RGB values on a scale from 0 to 255. For example:

  • Red: 212 Green: 232 Blue: 119

To use these numbers as attribute values, convert them to hexadecimal, base 16. In this example, the preceding values have been replaced by their hexadecimal equivalents:

  • Red: D4 Green: E8 Blue: 77

In (X)HTML, these values form a six-character sequence preceded by a # symbol:

  • #D4E877

The general syntax is:

  • #RRGGBB

RR represents red, GG represents green and BB represents blue, all in hexadecimal notation.

Adobe Photoshop conveniently displays hexadecimal values automatically at the bottom of the color picker next to the # symbol. The value can be copied directly from the picker into a stylesheet or HTML document.

If your image editor does not display hexadecimal colors, many converters are available online. Search for “RGB to Hex” or use one of the many web-based color pickers.

Standardized color names

Colors can also be specified by predefined names. The syntax is straightforward:

<body link="navy">

HTML 4.01 and XHTML 1.0 provide 16 valid color names, although many browsers support the full CSS3 set. Here are the names and corresponding hexadecimal values:

black

#000000

green

#008000

silver

#C0C0C0

lime

#00FF00

gray

#808080

olive

#808000

white

#FFFFFF

yellow

#FFFF00

maroon

#800000

navy

#000080

red

#FF0000

blue

#0000FF

purple

#800080

teal

#008080

fuchsia

#FF00FF

aqua

#00FFFF

These color names can also be used in stylesheets. The CSS 2.1 specification also lists the color orange (#FFA500). It therefore officially supports 17 colors. CSS 3 defines 140 standard color names already supported by many browsers. Using them in (X)HTML documents can, however, prevent those documents from validating.

   

 

 

 

back to top