Highlighting thumbnails using transparency
I recently figured out a nice and simple technique to create a nifty highlighting effect on thumbnails for your image gallery. This article will provide you with all the necessary information on how to successfully implement it across all major browsers, including Internet Explorer 6.
The goal
As said, the idea is to create a really simple thumbnail gallery with a nifty highlighting effect. We will use only html and css (no javascript) and make sure that it works in all major browsers.
The HTML
The thumbnail gallery will be built using a list for semantic reasons. On top of that, we wrap the list with a div that will act as a container. If you wonder why we add the extra div-wrapper instead of just adding the thumbnail class to the ul, check out this article by Onderhond. I don't think the link and image tags need more explanation here. We can of course add a title attribute to our a-elements for improved accessibility. Also, add as many list items as you wish.
The CSS
.thumbnails ul li {display:block; float:left; padding:0px 10px 10px 0px;}
.thumbnails ul li a {background:#FFFFFF; display:block;}
.thumbnails ul li a img {opacity:0.9;}
.thumbnails ul li a:hover img {opacity:1;}
We will quickly explain the css code above. We float the list items to the left and give them a bit of padding. Next, we set the display property of our links to block. Our gallery will now become a nice little grid, assuming that all thumbnails have the same dimensions.
To achieve the highlighting effect, we will give our image a solid white background and a little bit of transparency using the widely implemented css3 opacity property. On the hover pseudo-class, we will reset the opacity to its default value to achieve the nifty hover-effect.
The compatibility
Most major browsers already support the opacity property. Of course, the ugly duckling of the browser market (yes, I mean Internet Explorer 6) doesn't have support for opacity. Luckily, we can use an IE-only filter to set the opacity. I suggest keeping the css below in a separate stylesheet for IE6 and include it using conditional comments.
.thumbnails ul li {display:inline;}
.thumbnails ul li a img {filter:alpha(opacity=90);}
.thumbnails ul li a:hover img {filter:alpha(opacity=100);}
The result
When all the code is in place, the result should be somewhat like this example page. Have fun with your new nifty hover effect!
Where do we go now?
You can browse through the recent articles or go to the archive for older items.

There are 7 comments for this article
Filip Van Tendeloo on Dec 4, 2008
Jonas,
In this article you talk about adding a solid white background to the image. By doing this you force the default (no hover) image to be lighter than the original and thus causing the hover to be darker.
I don't believe this to be correct behaviour. A hover should be lighter than the original.
Did a little test on your example and it looks great with background-color #000 and opacity set at about 0.8
Jonas on Dec 4, 2008
I see your point, but I think it's a matter of visual preference instead of a matter of correct behavior. More so, I think that the visual preference is strongly influenced by the surrounding design.
Anyway, making the original image a bit darker works just as well. The reason why I prefer the white-background approach is that I think it gives the impression of the image (in its original state) being a bit blurry. This way, when you hover the thumbnail, it sort of "sharpens up", making it stand out of the other images.
As said, I think both approaches work equally well and therefore could both be tested against your page's design. You can always pick the one that looks best.
Jonas on Dec 5, 2008
Filip,
I added your method to the example page.
Mumbai Web Designer on Dec 16, 2008
Hey great stuff...
W3schools has always had this in their tutorials here http://www.w3schools.com/css/css_image_transparency.asp also they show out there how you can use Javascript to have the swap thing rather than CSS hover... Anyways probably when they wrote this tutorial Javascript was more reliable than CSS to work across browsers.
Romily on Dec 20, 2008
I'm not sure why you would want to have a slightly dimmed original presentation i.e. an image opacity of 90% - why make getting a great looking image harder? I anyway had trouble detecting the change in the image on rollover in your example.
I've settled on adding in my main style sheet and via conditional comments a single style, for the hover state, in which I set the opacity to 50%. Why this much? Because I want to make sure, as far as it's possible to do so, that visitors using "nothing special" displays (skewed colour balance, lower quality screen, with a little dust attached maybe...) and perhaps with less-than-perfect vision are able to easily detect the change in the image. Better usability, in my book.
Thanks for posting this tutorial, which works exactly as advertised on the opacity front in my testing (on Win XP: IE7, IE6, FF3, Chrome 1.0, O10. On OSX10.3: S1.3. Validates for CSS level 3.).
cortney on Dec 21, 2009
sometimes people don't have a choice. often times, clients prefer it to be dimmed whether its easier on the coder or on the viewers eyes.
veronica on Dec 21, 2009
sometimes people don't have a choice?!?!? tell the clients that prefer it dimmed its a poor judgement call