Defining the Active State of Menu Sections for a CSS Sprite-Based Navigation Bar
In this third part of a series, I put the final touches on our sample navigation bar, which uses the functionality of CSS sprites to define the visual presentation of the “normal,” “hover” and “active” states of its sections. Constructing a graphic user interface like this is a two-step process. First, one creates a good-looking background image, and then tweaks its X and Y coordinates via CSS.
Defining the Active State of Menu Sections for a CSS Sprite-Based Navigation Bar - Finish building the navigation bar (Page 4 of 4 )
As I explained in the preceding section, the last thing that must be done to finish building this CSS sprite-based links bar is to modify its markup, so it can indicate visually to users what section they’re currently visiting. Fortunately, this seemingly complex task is performed simply by assigning the “active” CSS class defined before to the link corresponding to the section being viewed.
The code fragment below represents a scenario where the active section is the one labeled “About Us.” Take a look at it:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<div id="footer">
<h2>Footer section</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
</body>
</html>
See how easy it is to inform users of which section they’re currently visualizing? I bet you do. However, if this process still seems obscure to you, the following screen capture should shed some light on it:
This picture shows the “About Us” section as the active one, but the others can also be active too, right? Well, to recreate this, here’s a final code sample that shows how the markup of the navigation bar should be amended to reflect these cases:
That was simple to read, wasn’t it? Of course, hard-coding each possible active section is pointless; at the risk of being repetitive, this task should usually be performed via a server-side language. Hopefully, this last example gives you a clear idea of how to use the functionality of CSS sprites for building highly-responsive navigational bars.
Now, leverage your creativity and start using this approach to develop your own killer user interfaces!
Final thoughts
That’s all for the moment. In this third part of the series, I gave the final touches to this sample navigation bar, which uses the functionality of CSS sprites to define the visual presentation of the “normal,” “hover” and “active” states of its sections. As you saw for yourself, constructing a graphic user interface like this is a two-steps process that requires first creating a good-looking background image, and then tweaking its X and Y coordinates via CSS. That’s all you need to do, really.
Considering the flexibility offered by CSS sprites, in the upcoming part I’ll be showing how to use them for building a more “artistic” navigation bar, fully loaded with a neat set of professional icons, cast shadows and a few other appealing effects.
Here’s my advice: don’t miss the tutorial to come!
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.