Our Mindfly Blog

Website Design and Development

Random creative design element

Fade to Black (CSS rgba colors and opacity)

by Kyle Weems 17.June 2008 08:14

I'm going to be honest here. Despite this post's title, I'm not a huge Metallica fan. Mind you, I respect the fact that they didn't get obsessed with codpieces in the 80's like everyone else with long hair and guitars did. I'm just going to go on record here: some fashions are worth resurrecting every now and again. Codpieces are not on that list, along with capris pants.

Now that I've dictated fashion, let's get down to business. Mozilla is releasing version 3 of their flagship product: Firefox. If you don't know what that is and you're reading this blog, then go download it, and throw away the little blue "E" icon that's sitting on your desktop. Future generations of web developers will thank you for contributing to a standards-compliant world. Coming out a couple weeks after Opera 9.5 and within the same general timeframe as Safari's last major upgrade, it is helping the implementation of web standards in browsers move forward.

Unfortunately, Internet Explorer 8 is still an indeterminate time away from release, with its beta 2 not scheduled until sometime in August. So don't throw away your conditional comments yet, boys and girls.

That said, this brings up some interesting CSS3 features that I think web designers will be using a great deal. Among them is rgba colors.

No, rgba is not an abbreviation of rutabaga. Rather, it refers to setting an element's color via red, green, blue, and alpha values. The first three values help determine the element's color, obviously (with each color getting a value of 0 to 255). The last one, alpha, refers to how transparent the element is, and can range from 0 (completely invisible) to 1 (completely opaque). 

The format for this would be:

e { color: rgba(255, 0, 0, 1); } 

That would result in the text of element e being red. What's nice about using rgba colors is that color and alpha of an object can be set seperately for the text and the background. This is great for having a faded/semi-transparent element with crisply visible text over it. Here's an example page showing this property at work... but only in Firefox and Safari. Sadly, neither Internet Explorer nor Opera have this property yet. (I parody Opera's deficiency in this regard in a CSSquirrel comic here).

Before the wailing and gnashing of teeth set in, though, let me tell you that not all is lost. (Quick aside: what is gnashing of teeth? Is that grinding your teeth? Or is it finding some teeth to chew on? Because that sounds like a bad way to handle disappointment.) There's another CSS property out there that can be used to determine opacity. The W3C decided to get really wild and crazy, and named it opacity. Here's how it looks:

e { opacity: 1; }

The above style would make element e completely opaque (which is default). The property takes a value between 0 and 1, with 0 being completely transparent. Here's our example page modified to make use of opacity. Sweet, right?

Well... maybe like Nutrasweet. Remember Nutrasweet? It was like the Splenda of the eighties. I'm shocked that it's still around. I think we're hitting a point where we should just realize that if it's a powdery additive that isn't bad for you, it's going to have a nasty aftertaste. It also may causes brain cancer. Which isn't sweet at all.

Ok, back on track. There's three major issues with using opacity that need to be mentioned. 1. It's not supported by Internet Explorer. 2. Both the foreground and background of an element is affected by the property, so the text will be faded too. And lastly, 3. It's automatically inherited by all of an element's children, and this can't be overwritten by setting a different opacity to them.

This really limits the application of opacity. #2 is less problematic if your design has faded text, but it's a real issue if that's not what you want. #3 is a doozy, and we'll get to that in a bit. #1 is actually the easiest to solve, despite IE's well deserved reputation as the rebellious punk of the browser world.

Microsoft, despite it's often questionable support of existing standards, decided to make some of it's own proprietary features for it's browser. I know, this isn't shocking in any way shape or form. What is helpful in this particular case is that we can make use of one of these properties to help the limping blue E keep up with the crowd. In particular, we're making use of their filter property. It can be seen in this example:

e { filter: alpha(opacity=50); }

This example results in element e having an opacity of 50%, mimicking what you'd get on a good browser with the style opacity: 0.5. This particular filter takes any value between 0 (completely transparent) and 100 (completely opaque). Like the opacity property, it affects both the foreground and background of the element. Of course this filter only works for IE, so to make opacity work for all browsers you'll want to use both the opacity property and the filter: alpha property in any CSS (I prefer to put the MS properties in an IE specific stylesheet, but you can keep them together if that's how you like to style).Here's our example page with it now including the filter. You'll find that it works now in all the major browsers.

Well, mostly. Once again, we've still got an issue with the text being transparent. Initially when testing this, I'd hoped to offset this by putting the text in spans, and then giving those spans opacity: 1. It seemed like an intuitive, reasonable solution. However, apparently the W3C's specification isn't so intuitive. As it turns out, once you've set an element's opacity, all of its children inherit the opacity. And it cannot be overridden. Who thought this was a good idea? Is there a browser limitation that I'm not noticing? I'd hardly think so, as the rgba colors don't have this problem.

Oi.

Ultimately, there's only one workaround to this issue. You need to keep whatever text that you want opaque in a seperate element that isn't a child of whatever element has the opacity set. This means we'll see extra markup for presentation (which is a bad thing, but often unavoidable in our non-perfect, IE-filled world), and it can also be challenging on some designs due to the need to use absolute positioning or other related tricks to get elements to stack on top of each other.

Unwieldy or not, though, here is an example of non-transparent text over semi-transparent backgrounds that work in all browsers. As you can see by looking at it's page source, it isn't the prettiest example of clean markup and minimal CSS. In fact, I question whether it'd be worth taking this approach when PNGs might be simpler. Then, with PNGs you introduce a host of IE6 issues (which are fixable by Dean Edward's IE7 script, although that can introduce a number of image positioning issues and some strange anchor bugs when PNGs are involved).

So if I were you, I'd get a greeting card with a sad puppy on it, write a tearful demand for rbga color support in it, and then mail it to either Dean Hachamovitch (of the IE Dev Team) or Håkon Wium Lie (of Opera). If anything, the sheer magnitude of dog tears will melt their frozen hearts so that they'll have pity on us.

Oh, and seriously, download Firefox 3 sometime today when the release finally goes out. It's fast, feature-filled, and just generally rocks.

Comments

Troy

Troy said on June 22, 2008 (12:52)...

I am very certain that,
filter: alpha(Opacity="num"Wink is older than you are. Or at least older than your first internet surf (November of 1996).

They've invented the opacity property along with an army of other features you cannot dream of already back then, which are still not supported nor even fore seen by W3C. Opacity in explorer can even have a shape or directional gradient since you've mentioned it from back then. That would be a handy feature, for replacing the gradient (image) backgrounds with a simple css declaration in all of them browsers wouldn't it? Meanwhile being able to programatically change its color and/or rotate its gradient direction over time, wouldn't it?

And guess what, - your example at:
www.mindfly.com/.../opacity.html
doesn't work on my latest version of firefox (2.0.0.14), at all! (Neither does IE 6 for that matter) but you shouldn't lie for these matters. So it is the "crowd" trying to keep up with way to old IE 4.01 still not surpased by new ones - not the IE!

But false propaganda is as was said once by Hitlers medial cheef: more than a killing weapon. And it is killing Internet evolution day by day.


Rusty

Rusty said on June 23, 2008 (08:45)...

You go girl!


Kyle

Kyle said on June 24, 2008 (08:32)...

Troy,

Thanks for the feedback.

Here's some clarifications and rebuttals:

1. If you'd read the entire post, you'd have noted that 2.0.0.14 is not in fact the newest version of Firefox. As of June 17th (the day the post was written, and five days prior to your comment) it is now 3.0. And Firefox 3.0 in fact does render the example correctly.

2. I was in fact making use of the Web a couple of years before November 1996, and even screwing around with HTML. I'll admit both the surfing and the coding were pretty simple things, but as I was in a county school next to a cow field, you'll have to forgive me for that.

3. The ancient lineage of MS filters is ultimately useless for web developers, since any website that takes advantage of them is then creating effects that won't display in the other major browsers. The entire concept of web development these days is web STANDARDS, which encourages guaranteeing that a web design works on the largest number of browsers. Perhaps you've heard of those. http://webstandardsgroup.org/standards/ holds the basics if you need a refresher.

4. CSS isn't about programatically changing a web page. It's not a Turing complete language, and was never meant to be. If you want on-the-fly changes, use Javascript or another actual programming language (CSS is a designer's tool first and foremost, not a developer's programming tool).

5. Example #1, as you pointed out, doesn't render in Firefox 2.0 (or for that matter, IE6). It never claimed to. It was showcasing a property that exists in FF 3 and Safari. Fortunately Firefox users have a much faster adoption rate for new versions, unlike the generation of IE6 users that won't/can't upgrade to IE7. It's worth noting that Example 4 should work fine in FF4, though, thanks to that version's use of the opacity property.

6. I agree that a site making use of these effects would need a custom stylesheet for IE6, due to that browser's dodderingly ancient rendering techniques. My examples were intended for modern versions of the major browsers, and work as expected on that.

7. Thanks for comparing me to a member of Hitler's staff. That's really sweet of you, but I want you to consider the following points: (1) If you'd done your homework before posting, you'd note that my examples all work as claimed. (2) Adhering to web standards and pushing the CSS3 envelope (while finding a way to keep up with out-of-date browsers, as my examples strive) is what it means to embrace Internet evolution, rather than your apparent inability to put aside MS's filter properties, which are incompatible with any other browser, and always will be.


Add comment



(Will show your Gravatar icon)  









Live preview

said on January 6, 2009 (19:30)...


 

Powered by BlogEngine.NET 1.2.0.0. Original Design by Heather Alvis.
Sign in

Bellingham, Washington
Copyright © 2007 Mindfly Inc. All Rights Reserved.