How to code a CSS Sprite Navigation for your website 2/2

Posted on Friday 26th Aug 2011 by James Bavington 6 Comments

In this week’s web design video tutorial we conclude our CSS Sprite Navigation bar tutorial. Last week, we prepared the sprite graphic, that we code in this tutorial. You can find all of the coding used in the video below. To view the live code example – simply click here.

The HTML Code from the video

<ul id="navigation">
    <li class="home"><a href="#">Home</a></li>
    <li class="about"><a href="#">About Us</a></li>
    <li class="chairs"><a href="#">Chairs</a></li>
    <li class="chaises"><a href="#">Chaises</a></li>
    <li class="endofbeds"><a href="#">End of Beds</a></li>
    <li class="stools"><a href="#">Stools</a></li>
    <li class="headboards"><a href="#">Headboards</a></li>
    <li class="faqs"><a href="#">FAQs</a></li>
    <li class="basket"><a href="#">Basket</a></li>
    <li class="checkout"><a href="#">Checkout</a></li>
    <li class="contact"><a href="#">Contact</a></li>
  </ul>

testing

The CSS Code from the video

#navigation { margin:0; padding:0; width:952px; height:37px; background-image:url(withouttext.gif); }

#navigation li { margin:0; padding:0; list-style-type:none; display:inline; height:37px; text-align:center; float:left; line-height:37px; font-family:Georgia, "Times New Roman", Times, serif; }

#navigation a { display:block; height:37px; color:#311f11; text-decoration:none; font-size:13px; }
#navigation a:hover { background-image:url(withouttext.gif); color:#fff; }

#navigation .home { width:71px; }
#navigation .home a:hover { background-position: 0px bottom; }

#navigation .about { width:94px; }
#navigation .about a:hover { background-position: -71px bottom; }

#navigation .chairs { width:76px; }
#navigation .chairs a:hover { background-position: -165px bottom; }

#navigation .chaises { width:84px; }
#navigation .chaises a:hover { background-position: -241px bottom; }

#navigation .endofbeds { width:114px; }
#navigation .endofbeds a:hover { background-position: -325px bottom; }

#navigation .stools { width:74px; }
#navigation .stools a:hover { background-position: -439px bottom; }

#navigation .headboards { width:113px; }
#navigation .headboards a:hover { background-position: -513px bottom; }

#navigation .faqs { width:70px; }
#navigation .faqs a:hover { background-position: -626px bottom; }

#navigation .basket { width:78px; }
#navigation .basket a:hover { background-position: -696px bottom; }

#navigation .checkout { width:96px; }
#navigation .checkout a:hover { background-position: -774px bottom; }

#navigation .contact { width:82px; }
#navigation .contact a:hover { background-position: -870px bottom; }

Hiding the text

As in the video, I also show you how to hide the text, allowing you to use typography that may be flattened onto your Sprite graphic. simply add the following line of CSS:

#navigation li span {display:none; }

And then add Span tags around the text, inside the anchor tags for each of the buttons within your navigation bar. Like in the video, these should be placed like this:

<li class="contact"><a href="#"><span>Contact</span></a></li>

About James Bavington

Away from computers, James enjoys mountains, military history and has an insatiable appetite for film - particularly 90's classics.

See all of posts.

Add on James Bavington Google+

James Bavington's Tweet's

This entry was posted in HTML & CSS Tutorials, Web Design Videos and tagged . Bookmark the permalink.

6 Responses to How to code a CSS Sprite Navigation for your website 2/2

  1. Ahmed says:
    September 6, 2011 at 2:11 am

    i’m one of your followers in youtube ,, and i make sure to watch every video you post ,, and now i become addicated … i appreciate your help & given advices ! i really find all of your tutorials very helpful

    good luck :)

  2. Richard says:
    November 10, 2011 at 8:44 pm

    Hi Guys,

    Love the tutorials!!

    Who did your cartoon characters? Been looking for one of me but cant find a good one

    Cheers

  3. R Middleton says:
    February 3, 2012 at 9:35 pm

    This helps to brake things down.. I always thought this method was a long drawn out affair but the way you put it simplifies it a lot more. The only thing I question is about the display:hidden; – Although you wouldn’t get penlised for it I would imagine that search engines would actively ignore everything with this attribute and if it doesn’t then it soon will as its getting clever

  4. John Jenkins says:
    March 17, 2012 at 10:03 pm

    Hey

    im using ur script to create a nav bar in yola. for some reason the background image i link in the css for the sprite wont show in the yola editor.
    any help would be much appreciated!

    many thanks
    John

  5. zee says:
    March 27, 2012 at 7:27 am

    hey guys, thank you so much for posting these videos. you guys are amazing!!!

  6. Tirath says:
    April 1, 2012 at 6:48 am

    Awesome tutorial, Thanks so much for sharing such a amazing & useful CSS Sprite tutorial
    your tutorials is very nicely explain & easy to understand
    Keep Educating us
    God Bless You :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>