XML technologies / CSS / Advanced websites with CSS / Adding some color / How to specify colors
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
Any CSS property that accepts color values can be assigned a color using one of the following five methods:
Color names
System-specific color values
Decimal RGB values
Hexadecimal RGB values
RGB percentages
The most common way to specify a color is by its reserved name. Although HTML and CSS support only 16 “official” names, almost every modern browser can interpret a total palette of 140 color names, as Netscape proposed in the early days of the Internet. (Note: despite defining 16 standard color names, the W3C’s guidelines require numeric values rather than color names.)
A complete list with the corresponding RGB values is available under CSS color reference. Here we will list only the 16 official color names:
black(black)white(white)aqua(light blue)blue(blue)fuchsia(pink)gray(gray)green(green)lime(lime green)maroon(maroon)navy(navy blue)olive(olive green)purple(purple)red(red)silver(silver)teal(turquoise)yellow(yellow)
I neither encourage nor discourage you from using the other 124 color names in CSS. On the one hand, no official W3C recommendation mentions them, so future browsers might stop supporting them. Nor can you predict their appearance in other browsers or operating systems without testing. Frankly, however, I see no real risk and use these names very often. They are simply the most descriptive way of specifying colors in source code and give me an idea of the effect before I see the page in a browser.
Besides descriptive color names, CSS also provides 28 system-specific color names. These names, such as AppWorkspace, refer to different parts of the operating system’s graphical user interface (GUI). Each color is determined by the operating system and possibly user preferences. These names let you create interfaces matching users’ GUIs. A complete list of system-specific color names is available under CSS color reference.
Every color on a monitor is produced by mixing the three primary colors—red, green and blue—at different intensities. These three colors offer two ways to specify a seemingly endless range of shades:
Supply three comma-separated values for the primary colors to the function
rgb.Alternatively, use three- or six-digit hexadecimal values preceded by the hash symbol (
#).
The following property definitions all produce the same result: assigning the color blue.
color: blue;
color: rgb(0, 0, 255);
color: rgb(0%, 0%, 100%);
color: #0000ff;
color: #00f;The first property simply defines the color by name. blue is one of the 16 color names supported by the W3C and can be used safely. The second property uses a decimal RGB value: red and green are set to zero, meaning they are absent, while blue gets the maximum value of 255. The third property color is identical to the second, except that the proportions are percentages. The fourth variation looks a little different: it gives the mixture in hexadecimal, with the first two digits representing red, the next two green and the last two blue. If both digits for each component are the same, a three-digit hexadecimal value can be used, as in the fifth property color. Black is represented by #000000 or, in short, #000, and white by #ffffff or #fff. If you prefer the function rgb, use rgb(0, 0, 0) for black and rgb(255, 255, 255) or rgb(100%, 100%, 100%) for white.
We have already discussed the advantages of color names. RGB values also have benefits. Looking at a color value, or even more easily comparing two values, usually makes it immediately clear how to change a color to achieve the desired effect. For example, if you define a color as #ff7f50 and decide it needs more blue, simply increase the last two digits, for example to #ff7f70.
Specifying mixture proportions numerically provides far more shades. Each primary color has 256 levels, allowing over 16.7 million combinations of red, green and blue. RGB values also offer greater precision: if a text color is not quite right, you can change it gradually by increasing or decreasing red, green or blue in small steps.
This flexibility and control over colors is precisely why professional web design consistently uses RGB values. Graphics programs let you find a color’s hexadecimal code and use it in CSS files. But for developers like me, who do not need the tiniest color nuances, the 140 named colors are quite sufficient. Why use a sledgehammer to crack a nut?
Copyright © 2006 of the German translation, dpunkt.verlag GmbH
You may print the online version for your private use.
Otherwise, this chapter from “CSS − Anspruchsvolle Websites mit Cascading Stylesheets” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
dpunkt.verlag GmbH, Ringstraße 19, 69115 Heidelberg
back to top