Home arrow Style Sheets arrow Page 3 - Building CSS Sprite-Based Navigation Bars
STYLE SHEETS

Building CSS Sprite-Based Navigation Bars


In this first part of a series, we'll start building a stylish navigation bar which uses a combination of image replacement and CSS sprites to define the visual presentation of its sections. The resulting bar will have a clean, professional look that would be at home on any corporate web site.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
February 26, 2010
TABLE OF CONTENTS:
  1. · Building CSS Sprite-Based Navigation Bars
  2. · Defining the navigation bar's structural markup
  3. · Defining the CSS styles for the normal state of the navigation bar
  4. · Including the previous CSS styles in the sample web page

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Building CSS Sprite-Based Navigation Bars - Defining the CSS styles for the normal state of the navigation bar
(Page 3 of 4 )

To be frank, I feel a little bit guilty. In the previous segments I made several references to CSS sprites, assuming that you're already familiar with them. If that's not the case, here's a quick explanation of what they really are, so you can understand their underlying logic.

Basically, CSS sprites are a handy technique that permits you to assign different background images to an HTML element (not at the same time, of course) by cleverly manipulating the "background-position" CSS property. Usually, this process requires first creating a single background graphic containing the distinct images that will be assigned to the targeted element, and then altering the X,Y coordinates of the aforementioned property, to reveal only a certain portion of the container graphic.

In this case a picture is worth a thousand words, so let me show you the background image that I'll be using to style the different states of the navigation bar defined in the preceding section. Here it is:

Do you understand how we're using CSS sprites to create all the sections of this navigational bar? I hope you do. As you can see, the above image includes the three states corresponding to every section, that is the "normal," "hover" and "active" states respectively.

You may be wondering what this buys us. Well, first, utilizing only one background image for all the sections demands a single HTTP request to the web server, and second, there's no visible delay when switching over different states of a section. These are definitely two advantages that are worth considering.

Now that I've outlined the logic behind using CSS sprites, it's time to get our hands dirty and start using the earlier background image to build the navigation bar. But before I get to that point, it's necessary to style the other elements of the XHTML document, including the unordered list just created.

The following CSS block performs this task:

body {

padding: 0;

margin: 0;

background: #fff;

font: 1em Arial, Helvetica, sans-serif;

color: #000;

}

#wrapper {

width: 960px;

margin: 0 auto;

}

#header, #content, #footer {

padding: 30px;

}

/* navbar */

ul#navbar {

width: 780px;

height: 100px;

margin: 0;

padding: 0;

list-style: none;

}

ul#navbar li {

float: left;

width: 130px;

height: 100px;

}

ul#navbar li a {

display: block;

width: 130px;

height: 100px;

text-indent: -9999px;

}

True to form, aside from horizontally positioning the HTML list containing the navigation bar, the above CSS styles are very easy to follow, except for one subtle detail: please, notice the use of simple image replacement method with all the <a> elements, which hides the links' inner text from display thanks to the assignment of a large negative value (-9999px) to the "text-indent" property.

That was the boring part, so let's get to the fun. Having properly positioned the unordered list, we'll now style the "normal" state of each section of the bar. How will we do this? Here's where CSS sprites are put into action! The following CSS code demonstrates this process:

/* About section */

ul#navbar li#about {

background: #800040 url("bg_navbar.png") 0 0 no-repeat;

}

/* Services section */

ul#navbar li#services {

background: #800040 url("bg_navbar.png") 0 -100px no-repeat;

}

/* Products section */

ul#navbar li#products {

background: #800040 url("bg_navbar.png") 0 -200px no-repeat;

}

/* Articles section */

ul#navbar li#articles {

background: #800040 url("bg_navbar.png") 0 -300px no-repeat;

}

/* Blog section */

ul#navbar li#blog {

background: #800040 url("bg_navbar.png") 0 -400px no-repeat;

}

/* Contact section */

ul#navbar li#contact {

background: #800040 url("bg_navbar.png") 0 -500px no-repeat;

}

See how easy it is to assign to each section of the navigation bar the desired background image, thanks to the tricky manipulation of the X, Y coordinates of its background-position property? I guess you do! In this case, the entire styling procedure is reduced to assigning the correct values to the coordinates per section being styled and nothing else. It's that simple, really.

And now that the "normal" state of the corresponding sections has been properly defined, the last thing we'll do in this tutorial is include the previous CSS styles in the sample web page.

This will be done in the last section, so click on the link below and read the lines to come.


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