Home arrow Style Sheets arrow Page 3 - Creating Rollover Effects with CSS Sprites
STYLE SHEETS

Creating Rollover Effects with CSS Sprites


Using CSS sprites can reduce the lag for certain events on web pages, thus making your web site perform more efficiently and improving your visitor's experience. In this first article of a three-part series, you'll learn how to use CSS sprites in the creation of rollover buttons.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 17
February 12, 2007
TABLE OF CONTENTS:
  1. · Creating Rollover Effects with CSS Sprites
  2. · Creating rollover buttons with CSS: a traditional approach
  3. · Improved rollover buttons: using a CSS sprite-based approach
  4. · Completing the CSS sprite-based rollover example

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Creating Rollover Effects with CSS Sprites - Improved rollover buttons: using a CSS sprite-based approach
(Page 3 of 4 )

As I expressed in the section that you just read, the goal here is to create a new set of rollover buttons which don't show any noticeable delay when the corresponding background images are displayed.

While this sounds like something hard to do, the truth is that achieving a seamless rollover effect is a matter of creating only one image that contains all the states that correspond to a particular link. Of course, if this concept sounds rather confusing to you, please take a look at the following background image, which should dissipate any possible questions. Here it is:

As you can see, the above image is called a "CSS sprite." It has been created in such a way that it contains the four basic stages that corresponds to a regular link. Naturally, the most significant difference to note here is that in the previous example I used fourth different pictures, while in this case I utilized only one.

I'm using not only a single background image, which implies that it's downloaded by the browser once, but also the overall performance of the web document where the rollover will be included can be improved, since the client performs a single HTTP request to the server.

Now that you hopefully learned the logic behind creating a simple CSS sprite, allow me to show you the set of CSS styles that make the rollover work properly. They're as follows:

<style type="text/css">
 
body{
 
    padding: 0;
 
    margin: 0;
 
    background: #fff;
 
}

  h1{
     font: bold 24px Arial, Helvetica, sans-serif;
 
    color: #000;
 
}

  ul{
 
    display: block;
 
    width: 150px;
 
    height: 25px;
 
    margin: 0;
 
    padding: 0;
 
    background: #fff url(buttons.gif) 0 0 no-repeat;
 
}

  li{
 
    margin: 0;
 
    padding: 0;
 
    font: normal 12px Arial, Helvetica, sans-serif;
 
    color: #00f;
 
    overflow: hidden;
 
    list-style: none;
 
}

  li a{
 
    display: block;
 
    width: 150px;
 
    height: 25px;
 
    margin: 0;
 
    padding: 0;
 
    background: #fff url(buttons.gif) 0 0 no-repeat;
 
    padding-left: 50px;
 
    text-decoration: none;
 
}

  li a:link{
 
    color: #000;
 
    background-position: 0 0;
 
}

  li a:visited{
 
    color: #00f;
 
    background-position: 0 -25px;
 
}

  li a:hover{
 
    color: #c90;
 
    background-position: 0 -77px;
 
}

  li a:active{
 
    color: #f00;
 
    background-position: 0 -51px;
 
}
</style>

In this specific case, the rollover effect is achieved by changing the position of the previous background image (the CSS sprite), in accordance with the different stages that correspond to a particular link. This concept is reflected more accurately by the following CSS declarations:

li a:link{
    color: #000;
    background-position: 0 0;
}

li a:visited{
   color: #00f;
   background-position: 0 -25px;
}

li a:hover{
   color: #c90;
   background-position: 0 -77px;
}

li a:active{
   color: #f00;
   background-position: 0 -51px;
}

As you can see, the background image is moved from bottom to top, in consonance with the specific state of the link, which results in displaying a flawless rollover effect without noticeable delays between its different states. Now, do you realize the advantages of using CSS sprites in your web pages? I hope you do!

Okay, at this stage I demonstrated how to include a simple CSS sprite that is used for creating a rollover button. However, the previous example would be rather incomplete if I don't show you the complete source code that includes the corresponding CSS styles that were listed before, and the (X)HTML markup as well.

Therefore, to see how the complete example looks, click on the below link and read the final section of this tutorial.


blog comments powered by Disqus
STYLE SHEETS ARTICLES

- CSS Combinators: Working with Child Combinat...
- CSS Combinators: Using General Siblings
- Intro to CSS Combinators
- CSS Semicircles and Web Page Headers
- Drawing Circular Shapes with CSS3 and Border...
- More CSS Pagination Link Templates
- CSS Pagination Links
- Animated CSS3 Image Gallery: Advanced Transi...
- CSS3 Animated Image Gallery: Transitions
- CSS3 Properties: Fixed Heights with box-sizi...
- CSS3 Properties: Altering Strokes and 3D Eff...
- CSS3 Properties: Text-Stroke
- CSS3 Transitions: Width and Height Properties
- Creating a Drop Down Menu in CSS3
- Intro to CSS Transitions

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials