Home arrow Style Sheets arrow Page 3 - 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 - Rebuilding the dynamic banner as a two-image CSS sprite
(Page 3 of 4 )

As I said in the previous section, creating a dynamic banner using a CSS sprite is indeed a simple process that can be performed with minor hassles. Basically, we will create a single background image that contains the respective stages of the banner in question.

To clarify this process, below I included a reduced version of the background image that I'm going to use with the sample banner. It looks like this:

Having at our disposal the above background image, creating the CSS styles that implement the sprite is as simple as this:

<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>

That's was really easy, wasn't it? As you can see, the above CSS declarations are really simple to grasp. All they do is change the position of the previous background image at each stage of the link where the banner will be included. Similar to the rollover example that you learned in the first part of the series, this case implements the same logic.

Now do you realize the neat functionality provided by CSS sprites in different situations? I bet you do!

Okay, at this point I'm pretty sure that you grasped the logic that stands behind using a CSS sprite for creating the prior sample banner. However, the CSS code that corresponds to the previous example looks rather disconnected, since it hasn't been attached to the respective structural markup.

Therefore, in the course of the section to come, I'm going to show you the complete source code that implements this simple CSS sprite-based banner. You can quickly copy it and paste it into your favorite text editor.

As usual, to see how the full code of this dynamic banner looks, click on the link below and keep reading.


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