Coding XHTML & CSS Sprite Navigation
Coding Sprite Navigation into functional XHTML & CSS. This is a second part to the earlier published Creating a Sprite Navigation Set in Ps, where the resource files are also available for download.
In another recently published tutorial labeled Icon Supported Navigation, some direction was given towards seeing Sprite Navigation being implimented and so here we are.
Part 1 – The Basic Mark-up
- template.html
- <div id="wwwNav">
- <ul id="Nav">
- <li id="nav00"><a href="#">home</a></li>
- <li id="nav01"><a href="#">about</a></li>
- <li id="nav02"><a href="#">services</a></li>
- <li id="nav03"><a href="#">work</a></li>
- <li id="nav04"><a href="#">blog</a></li>
- <li id="nav05"><a href="#">contact</a></li>
- </ul>
- </div>
Now that we have our main mark-up assigned for our navigation, we can get down to coding the CSS for it.
Part 2 – Beginning CSS
- style.css
- #wwwNav { width:950px; height:35px; margin:25px 0;}
- #wwwNav #Nav { width:950px; height:35px; margin:0; padding:0; background:url(‘sprite-nav.jpg’) 0 0 no-repeat; }
- #wwwNav #Nav li { display:inline; }
- #wwwNav #Nav li a { float:left; outline:none; width:125px; height:0; padding-top:35px; overflow:hidden; }
- #wwwNav #Nav li a { background-image:url(‘sprite-nav.jpg’); background-repeat:no-repeat; }
The important part I find within the above CSS – besides the entire setup of the navigation – is the ‘outline’ declaration. By applying the outline declaration and setting it to none, you will not get the dotted outline around menu items which often annoyingly includes positioning, margin and padding space.
Now that we have the above written, we can move swiftly on to create the alterative states of the navigation by simply editing the background position of the graphic we have already assigned.
Part 3 – CSS a
- style.css (continued)
- #wwwNav #Nav li#nav00 a { background-position: 0 0; }
- #wwwNav #Nav li#nav01 a { background-position: -125px 0; }
- #wwwNav #Nav li#nav02 a { background-position: -250px 0; }
- #wwwNav #Nav li#nav03 a { background-position: -375px 0; }
- #wwwNav #Nav li#nav04 a { background-position: -500px 0; }
- #wwwNav #Nav li#nav05 a { background-position: -625px 0; }
What we have done above is to create a seperate instance for each menu item by altering the position of the background. As our Sprite Navigation graphic is a horizontal bar, we only have to manipulate the x-axis with the width of each item which in this case is 125 pixels.
Part 4 – CSS a:hover
As we now move down the Sprite Navigation graphic, we alter the value in the y-axis by the height of each menu item, which in this case by 35 pixels.
- style.css (continued)
- #wwwNav #Nav li#nav00 a:hover { background-position: 0 -35px; }
- #wwwNav #Nav li#nav01 a:hover { background-position: -125px -35px; }
- #wwwNav #Nav li#nav02 a:hover { background-position: -250px -35px; }
- #wwwNav #Nav li#nav03 a:hover { background-position: -375px -35px; }
- #wwwNav #Nav li#nav04 a:hover { background-position: -500px -35px; }
- #wwwNav #Nav li#nav05 a:hover { background-position: -625px -35px; }
Part 5 – CSS a:active
Again we continue down our Sprite so alter the background position by double the height; 70 pixels.
- style.css (continued)
- #wwwNav #Nav li#nav00 a:active { background-position: 0 -70px; }
- #wwwNav #Nav li#nav01 a:active { background-position: -125px -70px; }
- #wwwNav #Nav li#nav02 a:active { background-position: -250px -70px; }
- #wwwNav #Nav li#nav03 a:active { background-position: -375px -70px; }
- #wwwNav #Nav li#nav04 a:active { background-position: -500px -70px; }
- #wwwNav #Nav li#nav05 a:active { background-position: -625px -70px; }
Part 6 – CSS Current State
Finally, if like me you apply an id attribute to the body tag, you will be able to use the following CSS for a current state declaration. There are other alternatives to coding the current state but I now find this my own prefered route.
- style.css (continued)
- body#home #Nav li#nav00 a { background-position: 0 -105px; }
- body#about #Nav li#nav01 a { background-position: -125px -105px; }
- body#services #Nav li#nav02 a { background-position: -250px -105px; }
- body#work #Nav li#nav03 a { background-position: -375px -105px; }
- body#blog #Nav li#nav04 a { background-position: -500px -105px; }
- body#contact #Nav li#nav05 a { background-position: -625px -105px; }
Thank you for reading & I hope you got something out of this!
Alex | Zen Elements



This is nice, however, not accessible when images are disabled.
@Adam | I’m glad you enjoyed this. If a users browser does not have images enabled, you are correct that it is inaccessible. This is quite the case with full image sprites as the text is complete with the menu item states. I do prefer to work in and develop usable/accessible websites myself and to achieve this, the best way is by leaving text out with the sprite, creating only a background set to work with standard hyperlinks.
Nice :)
@Adam – IMHO, if someone keeps images disabled in this century’s days, they simple should be censured from surfing the web.
@Zen Elements – nice approach to sprites.
very useful, thanks :)
@Arian Xhezairi: And what about blind people? :-/
@Jeromev – Jeromev you do have a point there! I guess like the Zen man said using standard hyperlinks is probably best for accessibility but if design comes first than images are the way forward! Thanks for the sprites sample. You have another follower!!! ;-)
Alternatively, just have the sprite for the background gradient, and leave the name of the menu item as plain text. Accessible for everyone, and still nice to look at.
I agree with Anna. It is better to have the text accessible and the background image change.
Hi, do you have any suggestion about adding a drop-down menu on ths navigation? I think it would be very useful
Hi All,
Good tutorial indeed, but as much I think users should still have images enabled, I like to cover all corners as well so I would love to know how to adapt with Anna’s suggestion. What exactly would the code look like Anna? Anybody?
Thanks!
wow nice tutorial
Thanks for posting, just what i needed!
useful info. thanks..soon:)
Wonderful site=) I will need a decent amount of time to examine this website.
ultimate gaming chair
Hi,
Great tut, I’m just wondering, currently the nav002 is classed as active, but how do you change this so I can have each different one active for the correct page?
To explain better to my earlier question, on the demo page it says: The above navigation has been set within a body containing the id attribute value of ‘about’ to illustrate the means of a current state. – how do I change this for the value of ‘about’ to say ‘home’
Thanks
Couldn’t you just add alt text with URLs?
This has been based on writing for a static website, so in each page you would just update that id ‘about’ to the label for your active class. Alternatively, you could just write an active state directly for the navigation.
If you’re looking for a dynamic way of doing this, you could detect the page (i.e – with PHP) and write an id into the markup, then target this with your CSS. We prefer to write an id like this onto body in the markup, so we can target other elements in the page the same way. I hope this helps!