Subscribe

RSS Feed | RSS Email | Twitter
 

Sponsors

 

Search / Categories

 

Coding XHTML & CSS Sprite Navigation

Posted on March 26th, 2009 by Zen Elements 11 Comments

XHTML CSS Sprite Navigation Tutorial

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

  1. template.html
  2. <div id="wwwNav">
  3.   <ul id="Nav">
  4.     <li id="nav00"><a href="#">home</a></li>
  5.     <li id="nav01"><a href="#">about</a></li>
  6.     <li id="nav02"><a href="#">services</a></li>
  7.     <li id="nav03"><a href="#">work</a></li>
  8.     <li id="nav04"><a href="#">blog</a></li>
  9.     <li id="nav05"><a href="#">contact</a></li>
  10.   </ul>
  11. </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

  1. style.css
  2. #wwwNav { width:950px; height:35px; margin:25px 0;}
  3. #wwwNav #Nav { width:950px; height:35px; margin:0; padding:0; background:url(’sprite-nav.jpg’) 0 0 no-repeat; }
  4. #wwwNav #Nav li { display:inline; }
  5. #wwwNav #Nav li a { float:left; outline:none; width:125px; height:0; padding-top:35px; overflow:hidden; }
  6. #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

  1. style.css (continued)
  2. #wwwNav #Nav li#nav00 a { background-position: 0 0; }
  3. #wwwNav #Nav li#nav01 a { background-position: -125px 0; }
  4. #wwwNav #Nav li#nav02 a { background-position: -250px 0; }
  5. #wwwNav #Nav li#nav03 a { background-position: -375px 0; }
  6. #wwwNav #Nav li#nav04 a { background-position: -500px 0; }
  7. #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.

  1. style.css (continued)
  2. #wwwNav #Nav li#nav00 a:hover { background-position: 0 -35px; }
  3. #wwwNav #Nav li#nav01 a:hover { background-position: -125px -35px; }
  4. #wwwNav #Nav li#nav02 a:hover { background-position: -250px -35px; }
  5. #wwwNav #Nav li#nav03 a:hover { background-position: -375px -35px; }
  6. #wwwNav #Nav li#nav04 a:hover { background-position: -500px -35px; }
  7. #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.

  1. style.css (continued)
  2. #wwwNav #Nav li#nav00 a:active { background-position: 0 -70px; }
  3. #wwwNav #Nav li#nav01 a:active { background-position: -125px -70px; }
  4. #wwwNav #Nav li#nav02 a:active { background-position: -250px -70px; }
  5. #wwwNav #Nav li#nav03 a:active { background-position: -375px -70px; }
  6. #wwwNav #Nav li#nav04 a:active { background-position: -500px -70px; }
  7. #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.

  1. style.css (continued)
  2. body#home #Nav li#nav00 a { background-position: 0 -105px; }
  3. body#about #Nav li#nav01 a { background-position: -125px -105px; }
  4. body#services #Nav li#nav02 a { background-position: -250px -105px; }
  5. body#work #Nav li#nav03 a { background-position: -375px -105px; }
  6. body#blog #Nav li#nav04 a { background-position: -500px -105px; }
  7. body#contact #Nav li#nav05 a { background-position: -625px -105px; }

We have now reached a point where if everything is in place correctly, we can load our newly coded Sprite Navigation.

Thank you for reading & I hope you got something out of this!

Alex | Zen Elements


 
RSS
Facebook
StumbleUpon

Comments (11)

AdamMarch 26th, 2009 at 2:56 pm

This is nice, however, not accessible when images are disabled.

Zen ElementsApril 2nd, 2009 at 8:10 am

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

tashfeenApril 15th, 2009 at 2:12 am

Nice :)

Arian XhezairiMay 9th, 2009 at 11:52 am

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

IvanJuly 6th, 2009 at 11:39 am

very useful, thanks :)

JeromevAugust 21st, 2009 at 8:34 am

@Arian Xhezairi: And what about blind people? :-/

Sahus Pilwal, Web Designer HastingsSeptember 11th, 2009 at 6:06 pm

@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!!! ;-)

AnnaSeptember 23rd, 2009 at 3:08 am

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.

PSD to HTMLOctober 14th, 2009 at 4:09 pm

I agree with Anna. It is better to have the text accessible and the background image change.

JRFebruary 5th, 2010 at 6:19 pm

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!

julianneFebruary 11th, 2010 at 1:43 pm

wow nice tutorial

Leave a comment

Your comment

top
  • Zen Elements on Facebook
  • Zen Elements on Flickr
  • Zen Elements on Twitter
  • Zen Elements RSS Feed

© Zen Elements Ltd. 2008. All Rights Reserved. Sitemap | CSS | XHTML | Terms & Conditions