DiamondLime.com

 
 

Awesome Rollovers with CSS Sprites

If you haven’t learned to do a CSS rollover effect yet, it’s time to learn—they’ve become a main-stay of modern web design and do just about everything better than a Javascript rollover (there are a few exceptions, which I will discuss briefly later)

The Basics of a CSS Sprite Rollover

The basic concept of a CSS sprite rollover is to assign a background image to a link. This image contains the possible states of the link rollover. Based on which “state” is being used, we shift the background image around to display the correct portion of the image.

For example, the Lime Blog RSS feed icon is a CSS sprite rollover. Here’s the image used to create the effect:

CSS rollover image

In the default state of the link, I display the normal version of the RSS logo. The rest is hidden beyond the borders of the link (illustrated in black) like this image illustrates:

CSS rollover default state

In the highlight or selected state of the link, I use the same RSS logo as a background, I just shift it so that the hover state is displayed and the default state is hidden:

CSS sprite hover state

Here’s the basic HTML needed:

<a id=”rssFeed” href=”http://www.diamondlime.com/blog/feed/” title=”Subscribe to RSS feed”>RSS Feed</a>

And here’s the important part of the CSS:

#rssFeed:link, #rssFeed:visited {
background: url(’http://www.diamondlime.com/images-layout/rss.gif’) top left no-repeat;
}
#rssFeed:hover, #rssFeed:active {
background: url(’http://www.diamondlime.com/images-layout/rss.gif’) top right no-repeat;
}

Benefits of a CSS Rollover Using Sprites

There are many benefits to using CSS sprite rollovers, especially for most simple rollover applications:

  • Reduce page load time

    By reducing the number of file requests and the overhead associated with each file, you reduce the load on your server, resulting in a snappier site.

  • Automatic pre-loading of alternate states

    The alternate states are included in just one image that is loaded for the initial state. Once the initial state is loaded, all of the states are loaded.

  • Simple updates

    You only have one image file and one CSS file to edit.

  • Clean, semantic HTML

    Most Javascript rollovers result in bloated, ugly code with behavior and presentation mixed in with the content.

  • Improved accessibility

    Some people have their Javascript and/or images disabled. Rollovers based solely on Javascript would break, whereas the CSS rollovers would degrade gracefully.

Shortcomings of CSS Sprites

There are a few shortcomings to using CSS to execute rollovers:

  • Lack of transition effects

    Using animated .gifs, you can create some transitions, like the tabs at the bottom of the page on Corda.com, but you have to use separate images and they get big quickly.

  • Limited states

    For some complex applications, it’s nice to have multiple states to an object, and CSS is limited to link, visited, hover, and active.

For most intents and purposes, however, CSS is lighter, faster, and cleaner for a rollover, and a sprite-based rollover is one of the best CSS rollover methods.

2 Responses! to 'Awesome Rollovers with CSS Sprites'

Subscribe to comments with RSS or TrackBack to 'Awesome Rollovers with CSS Sprites'.

  1. SpiderSavvy said,

    on February 1st, 2008 at 1:56 pm

    Thanks for the post Michael. Short and to the point!

  2. joejac said,

    on February 4th, 2008 at 3:03 pm

    Thank you for this nice information, unfortunately I can not use embedded nor external css I am forced to use inline style.

    My question is how I do this in an inline style?
    Regards

Leave a Reply

 
 
 

Online Advertising

Blog Roll

BYU Student and Alumni Blogs and Sites