RGBA Color

You're familiar with hexadecimal colors like #FF0066. And in Chapter 6 (RGB), you read about RGB color notation: rgb(25, 255, 0). for example. CSS 3 adds another way of specifying color: RGBA, which stands for Red, Green, Blue, Alpha. Basically, it's RGB color with the addition of alpha transparency. Alpha works the same way as opacity, as described in the previous section, in that you specify a value from 0 to 1 to indicate how much you can see through the color. A value of 0 means the color's invisible, while 1 means it's a solid color—you can't see through it.

Say you want to add a background color to a div with a class of caption, but you want that color to be see-through enough so the content underneath the div is readable (see Figure 16-6). You could then create a style like this:

.caption { background-color: rgba(95,156,140,.75); }

In this case, the background color is teal (95, 156, 140) with 75 percent opacity (.75). You can use RGBA wherever you'd normally use a color value in CSS—for a background color, text color, border color, and so on.

The downside of RGBA color is that Firefox 2, Opera 9, and every version of Internet Explorer don't understand RGBA colors. If those browsers encounter this style, they simply ignore it and leave the background color empty. You have a couple of approaches for dealing with these browsers: First, don't worry about it and let those browsers ignore the color. That's the easiest tactic, but not necessarily the best. For example, the text might not be legible if there's no background color at all (Figure 16-6).

The new CSS 3 RGBA color gives you an easy way to add partially transparent backgrounds to page elements, like the paragraph pictured here.

Figure 16-6. The new CSS 3 RGBA color gives you an easy way to add partially transparent backgrounds to page elements, like the paragraph pictured here.

Tip

Any image-editing program, like Photoshop or Fireworks, can give you an RGB color value as well as a hexadecimal color. If you already know a hex color value, and you want to convert it to RGB so you can use RGBA colors, visit this web page for help: www.javascripter.net/faq/hextorgb.htm.

Another approach is to give the background a solid color using either the regular RGB color or a hexadecimal color. Doing so takes two passes. First, to deal with non-IE web browsers that don't understand RGBA, you must edit the rgba style:

.caption {
  background-color: rgb(95,156,140); /* Opera and others */
  background-color: rgba(95,156,140,.75);
}

In this case, the first background-color declaration uses RGB color, which all browsers understand. When Opera, Internet Explorer 8, or Firefox 2 encounters the second declaration, the browser ignores it, since it doesn't know what RGBA color is. This style works for Opera 9 and Internet Explorer 8, but IE 6 and 7 get confused and don't display any background color at all.

Accordingly, you need to add another style sheet for IE using conditional comments, as described on Isolate CSS for IE with Conditional Comments. Here's an example of how to get the previous code to work with IE as well:

<!--[if IE]>
<style type="text/css">
.caption {
  background-color: rgb(95,156,140);
}
</style>
<![endif]-->

There's another way to get Internet Explorer to join the RGBA party. As with opacity, IE provides a filter property that you can use to achieve the same effect as RGBA color. It's a bit of a handful to type, and you need to add a one other CSS property to make it work. To get a taste of how the finished product looks, here's you what you'd add to your page to get the same effect as described in the previous section on RGBA color:

<!--[if IE]>
<style type="text/css">
.caption {
  background-color: transparent;
  filter:progid:DXImageTransform.Microsoft.gradient(
  startColorstr=#BF5F9C8C, endColorstr=#BF5F9C8C);
  zoom: 1;
}
</style>
<![endif]-->

This chunk of code does the same thing as this:

.caption { background-color: rgba(95,156,140,.75); }

Here's how it works: