![]() ![]() |
Coloring By NumbersUnless you've been living under a rock, or are still cruising the Web with a Lynx browser, you're probably familiar with the use of colors in Web page design. HTML allows us to specify colors for text, links, visited links, backgrounds, etc., with a fair amount of ease. You can do all these things, and much more, under CSS1. In fact, with CSS1 you can define a color value for any element, class, pseudo-class or -element, and so on. However, we aren't going to talk about how to set the colors today. Instead, we're going to discuss the ways in which the colors can be specified. There are more of them than you might think.
Coloring Basics
Actually, I am going to talk briefly about one property: color: <color> That isn't an actual value there. It's a placeholder for a whole range of possible values, which are the subject of today's discussion. Playing the PercentagesThe easiest CSS1 color system to understand uses percentage values. In this scheme, the RGB levels are specified by writing out three percentages, like this: <P style="color: rgb(50%,0%,50%);"> This is a shade of purple.</P> This is a shade of purple.
Let's pull out the actual rule and examine it. First, the set of RGB values is preceded by the keyword <P style="color: rgb(0%,50%,50%);"> This is not a shade of purple.</P> This is not a shade of purple. One theoretical advantage of percentages is that you can, if you so desire, declare percentages using real numbers (as opposed to integers, which we'll get to in a moment). In other words, the following declaration is perfectly legal: <P style="color: rgb(50.5%, 0.0%, 49.7%);"> This should be a shade of purple.</P> This should be a shade of purple. If, however, your browser shows a color which is significantly different from the one shown in the previous 'purple' example, then it probably isn't interpreting real numbers too well. This is one of those areas where authors should tread with caution. A Scale of a Different RangeThe next way to specify colors is very similar to using percentages. This time, however, instead of values having the range 0.0% - 100.0%, they have the range 0 - 255, like this: <P style="color: rgb(128,0,128);"> This is a shade of purple.</P> This is a shade of purple. The number 255, as you may know, is one of those mystical powers-of-two values around which so much of computer programming revolves. We'll see how it came into color values in the next section, but for now, let's concentrate on how to use it.
As you can see above, it's another 192 / 255 = .75294 (approx. 75%)
Thus, This Should Seem Familiar
We turn now to hexadecimal notation, which you've probably seen or used in countless Web pages, because it's been used for at least a couple of years now. In this color scheme, colors are represented using three hexadecimal values strung together into one string, something like this: If you're familiar with hexadecimal notation, this part will be a bit of a bore. For those still in the dark, here's how it works. Every decimal number (that is, every number in the base-ten counting system we all use) has an equivalent value in the hexadecimal notation, which is base-sixteen. Counting up from zero goes like this: 00,01,02,03,04,05,06,07,08,09,0A,0B,0C,0D,0E,0F, 10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F, 20,21,22...
In <P style="color: #800080;"> This is a shade of purple.</P> This is a shade of purple.
From this, you can infer that As for converting from hexadecimal to integers (and from there to percentages), my recommendation is that you use a calculator which convert between decimal and hexadecimal numbers, or a obtain program for your computer which does the same thing. Of course, you might also have a program which lets you pick colors from a palette and then gives you the appropriate hexadecimal value. In that case, just use the program's output and don't worry about converting from one system to another.
There is an even easier way to use hexadecimal notation. It's sort of like a shorthand, although you can only use it in certain circumstances. This system is written
What you have to remember is that the three-digit form is converted to the six digit form by replicating digits, not filling in zeros. Therefore, the color Just Name the Color!There is one last way to declare colors. It's even simpler than the last one. In this system, you just fill in the name of the color, like this: <P style="color: purple;"> This is a shade of purple.</P> This is a shade of purple. Of course, this is a lot easier to do, because color names are easier to remember than percentages or hexadecimal values or what have you. However, there is a major drawback, which is that there are only sixteen defined color names. They are, in alphabetical order:
That's it. Some browsers may recognize more values than just these sixteen, but they aren't part of the specification. Conclusion
We've seen all the ways you can define a color in CSS1. For those who are interested, I've drawn up a list of color equivalencies, based on the sixteen named colors. A little bonus for those of you who stuck with me to the end: the three-digit hex system is a convenient way to declare "Web-safe" colors for page elements. All Web-safe colors are based on the hex-pairs 00, 33, 66, 99, CC, and FF. An example is Thanks for sticking with me through all the numbers, and come back for the next "Sense of Style!"
Color Equivalencies Table |
||||
|
Go Back |