Tony's Wibbles

GeekySimple CSS based image rollover effects

As usual, I wanted to do something I felt should be simple in HTML, and I couldn't find a good answer. In this case, I wanted to create a rollover effect on an image, without having to revert to using JavaScript. All the solutions I found used background images and block elements. These just never quite worked for me as I wanted the images to be inline. So I came up with my own solution.

My solution is based on providing both images in the HTML then selectively hiding them based on the hover status. Here is an example HTML...

 
<a href="#" class="Rollover"> 
  <img src="Normal.gif" class="Out"  /> 
  <img src="Hover.gif" class="Over"  /> 
  Link Text 
</a> 

The class "Rollover" defines the scope of the rollover effect. So in this case, hovering over the link will trigger the rollover effect. Elements inside the "Rollover" marked with the class "Out" are visible while no rollover (hover) is in effect. Elements marked with the "Over" class will only be visible while rollover is in effect. For this example the Hover.gif will replace the Normal.gif when the user hovers over the link.

In order for this to work the following CSS is required...

 
.Rollover .Out 
{ 
	display:inline; 
	visibility:visible; 
} 
.Rollover .Over 
{ 
	display:none; 
	visibility:hidden; 
} 
.Rollover:hover .Out 
{ 
	display:none; 
	visibility:hidden; 
} 
.Rollover:hover .Over 
{ 
	display:inline; 
	visibility:visible; 
} 

You should also check ouy my similar article on a Simple CSS based tool tips

Comments

2008 Jun 25, 12:00AMOptimator said...
Simply Beautiful!
Now I'll try and implementing it into an <ul><li> float:left horizontal image menu with captions on top.



2008 Jun 30, 03:43AMjim said...
Absolutely brilliant, I have trying to suss this out myself. Thanks for this.



2008 Jul 02, 08:47PMFrank said...
Excellent - this is the first method I've found that works easily for a gallery where I want the same effect applied to many different images.



2008 Jul 04, 05:47PMJeff said...
great stuff



Add Comment
Go
11 Oct Published "ClockWork Website Link Verifier"
Initial Application Release
29 Sep Updated "ClockWork Script Builder .Net"
I've added an ExtJs based demo and compression support.
06 Sep Updated "ClockWork Firefox to KeePass Converter"
New version that supports direct access to Firefox's passwords, and more...
23 Aug Published "ClockWork Script Builder .Net"
Build readable scripts in .Net
07 Aug Updated "ClockWork Firefox to KeePass Converter"
New version for 2.05 and renamed to ClockWork
PicLens Logo Copyright ©2008 Anthony James McCreath. All Rights Reserved. Contact Me. Powered by GRUNT 2.1 Alpha