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

