PHP Developer / Blog

April
3rd, 2008

Extremely efficient image rollovers using CSS sprites and NO Javascript

Digg this article · Save to del.icio.us · Stumble it!

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.

  1. A single sprite graphic (we’ll use one similar to the tabs above)
  2. 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 WasWay Too Easy, No?

That’s the basic idea. You can add any additional CSS needed to incorporate it into your site.

As always…comments welcome.

7 Responses to “Extremely efficient image rollovers using CSS sprites and NO Javascript”

  1. Brando Says:

    This is so brilliantly simple, plus there’s no extra load time for double the graphics.

    Way to go man…

  2. jaisen Says:

    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.

  3. HENRY Says:

    broken image / demo..

  4. jaisen Says:

    @Henry, Sorry about that. Result of a git merge gone bad. Should be back now.

  5. MousePotato Says:

    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)

  6. jaisen Says:

    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.

  7. Steve Abrahart Says:

    Well - thats the most elegant solution I have seen in a while - FAB - thanks

Leave a Reply

Captcha
Enter the letters you see above.


About this site:
This is my (Jaisen Mathai) personal site for potential employers who want to see my resume or portfolio. My ideal job would be to work as a PHP developer on a large scale consumer website. My experience is in using PHP, MySQL, Ajax and JSON. I really enjoy creative brainstorming...taking a problem apart and narrowing 100 solutions down to the best one.

Thanks for stopping by. Be sure to drop me a line.