opacity (or, a paucity of transparency)

December 13, 2009 Alex Chaffee

The rules for how to make parts of your HTML page translucent are kind of hard to understand — in other words, the opacity rules are pretty opaque. (Anyone who can make that into a good pun, let me know and I’ll change the title of this article accordingly.) The following represents the results of a couple of days of empirical research and as such may be incomplete or inadequate, but here goes.

In the brave new HTML5 world, with all the CSS gizmos supported by Safari and Chrome and Firefox, there are now three ways to make things translucent. And none of them works quite the way I naïvely expected.

One. Use the “opacity” CSS attribute. This attribute works pretty well… at first. It applies to an element and all its children, but according to the spec it’s meant to act as an upper bound on the opacity of all its children, and while it can technically be overridden, the overridden value is applied as a multiplier to the previous value, not as a whole separate value. So if you want some fully opaque children inside a translucent container, you can’t get there from here. The children are always going to be at least as transparent as the parent — in other words, they can’t transcend their parent’s transparency.

This is spelled out in detail in https://developer.mozilla.org/En/Useful_CSS_tips/Color_and_Background and as a solution they propose either pulling the child out of the normal hierarchy (ugh — that means you lose all the other CSS inherited styles and positioning), or …

Two. Make an alpha channel PNG and use it as the parent’s background, probably with background-repeat:repeat. This is adequate, except that there’s now another, cleaner way…

Three. For the parent, use background-color: rgba(255, 255, 255, 0.5) (where ‘0.5’ is the opacity and ‘255,255,255’ is the decimal RGB value) — that will work the same as an alpha PNG but without needing to go round-trip to Photoshop every time you want to change the color or level. Much better.

I have no idea what the level of support for rgba background colors is, but it seems to work in the latest Safari and Firefox so I’m happy.

About the Author


SF.TUG, December 2009 edition
SF.TUG, December 2009 edition

Yesterday we held a TUG over lunchtime at our San Francisco offices. The subject was "Tracker 101". Thank...

annotate 2.4.0 released
annotate 2.4.0 released

Remember the annotate_models rake task? Dave Thomas wrote it many years ago and it corrects one of the flaw...