April
3rd, 2008
Extremely efficient image rollovers using CSS sprites and NO Javascript
Recently we’ve seen the death of javascript rollovers. But rollovers aren’t such a bad thing. However, they generally required the use of multiple images and Javascript which would handle mouseover and mouseout events. I think this is why they lost their appeal. There’s got to be a better way, right?
Getting the ingredients
Using a single image and some CSS we can emulate rollovers from the good ol’ days. We’ll only need two ingredients to make this happen.
- A single sprite graphic (we’ll use one similar to the tabs above)
- 2 extremely simple CSS rules
Here’s the sprite image:
![]()
Adding some sprite
Let’s start with the sprite graphic. The one I’ll use for this example is two tabs placed on top of one another. The position of the top tab is 0,0 to 102,46. The position of the bottom tab is 0,46 to 102,92. These numbers will come in handy when we define the CSS for the sprites. The sprite definitions will look like this:
a:link.sprite, a:visited.sprite{
background:url(/images/sprite.png) no-repeat top left;
background-position:0px 0px;
width:102px;
height:26px;
padding-top:20px;
display:block;
}
a:hover.sprite{
background-position:0px -46px;
}
Basically, we’ve just defined two states for the link. the default state uses the sprite image as a background and aligns the top left corner to the anchor. We go ahead and define the width, height and padding so that the anchor will display the entire tab. Anchors are inline elements so we make them block level elements so that we can size it to the tab.
Repositioning the sprite image
The second state just re-positions the background image so that the bottom tab shows up. We do this essentially by sliding the background up 46 pixels. Now we’re ready to write the html.
<a href="#" class="sprite">Rock On</a> <a href="#" class="sprite">So Easy</a>
Here’s an example of what that looks like.
That’s the basic idea. You can add any additional CSS needed to incorporate it into your site.
As always…comments welcome.

April 12th, 2008 at 12:40 am
This is so brilliantly simple, plus there’s no extra load time for double the graphics.
Way to go man…
April 16th, 2008 at 11:47 am
Thanks for the kudos. I had originally set out to make a really simple javascript implementation which used sprites but ended up doing it this way which I was very happy to find.
April 17th, 2008 at 11:52 pm
broken image / demo..
April 18th, 2008 at 1:25 am
@Henry, Sorry about that. Result of a git merge gone bad. Should be back now.
April 27th, 2008 at 11:15 pm
awesome!! I love this.
minor issue…maybe
The images don’t seem to work for me unless I take out the leading slash from background:url
code included in the tutorial (didn’t work for me)
background:url(/images/sprite.png)
this worked for me
background:url(images/sprite.png)
April 30th, 2008 at 10:32 pm
The leading slash may or may not be required based on your site’s directory structure. Glad you figure out how to get it working.
May 13th, 2008 at 1:45 am
Well - thats the most elegant solution I have seen in a while - FAB - thanks