Who said that you can’t teach an old rocker new tricks? At An Event Apart Chicago, it was Dan Cederholm‘s latest talk example, Color Transparency With RGBa that made me sit up and take notes.
With only one month to go, places on our Visual Web Design Master Class in London on 1st December are selling fast. Don't miss out, book your place.
At An Event Apart Chicago in October I was lucky to catch Dan Cederholm's latest talk, Bulletproof A-Z (or A-U, as there are not too many bulletproof examples for letters V to Z). Dan had been parachuted onto the stage to stand in for Derek Powazek and Dan proved again what an amazingly clever and insightful ace face is is. For me, Dan's hour on stage was the most entertaining and educating session of the show in Chicago.
Dan's example for the letter C, Color Transparency With RGBa was the one that made me sit up and take notes. I've been aware of the principles of RGBa for some time, but like a lot of people I imagine, I've never really put it through its paces, largely because of its lack of implementation in some popular (cough) browsers such as Internet Explorer and Opera. But as I've been working on several designs lately where RGBa could be put to good use, I decided to explore how using it could make my life easier and my CSS more efficient and maybe save my visitors a few HTTP requests along the way.
Run a Google search for RGBa and you'll find that it stands for Red, Green, Blue, Alpha; a colour space or model with a fourth channel, an alpha channel, which describes transparency. CSS3 adds this transparency information to other CSS color properties, enabling you to not only specify colours in RGB values but (most interestingly) the alpha-transparency value of that colour.
#facebox { background : rgba(255, 255, 255, .9);
Where the first three values (255) are R.G.B. colour values and the fourth value (.9) is the level of transparency from 0 (fully transparent) to 1 (fully solid).
Most progressive browsers have already implemented CSS opacity and you might already be using it. But it's important to know that RGBa transparency and opacity are not the same thing. So how do they differ and why do these differences matter?
As CSS3.info explained so well, way back in June 2006, when you apply an opacity value to an element, say { opacity : .9; }, you set the opacity for that element and all of its children. Specifically, the opacity property is inherited by children of that element. So if I simply apply opacity to a division, the text and images contained in that div will inherit the opacity property and they will also be slightly transparent. Not in the designs that I'm working on, that's not the effect that I'm looking to achieve.
RGBa on the other hand sets colour transparency only for the element that I am declaring values for and the transparency is not inherited by child elements. Very cool and allows for some great creative possibilities.
Let's take a look at my example file from a design that I'm currently working on. This site is not yet live and its design is not a template, so I'd appreciate it if you don't reuse it elsewhere. That is, unless you want me to come round to your house with my attack dog, Palin.
I've chosen to use RGBa transparency subtly in three areas of this design:
I designed the main navigation rail to have two distinct areas; one on the left for general information about this holiday park and the other, on the right, for contact and location information. I differentiated the list items on the right by giving them a lighter background. In the past I would have achieved this same result by either giving these list items a different background color value or perhaps by applying a semi-transparent white PNG background image.
But what if half way through my site build, the client asks for a different shade of blue, or each section of the site has to have a different navigation background colour? My CSS would then need to contain a separate set of colour values for each alternative unordered list, plus an alternative colour for each of the right hand areas; making my CSS more complicated than I would like.
RGBa simplifies my CSS by layering a semi-transparent white list item over the navigation's unordered list and allows whatever background colours I chosen for that list to show through.
#nav-main { background-color : #415968; }
li#nav-contact, li#nav-location { background : rgba(255, 255, 255, .25); }
li#nav-contact:hover,
li#nav-location:hover { background : rgba(255, 255, 255, .5); }
As well as the background colour, I also use RGBa to make the navigation links themselves semi-transparent, returning them to fully solid on :hover.
#nav-main a { color : rgba(255, 255, 255, .75); }
#nav-main a:hover { color : rgba(255, 255, 255, 1); }
By using RGBa I either simplify my CSS across right the entire site or (if I'd chosen to employ alpha-transpent PNG images instead, a few HTTP requests and a few bytes of data.) The caption overlaid onto my jQuery slideshow is achieved in exactly the same way, this time with black RGBa colour values, to allow the changing images to show through behind the caption. Again simplifying my CSS and perhaps eliminates one HTTP request.
#interactive .one p { background : rgba(0, 0, 0, .5); }
But it's when using RGBa on my jQuery Facebox popup map that it really shows its creative possibilities. Here a solid white background made the page feel a little flat, whereas a semi-transparent background helps make the page feel a lot more three dimensional.
#facebox { background : rgba(255, 255, 255, .9);
border : 10px solid #ede4c3; }
So far I have used RGBa on link colour values and on backgrounds. Take a closer look at the design that I'm working on and you'll notice that the Facebox's border is still a solid colour. That is because RGBa has only been applied to the box's background. But when I apply RGBa values to the border as well, and you'll notice that elements of the page now show through to border, completing the result that I am trying to achieve.
#facebox { border : 10px solid rgba(237, 228, 195, .5); }
It's the question that I bet many people might be thinking. What about Internet Explorer or Opera that are so far non-supportive of RGBa? How would handle their lack of support? For Internet Explorer, my answer is simple; I will either supply those browsers with either solid colours or, in the case of Internet Explorer 7 I would supply an alpha-transparent PNG image, all through conditional comments.
But what about Opera. Take a look at my example in that browser and you'll see the impact of their (so far) non-support for RGBa. If your visitor demographic includes people who use Opera (and you can find them if you listen for the sound of jangling tambourines) you'll need to work harder as, of course, there are no conditionals to target the Opera browser. If and when I choose to target Opera, I use either Conditional CSS (a PHP server-side solution) or Raphael Lima's 1kb JavaScript solution that will both allow me to send specific CSS rules to the Opera browser.
So is RGBa ready to rock as a CSS property that you can use in everyday work? I think so. Rock on.
Update: Both HSLA and RGBA are implemented in the next version of Opera’s rendering engine, Core-2.2, and can be previewed in the Opera ACID3 build on Opera Labs for Windows and Linux, but sadly and strangely not for OSX.
25th Oct 2008Been using rgba in a few recent projects, and was delighted to hear dave storey tell me that presro2.2 would support it. Makes a world of difference.
25th Oct 2008Thanks so much for the screen-cast I found it useful. This is the first example I’ve seen of Dreamweaver CS4 in the wild. You should consider doing a screencast on it and why you choose using it over something like BBEdit or Textmate.
25th Oct 2008What about FF2? Do you use conditional css (php) to send the same fixes to FF2 that Opera gets?
26th Oct 2008ff2 is obsolete and a very rare edge case, personally I ignore it, but using the javascript file Andy links to would allow you to feed ff2 it’s own overide.
Unlike ie, the firefox and opera userbase tend to upgrade very quickly. Not least because updates are pushed out to users.
26th Oct 2008Thanks for the post. I have been trying to swing my team into using RGBa. Hard to argue when sites like this are recommending it.
26th Oct 2008I love screen-casts, hope you plan to do some more and indeed this one was really good. Being able to see what is being explained is awesome (though for me, and it could be beer goggles the code wasen’t really legible - though of course you can read it perfectly in the supporting transcript).
I found this article a few weeks back about additional ways to serve CSS to the “good” browsers (http://www.nealgrosskopf.com/tech/thread.asp?pid=20) - might be worth a look regardless of the fact that these hacks seem pretty gastly.
26th Oct 2008Thanks for this tutorial.
One quick point though, as one point you refer to 0.9 as 90% transparency… surely that’s 90% opacity, i.e. 100% would be fully opaque, not fully transparent.
Nice tambourine quote for Opera users. :)
26th Oct 2008Excellent screencast, I hope you make more! Good examples and crystal clear.
I noticed a little problem with rgba in your navigation example, where the alpha value changes on hover. From your example it looks like you have to declare the rgb in each state, rather than being able to set the alpha channel only. Therefore, any time you need to change the rgb you have to update both places.
Of course that is a trivial issue :)
26th Oct 2008@Matt Wilcox: It’s nice to know that at View you’re finally getting to work on cool stuff.
@Cameron Westland: Actually, I use CS3 (I changed the default icons). I chose Dreamweaver for the screencast as it’s what many people will be familiar with, but personally I use either SKedit or simple TextEdit on OSX.
@will haven: I would use Conditional CSS to target Firefox <3 if I had to. But I have my clients sign a contract that states that I only target current browser versions, so that rarely applies. (@matt wilcox is right about people upgrading)
@Ross Bruniges: I plan to do all my CSS and design posts/tutorials as screencasts from now on.
@Francis Booth: You’re right. It’s tricky to get the terminology right when you’re ad libbing.
@Mike Robinson: This was my first real experiment with RGBa so I’m still on that learning curve too.
26th Oct 2008You can easily target Opera and all other browsers that do not support RGBa (except IE, that will still be in Conditional Comments) with this trick:
selector { background-color: #FFF; background-color: rgba(255, 255, 255, 0.8); }When the browser sees the second rule, it says: “WTF?!”. But since you have already declared a background, the browser keeps it that way. All browsers that support RGBa, however, use the RGBA rule because it is the last declared rule for the background. It simply overwrites the previous rule (selectors with the same specificity ;)
That’s also why you actually can use the
//comment to comment out a line of CSS, since a browser simply ignores it.
27th Oct 2008Great stuff, Andy. Nice insights, clever application of the principles and beautiful presentation.
This solves a previously curly problem for me with a current project. I love it when that happens.
I’m look forward to more screencasts like this.
Thanks, Andy
27th Oct 2008Very good presentation! Do you have this screencast available in a slightly higher resolution? The code view is not legible when the video is scaled down so far. Not to worry though as you provided text examples in your post.
27th Oct 2008Very useful, thanks Andy. Think that I’ll start experimenting with this in some of my personal sites!
27th Oct 2008@Arjan Eising: Thanks for that. I’ll update my example files and post today.
@Jim Jeffers: I’m still experimenting with ScreenFlow and the best ways to make these screencasts more readable when I show code.
@Clinton Montague: Be brave, use it on commercial projects too.
27th Oct 2008Great stuff, Andy, thanks for sharing! Will try and use this technique right away :)
@Arjan Eising: nice! That’s one (another) case when breaking CSS validation actually is the right thing to do, accessibility wise: we can deliberately break CSS validation, if we know what we’re doing.
27th Oct 2008@Andrea Gandino: Arjan’s suggestion isn’t invalid CSS.
27th Oct 2008@Andy Clarke: Mmm, actually I’ve tried to validate it, and it throws me an error… :/ What am I doing wrong?
Still, everything is fine, validation wise, if I use
backgroundinstead ofbackground-color.The code works in both ways, though.
27th Oct 2008@Andrea Gandino: Both the HTML and CSS validators at W3.org have their oddities. Sometimes they say something is valid (while it isn’t), and vice versa. Best thing is to focus on having readable and editable code for others (keep it simple), and to have a nice looking page in all common browsers.
27th Oct 2008Oh sure. Nearly 1am my (Australian) time, and NOW the conversation gets interesting.
Har-and, frankly-rumph.
27th Oct 2008Doesn’t it make some kind of sense if you accept that the alpha part isn’t a colour command but a conditional command on how the colours are displayed?
In which case rgba could be used with background but not background-color.
27th Oct 2008@Ricky the ‘a’ in RGBA is a part of the colour model. CSS offers four colour models: keyword, hex, RGBA, and HSL. All CSS colour attributes (with the exception of keyword) require you to prepend the colour code with the model you’re using:
#333333
rgba(0,0,0,.5)
hsl(50,50,50)You can use any of those models anywhere you want to specify a colour, whether in a regular rule ( background-colour ) or a shorthand rule ( background )
27th Oct 2008Without getting too semantic, does that mean that RGB - used in the context of eg,
selector { background-color: rgb(255, 255, 255); }
is an abbreviated or incomplete version of RGBA?
Or has RGB been superseded by RGBA?
27th Oct 2008@Ricky
Both RGB and RGBA are supported as part of CSS (one is a CSS2 spec, the other a CSS3 spec) and as such you can use both; but for all practical purposes RGBA would seem to supersede RGB on the basis that RGBA has all of the functionality of RGB - but with the addition of an alpha value.
http://www.w3.org/TR/css3-color/#rgb-color
“The RGB color model is extended in this specification to include ‘alpha’ to allow specification of the opacity of a color.”
The real thing to be bothered about is whether browsers support whichever colour model you feel you want to work with, which is what Andy’s post is about. RGBA won’t work in IE yet, but it works fine in every other browser (including the next version of Opera). You can cook up some really nice visuals by mixing RGBA, rounded corners, and transforms (three of my most used and loved ‘new’ CSS features)
28th Oct 2008How do I watch this at fullscreen without losing quality?
28th Oct 2008slightly off the point but what a great name for an attach dog
29th Oct 2008@Matt
Thanks for that. A key point for me is that an alpha value is allowed rather than required or expected. All good.
Fully take your point, and Andy’s, about browser support. There are some developments that present such a useful advance that they become worth adopting for supported browsers and devising a hack or trick to emulate that effect, or compensate for the lack of it, in IE.
It’s not always easy to see which developments are worth doing that for, but after a bit of mucking around in the last few days, I’d agree that RGBA is one of those.
It certainly makes some lovely effects available.
29th Oct 2008Nick Toye: I’m still playing with the best way to serve these screencasts. Please bear with me.
Ricky Onsman: I think Matt cleared that up nicely. Don’t you?
dccrowley: Palin says “woof, grrrr”