Home arrow Style Sheets arrow Page 4 - Building a Dynamic Banner with CSS Sprites
STYLE SHEETS

Building a Dynamic Banner with CSS Sprites


CSS sprites are a powerful technique that can be used to incorporate rollover buttons, dynamic banners, and similar elements to any web page. Sprites can also improve the overall performance of a given web site, since they minimize the number of HTTP requests made by the browser. If you’re interested in learning how to make this approach work for you, then you should start reading this article right now!

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 24
February 19, 2007
TABLE OF CONTENTS:
  1. · Building a Dynamic Banner with CSS Sprites
  2. · Building a banner the classic way
  3. · Rebuilding the dynamic banner as a two-image CSS sprite
  4. · Listing the complete source code for the banner

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Building a Dynamic Banner with CSS Sprites - Listing the complete source code for the banner
(Page 4 of 4 )

In consonance with the concepts that I deployed in the section that you just read, below I listed the complete source code that corresponds to this sample banner, which uses a two-image CSS sprite to do its business. Have a look at it, please:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
 
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
 
<title>Sample Banner using CSS Sprites</title>
 
<style type="text/css">
   
body{
       padding: 0;
       margin: 0;
       background: #fff;
   
}

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

    #bannercontainer{
       width: 400px;
       height: 100px;
       padding: 0;
       margin-left: auto;
       margin-right: auto;
       background: #eee;
       border: 1px solid #000;
   
}

    #bannercontainer a{
       display: block;
       width: 400px;
       height: 100px;
       padding: 0;
            background:#eee url(banners.jpg) 0 0 no-repeat;
   
}

    #bannercontainer a:link,a:visited{
       background:#eee url(banners.jpg) 0 0 no-repeat;
   
}

    #bannercontainer a:hover{
       background:#eee url(banners.jpg) -400px 0 no-repeat;
   
}
 
</style>
 
</head>
 
<body>
   
<h1>Sample Banner using CSS Sprites</h1>
   
<div id="bannercontainer"><a href="http://www.php.net" title="Download PHP now!"></a></div>
 
</body>
</html>

If you don't feel inclined to copy and paste the code of the above example, you can download this ZIP file containing all the supporting material, including the background image that you saw previously. I hope you have a good time testing this CSS sprite-based banner!

Final thoughts

Over the course of this second article of the series, I demonstrated how to create a simple yet effective banner which uses the concept of CSS sprites to work properly, without any noticeable lags between its different stages. As you saw, the logic applied here is identical to that used to build the rollover buttons covered in the first part.

However, this journey hasn't finished yet. In the last tutorial I'm going to teach you how to build a basic image gallery that takes advantage of the functionality provided by CSS sprites to display thumbnails and large pictures as well. See you there!


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials