Image Swap Using CSS
When I was designing the new Ask-a-Pro Detailer blog for Detailed Image I figured out a way to do an image swap without using Javascript.
The basis of this image swap technique is to set a background image to be your image you want to have swapped when you hover over it. Then insert the default image which will sit on top of the background hiding the background. Create a style to change the opacity of the default image to full transparency when you hover over it, voila you have an image swap.
Requirements
CSS3 Property: Opacity
Opacity changes the transparency of an element. Most modern browsers already support this CSS3 property. Set values between 0.0 (full transparency) to 1.0 (no transparency).
CSS2 Property: Pseudo Class :hover
Pseudo Class :hover is a dynamic action that happens when you hover over an element.
Examples:

HTML
<div id=”css-image-swap-1″>
<img id=”swap-1″ src=”default-image-url” alt=”CSS image swap” />
</div>
CSS
#css-image-swap-1{
width:300px; height:150px; background:url(swap-image-url);
}
#css-image-swap-1 img:hover{
opacity:0;
}
HTML
<div id=”css-image-swap-2″></div>
CSS
#css-image-swap-2{
width:300px;
height:150px;
background:url(image-url);
}
#css-image-swap-2:hover{
opacity:.5;
}
I mentioned above that this method should work with most the modern browsers but I know it doesn’t work for IE7 and older because IE totally sucks.
I’m sure this has been done before but I’d thought I’d share this with everyone. I didn’t find this particular technique when I searched for CSS image swap but I did find others using this basic concept using CSS visibility instead of CSS opacity. Both techniques will get the job done for an image swap.
02/14/2010
Comments (3)




[...] the CSS image swap I posted about back in February you can hover over the above image to see some of the key design [...]
Pingback by New Detailed Image Homepage - Michael Li (2010) — August 6, 2010 @ 9:34 am
Hi Michael,
Your image swapping and fading look great.
I wonder if you could answer my question. I am trying to implement a slideshow with CSS. You know, a few images displaying one at a time in the same window with fading in between in a loop as long as the window is shown. It looks like combining your both features of swap and fade will do the job. The rest is to time the change of the images.
Can you do it?
Do you think I need to employ DHTML?
Keep up you great work.
Best Regards,
Ken
Comment by Ken — August 25, 2010 @ 7:05 pm
Ken sorry for the late response your comment got caught in spam.
Creating a slide show can certainly be done but you’ll have use CSS in conjunction with a language like Javascript.
I can point you towards couple sites that can help you out further.
- Dynamic Drive offers a library of original DHMTL and Javascripts http://www.dynamicdrive.com/dynamicindex14/index.html
- Tizag offers great tutorials from html, javascript and much more http://tizag.com/
Hope that helps you out. Thanks for commenting Ken!
Comment by Michael Li — August 31, 2010 @ 5:53 pm