Style Sheets
  Home arrow Style Sheets arrow Page 3 - Creating Rollover Effects with CSS Sprites
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

Creating Rollover Effects with CSS Sprites
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2007-02-12

    Table of Contents:
  • Creating Rollover Effects with CSS Sprites
  • Creating rollover buttons with CSS: a traditional approach
  • Improved rollover buttons: using a CSS sprite-based approach
  • Completing the CSS sprite-based rollover example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

    More Style Sheets Articles
    More By Alejandro Gervasio


       · In this first tutorial of a three-part series, you'll learn how to use CSS sprites...
       · Great job on this one, much like all of your other tutorials and articles I've read...
       · Thank you for the kind comments on my article. I truly appreciate them, and hope the...
     

    STYLE SHEETS ARTICLES

    - Image Replacement CSS Techniques
    - Using BlueTrip`s Success, Notice and Error C...
    - More Uses for the Thin and Caps CSS Classes ...
    - Styling Definition Lists with the BlueTrip C...
    - Styling Unordered and Ordered HTML Lists wit...
    - Using the BlueTrip CSS Framework`s Thin and ...
    - Adding Borders to Web Page Columns with Blue...
    - Introducing the BlueTrip CSS Framework
    - Using a Background Grid to Assist Web Page L...
    - Extending the Rule Of Thirds for Web Page La...
    - A Two-Column Web Page Layout Based on the Ru...
    - Using the Rule Of Thirds for Web Page Layout
    - Swapping Columns Using the Divine Ratio for ...
    - Using the Golden Ratio in Liquid Web Page De...
    - Fundamental Design Principles for Web Page L...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek