Image Sprite Navigation With CSS
This entry was posted on Monday, October 22nd, 2007 at 9:20 pmWhy slice a new image for each item in a navigation when you can use the same image for all of them? If you’re thinking you can’t do that without sacrificing rollovers, think again. By creating a navigation using an image sprite, you can have a complete navigation, rollovers and all, by only using one image.
What is an image sprite?
Image sprite originated from old nintendo games - what developers had to do to keep the game from having to load a new image each time a different character entered the screen was store all of the images into a grid - once they had this grid they could call different sections of it and tell it to display it on the screen. The same big image was loaded one time, from then on different sections were called and displayed on the page.
How to use image sprites to create a navigation bar
What we are going to do is copy the old image sprite technique used in video games, and apply it to our CSS. How you ask? We can do this in by off-setting the background position of each list item in our nav. Let me break this down step by step to help understand this concept.
1. First thing I did was create a very simple image for my nav bar, you can make one the same as mine if you are following along or if you’d like, it should be clear enough to make your own.

2. Once I was satisfied with my image, I exported it as nav.jpg. Once you have that image exported, change something on your navigation, you can change the color of the background, underline it, drop shadow or anything you want as long as the text for the titles don’t get too close to each other where they are interfering. Once you have made these changes, export the image again but call it: nav-over.jpg.

3. Now that we have two images, we need to combine them together into one image. Open both of the images, with nav.jpg selected, go to the Image > Canvas Size… option. Select the top of the image as the anchor point and change the units of measure to percent. Now make the height 200% and click ok.
Now our nav.jpg is in the top of its window, with blank space beneath it.

4. With Snap turned on, drag nav-over.jpg into the same window as nav.jpg. Place it exactly below it so it fits perfectly into the newly created space. Our new image should look something like this.

5. Now resave nav.jpg with both of the images combined, this is going to be the image we use for our navigation.
Once you have the image saved, we can start applying HTML and CSS to get this sucker working.
6. First we need the HTML, here we can make a simple unordered list that includes a different li for each of the buttons we created in photoshop (in this case 4.)
<ul id=”nav-example”>
<li id=”nav-example-01″><a href=”#”><span>Blog</span></a></li>
<li id=”nav-example-02″><a href=”#”><span>Portfolio</span></a></li>
<li id=”nav-example-03″><a href=”#”><span>Resume</span></a></li>
<li id=”nav-example-04″><a href=”#”><span>Contact</span></a></li>
</ul>
7. What we have now is an unordered list with 4 different button list items that each match a certain button on the navigation. The reason there is a span tag with the text inside it is because this way search engines see the text, not just an image. We will end up turning the span to display:none so the text doesn’t show.
This is still viewed as white hat SEO because the text matches the same as the text in the images. Be careful because you can get flagged for putting different content in the hidden fields if it doesn’t match the text on the background image.
8. Here is the final CSS of the image sprite navigation, the final example is at the bottom of the post, I’ll break each section of the CSS down piece by piece:
#nav-example {
background:url(”/images/nav-final.jpg”) no-repeat;
width:490px;
height:40px;
margin:0;
padding:0;
}#nav-example span {
display: none;
}#nav-example li, #nav-example a {
height:40px;
display:block;
}#nav-example li {
float:left;
list-style:none;
display:inline;
}#nav-example-01 {
width: 98px;
}
#nav-example-02 {width: 131px;}
#nav-example-03 {width: 123px;}
#nav-example-04 {width: 138px;}#nav-example-01 a:hover {background:url(”/images/nav-final.jpg”) 0px -40px no-repeat; }
#nav-example-02 a:hover {background:url(”/images/nav-final.jpg”) -98px -40px no-repeat; }
#nav-example-03 a:hover {background:url(”/images/nav-final.jpg”) -229px -40px no-repeat; }
#nav-example-04 a:hover {background:url(”/images/nav-final.jpg”) -352px -40px no-repeat; }
9. The first part is the sizing and background image of the nav bar, the reason we set the height to 40px is because that is how big my original nav.jpg was before I combined it with the nav-over.jpg. This way only the top half of the image displays.
10. The #nav-example span is set to display none, this is what is going to hide the text of each list item. (Remember be careful not to try to hide keywords in these hidden fields, search engines will penalize you.)
11. #nav-example li, #nav-example a {height:40px; display:block;} - What this line is doing is displaying each list item and link to display block with a height of 40px. This makes sure that each link on the navigation is the same size. The reason it is displayed as block is so we can change the size of it, if it wasn’t we couldn’t change the width and height of each link.
12. #nav-example li {float:left; list-style:none; display:inline;} - The float:left is making it so each list item moves to the left side of each li below it, display inline makes sure they display in a horizontal line. List-style: none changes the li’s to not have a bullet next to them, sweetness.
13. #nav-example-01 {width: 98px;} - This is setting the width of the first button “Blog,” if I wanted to start this button 10px off the left side of the #nav-example I could do it by setting margin-left: 10px. The next couple lines are setting the width of each individual link, this way the 4 link widths add up to the total width of my main navigation(you can measure each link by either making guides or selections). Now comes the cool part.
14. What we have to do is set the a:hover state, the way the background is offset on each hover is (horizontal number of pixels) (vertical number of pixels.) Since we have a horizontal nav bar, the vertical number of pixels to be offset is going to be the same for all of them. For the first one it is going to be 0px -40px, 0px because the background image only needs to come straight up to the top left of the nav, so what we do is tell it to go 0px left or right and -40px vertical, a negative integer brings the background UP. This shows the bottom part of the sprite nav, and since we set the width of the li’s it only shows on the particular li we are hovering over.
15. To get the next number we take the width of the nav-example-01 and subtract if from the previous horizontal offset number(which was 0 in this case.) 0-98 = -98px, I’m a math genius. Now we can put this in for the next li, nav-example-02 a:hover will be -98px -40px no-repeat.
16. We do the same for the next one now, -98-131=-229px. Now the offset for the a:hover on nav-example-02 is -229px -40px. This is moving the top left corner of the li background image to the correct position.
17. Normally if you were to put the a:hover’s background image to nav-final.jpg, whichever li you were in it would show the new background image in the top left corner of it, this is why you have to offset the horizontal value to get the correct section where you want it.
18. Now if you do the same process for calculating each button you’ll end up with the same numbers I do, unless you have your own nav with more buttons or different width, then you’re on your own at this point. Once all the CSS is laid out correctly your nav should function like this one below.
If you enjoyed this article, you can digg it here Creating an Image Sprite Navigation with CSS. Or if you missed my passed article on Mac Applications that can increase productivity, you can check that one out also.
Any questions can be posted below in the comments and I will try to answer them as clearly as possible.

It’s also important to point out that this way of doing an image-navigator through CSS, concedes with the lines of accessibility through W3’s WCAG.
…as long as you remember to have a good contrast on the background and the text-color
October 24th, 2007 at 3:57 amThat was absolutely pukka!
The Bloc Party website was designed like this and I was really impressed with the concept as it is a low resource way of using Custom typography.
What happens if you float the element in your design though? will the internal co-ords still be absolute??
October 24th, 2007 at 6:12 amIt will still work if you float it, the reason is that the coordinates are only changing the background position, not the position of the actual nav. It should be fine, if you try it and run into problems let me know and I can figure it out for ya
October 24th, 2007 at 8:08 amVery nice. Css is amazing. I love what you can accomplish with it.
October 24th, 2007 at 10:50 amThank you for posting this. This is an awesome technique. I use it on all my sites.
October 24th, 2007 at 11:07 amThank you, I just wanted to give a greeting and tell you I like your website very much.
October 25th, 2007 at 9:06 amWhat happens when the client comes back next year and asks for three more links in the navigation, then asks for padding to be reduced between each link? You’d need Photoshop each time the positioning needed editing. Wouldn’t the nav be better served by slicing each link individually and using CSS to pad them, rather than all in one piece? Otherwise this seems like a really un-scalable solution.
October 25th, 2007 at 11:28 amMaybe I’m alone but using photoshop one time a year doesn’t sound like too much work to me. You are going to have to make new slices any way you put it, in your case, you’ll be making a lot more slices which also turns into more code. The amount of time it takes me to add links, reslice and code the nav would take me a lot less time than slicing 3 more images and coding each one of them.
October 25th, 2007 at 5:46 pmDefinitely true - there are many ways to skin a cat. Before reading yours, I was more familiar with the Sprite/Sliding Doors method where the text is ascii and the images are scalable, thus no Photoshop required.
October 25th, 2007 at 7:20 pmThanks for example! I will use it!
October 26th, 2007 at 2:13 am@Darren
There are many ways and that is the best part about it! I have seen that sliding doors example before but one of the advantages of this way is that you don’t have to use a default font. You can use any font you want and add text effects to it. If all I wanted was a background or color change that is definitely one of the ways to go about it though. Thanks for the comments-
October 26th, 2007 at 8:26 amSomeday I will switch to CSS. But tables are so much easier to work with.
October 26th, 2007 at 9:05 pm[…] Image Sprite Navigation Using CSS - Tutorial (tags: css html navigation photoshop tutorial) […]
October 28th, 2007 at 5:19 amI should point out that it’s slightly less code if you leave off the full background declaration in the :hover portions and just set background-position appropriately. That way you only specify the image in one place and thusly make your life better.
Your implementation is a little strange, because you actually seem to mask a general background with a more specific one per-element, whereas the approach I would probably have used would have had each element having its own background to begin with, and then moving it.
Anyway, sprites rock
October 28th, 2007 at 4:37 pmI would argue this is not the most accessible way of doing things. As Roger Johansson recently pointed out (http://www.456bereastreet.com/archive/200711/screen_readers_sometimes_ignore_displaynone/), display: none; is not always read by screen readers. In this case, I would recommend removing the spans as they are not necessary and using the text-indent: -9999px; command to move the link text off the page and keep it accessible to screen readers.
Otherwise, the rest is well formed. Keep up the good work.
Best,
November 8th, 2007 at 10:10 pmDaniel
You show us very important matters. most of your guideline is unknown to me. I was very much needy. I was searching since long time. I am really lucky at least I found your blog. Thank you for your kind consideration.
November 10th, 2007 at 4:33 amHere is good tutorial about CSS : CSS Tutorial
November 19th, 2007 at 3:25 amHere is good tutorial about AJAX : AJAX Tutorial
Hi! Thanks for the tutorial, but somehow I can’t make it work.
Maybe I did something wrong and just can’t figure out what. Anyways! KUDOS to the tutorial! Hoping that I can use it correctly! 
November 20th, 2007 at 5:24 amokay… sorry for triple posting. it worked fine now!
the only problem was the ” sign… it showed a different one in my control panel. anyways! THANKS SOOOOO MUCH!!!
November 20th, 2007 at 5:48 amI had problems but now it’s working!
November 22nd, 2007 at 11:42 amThanks!!!, You are sooo helpful!
November 23rd, 2007 at 6:04 pmHi, this tutorial is amazing. I just finished implementing something similar based on this tutorial. Kool!
November 28th, 2007 at 2:11 amThank you, this is exactly what I was looking for. This Rocks!
December 6th, 2007 at 8:07 pmI like the tutorial,and was able to make it work for my own images the first try! Thanks. However, I was trying to create a dropdown navigation for two of the links, and just can’t seem to make it work. I have reviewed suckerfish, and all the others out there to be googled, but am having no luck modifying or understanding the code to work in my instance. I’m creating this for my company’s intranet which uses IE6, and am an intermediate developer. Thanks in advance for any help.
December 11th, 2007 at 2:02 pmIs this where you are referencing from? http://www.htmldog.com/articles/suckerfish/dropdowns/
That is the method I usually use but I’ve never had a problem in IE6, just IE7, in that case I use the swfobject javascript.
If you still can’t get it maybe you can send me your code for the nav and I can take a look at it. The easiest way to do that is probably using the contact form. If you do this I can try to get to it as soon as I can
December 13th, 2007 at 12:03 pmJose check your email, I sent you a request for the jpg image. Also, try not to post your code here, use the contact form on the Contact page. Thx
December 13th, 2007 at 11:56 pmI need it too! my e-mail: zepp@enfootwear.com! Thanks!
December 14th, 2007 at 8:35 amI finally got it to work, after hours of hair pulling. I would like you to look at my code if you don’t mind to give me pointers as to where it can be cleaned up, etc. Sorry about the mess I made before, I am new to the posting and blogging stuff. I will send it the ‘right’ way later. Thanks for your help.
December 19th, 2007 at 3:50 pmCSS is so much more difficult for me to do everything. I find it such a waste of time. But your post actually makes some sense. So i might give it a go again. Thanks
December 27th, 2007 at 11:10 pmAwesome stuff! Thank you for letting me know about this! it seems very helpful. this tutorial is amazing. thank u again for sharing.
January 3rd, 2008 at 11:12 pmso what happens when a user with images on but css off ? can this be made to degrade back to something usefull to the handicaped browsers
January 16th, 2008 at 1:21 pmNice
January 19th, 2008 at 5:10 amThanks for post! It’s really interesting for me
Excellent stuff. Thanks for the tutorial. I find CSS more difficult to work with. But after reading your article I will certainly try it.
January 19th, 2008 at 10:06 pmThanks.
I’m totally confused and sad because I just can’t get a background. The list is order properly :S
I don’t understand what I’m doing wrong and it’s really frustrating.
can someone please help me?
February 1st, 2008 at 11:44 amgreat tutorial.
February 6th, 2008 at 8:05 amhow ever the menu i’m making is vertical. So i tryed to aply this tutorial to that… i seem to have got it to work … but the images wont show.
Nvm … included a pngfix script and it works like a charm.
February 6th, 2008 at 8:23 amThanks for the tut. Works great on my site.
February 8th, 2008 at 7:44 pmCool. It works great on my site. I’m share this to my friends
February 15th, 2008 at 5:43 am[…] 2. Image Sprite Navigation With CSS […]
February 16th, 2008 at 1:09 amVery very cool. its also looks on my site
February 16th, 2008 at 10:18 amawesome, i’m so glad i found your website.
February 19th, 2008 at 12:32 pmGreat tutorial. Thank you!!
Could you please explain how you would maintain an active state on the button which relates to the page that is being viewed.
Is it possible?
February 19th, 2008 at 9:09 pmcool
February 20th, 2008 at 1:50 am@Michael
The easiest way for me to get the current page to show the active state is to use a body id. So if its the index file I’d have in the actual HTML.
This way in the CSS you can write a specific rule for it like so,
body#index #nav-example-01 {background:url(”/images/nav-final.jpg”) 0px -40px no-repeat; }
February 20th, 2008 at 9:19 amAwesome and clever technique! Can you give an actual working example of your sprites menu keeping the active state showing? I’m not sure how to apply what you’ve said above about it. I made my graphic with a 3rd extra darker button row for the active page link and added a section with lines like #sprite-nav-01 a:active {background:url(”images/my-nav.gif”) 0px -60px no-repeat; } etc. The active button shows when I click a page link but it won’t stay to show its the current page. Each button in my 600×90 graphic is 100×30 each for a 6 button nav bar. Thanks!
February 25th, 2008 at 9:49 pmSorry, I forgot to post my test page link to my name.
February 25th, 2008 at 9:50 pmI got the active page buttons to stick after I figured out to modify the html body tag to etc. for each of my test layout pages,(matching the menu link names and css code references. Thanks so much for your sprites code!
February 26th, 2008 at 5:38 pmGreat tutorial man. Glad to see a lot of people are figuring this technique out. Good job spreading the word!
February 29th, 2008 at 12:58 pm[…] Image sprite navigation using CSS from Style Meltdown […]
March 3rd, 2008 at 8:14 pmCool Steve glad to see you got it working
March 3rd, 2008 at 11:09 pm[…] Image Sprite Navigation With CSS […]
March 3rd, 2008 at 11:25 pm[…] Imagem sprite navegação, utilizando CSS - Créditos: Style Meltdown […]
March 4th, 2008 at 9:54 pmWoow. interesting post.
March 9th, 2008 at 6:33 amthanks for all.
Great tutorial! I’m trying to implement this with sub level drop downs. Everything works great except…when I mouse onto the drop down sub level I lose the mouseover state of the main level. Any suggestions??
March 21st, 2008 at 12:31 pm@josh - Ive never been able to/tried to get the rollover to stay in the hover state when going to the sub navigation, I’ll see if I can figure something out
March 21st, 2008 at 1:12 pmWow great! I did see this:
http://www.tyssendesign.com.au/examples/IR-navbar.html
The hover state stays intact on this example but I still can’t figure out how to integrate it into your example.
March 21st, 2008 at 1:46 pmI’m doing something wrong. I have no idea what. My hover state doesn’t display. Would someone be willing to look at my code? I followed step by step too!
Please, Please, Please. I want to learn how to do this!
March 26th, 2008 at 12:02 pmvery nice…thanks..that helped a lot!
March 31st, 2008 at 11:17 am=P
Good my comment is 59 !!! Its a cool blog I ve got to tell ya, Info help me a lot, just like for you Ben Wagner
April 8th, 2008 at 10:00 amI never understood how they did any of this. Wow, CSS has so much involved. I’m intrigued though now. Interesting blog, great tutorial for those that have no clue how to do this stuff… thanks for posting!
April 9th, 2008 at 9:46 am[…] 2. Image Sprite Navigation With CSS […]
April 10th, 2008 at 11:13 pm[…] Image Sprite Navigation With CSS- Creating a navigation using an image sprite, you can have a complete navigation, rollovers and all, by only using one image. […]
April 21st, 2008 at 6:17 am[…] Image Sprite Navigation With CSS- ????”??”??,????????????. […]
April 23rd, 2008 at 1:05 am[…] Image Sprite Navigation With CSS- Creating a navigation using an image sprite, you can have a complete navigation, rollovers and all, by only using one image. […]
April 25th, 2008 at 2:18 am[…] an image sprite, you can have a complete navigation, rollovers and all, by only using one image.read more | digg […]
April 27th, 2008 at 10:06 amCan you tell me why my image only shows on hover:
http://www.sandleraia.com/28a.php
Same thing on that page when I used your image and related code.
Thank you.
April 28th, 2008 at 12:09 pmHey Dave -
Try adding background: none; to the line #nav-example a. Should looking something like:
April 28th, 2008 at 1:15 pm#nav-example li, #nav-example a {
height:31px;
display:block;
background: none;
}
[…] Image Sprite Navigation With CSS [Live Demo] - Creating a navigation using an image sprite, you can have a complete navigation, rollovers and all, by only using one image. […]
April 29th, 2008 at 12:41 amThank you Alex, that worked!
April 29th, 2008 at 3:42 amOOPS - it works in FF but not IE, any further ideas???
April 29th, 2008 at 3:46 amDave - Sorry about that, looks like you need a space between your background url and no-repeat on #nav-example. Should look like this (so there is a space between .png) no-repeat)
#nav-example {
background:url(/images/nav-bar.png) no-repeat;
width:776px;
height:31px;
margin:0;
padding:0;
}
Thats why we love IE6
April 29th, 2008 at 7:57 amThat did it, Alex - sorry for my carelessness, and thanks again.
April 29th, 2008 at 12:34 pmalright fools, spent all day figuring this one out. I had a sprite sliding png menu that had to work in IE6.
The only way to accomplish this is to put a link with the background png inside of a div that has overflow:hidden, and then on a:hover change the relative position of the anchor tag within the div.
THE CSS:
#header_tab{
position:relative;
overflow:hidden;
float:left;
height:35px;
width:72px;
}
#home_link {
display:block;
position:relative;
top:0px;
bottom:0px;
height:70px;
width:72px;
cursor:pointer;
background:url(../images/header/tab_home.png);
behavior: url(iepngfix.htc);
}
#home_link:hover {
top:-35px;
left:0px;
}
THE HTML:
Hope this helps anyone trying to pull this off. There’s also a version of the twinhelix htc file that supposedly does some background positioning automatically, but i couldn’t get it to work right (it’s in beta I think).
any questions: dan[at]kickascii[dot]com
May 2nd, 2008 at 4:02 pmthe HTML was stripped from my above post…
{div id=”header_tab_left”}
May 2nd, 2008 at 4:06 pm{a id=”home_link” href=”#”}{/a}
{/div}
crap..I forgot to change the id on the header tab..here it is in the correct form.
{div id=”header_tab”}
May 2nd, 2008 at 4:08 pm{a id=”home_link” href=”#”}{/a}
{/div}
If you add this to the CSS:
#nav-example-01 a {background:url(”/images/nav-final.jpg”) 0px -40px no-repeat; }
#nav-example-02 a {background:url(”/images/nav-final.jpg”) -98px -40px no-repeat; }
#nav-example-03 a {background:url(”/images/nav-final.jpg”) -229px -40px no-repeat; }
#nav-example-04 a {background:url(”/images/nav-final.jpg”) -352px -40px no-repeat; }
and remove the:
background:url(”/images/nav-final.jpg”) no-repeat;
from #nav-example, you can use padding a margin attributes in the #nav-example and still have the desired effect.
May 5th, 2008 at 4:43 amLOL if you add this sorry.
#nav-example-01 a {background:url(”/images/nav-final.jpg”) 0px 0px no-repeat; }
May 5th, 2008 at 4:44 am#nav-example-02 a {background:url(”/images/nav-final.jpg”) -98px 0px no-repeat; }
#nav-example-03 a {background:url(”/images/nav-final.jpg”) -229px 0px no-repeat; }
#nav-example-04 a {background:url(”/images/nav-final.jpg”) -352px 0px no-repeat; }
[…] Image Sprite Navigation With CSS- Creating a guidance using an ikon sprite, you crapper hit a rank navigation, rollovers and all, by exclusive using digit image. […]
May 6th, 2008 at 1:08 am[…] Image Sprite Navigation With CSS- Creating a guidance using an ikon sprite, you crapper hit a rank navigation, rollovers and all, by exclusive using digit image. […]
May 6th, 2008 at 1:08 am