Using CSS sprites allows you to greatly increase your websites speed by using single image files that contain multiple graphics. In other words, when you have many images to be used, instead of having them as different individual files, we combine them into one. Therefore, the client computer only downloads one image for all the different graphics to be displayed.
Benefits of CSS sprites
Using CSS sprite images on your website is the best way to quicken up the loading time of your images and is also the best way to keep up with today’s best web design practices.
CSS sprites decrease the loading time of images on your web pages, and can significantly lower the number of requests that your website makes which can drastically lower total page load time.
If you need more reason to use sprites, consider that most of the top sites out there are using them: Apple, Amazon, Yahoo… Tutorial9 ![]()
Materials Needed:
- A navigation menu (with multiple states other than the normal state, you may download the sample).
- Measuring application (err, you can try with a real-life ruler but Photoshop, Slammer or XScope would be better).
- A code editor (preferably, Notepad++ on Windows or TextWrangler on Mac, both free. I used both TextWrangler and Coda).
Image – Step 1: Set up each individual navigation menu image states
Since this tutorial is not focused on designing beautiful navigation bars, we’ll use pre-made ones. Don’t worry, there are plenty of Photoshop tutorials on Tutorial9 to teach you that, browse around!
Need a pretty navigation bar?
Download the source files here.
In the source files, there are 3 separate files for the navigation bar’s normal state, hover/rollover state and clicked state.
We’ll teach you how to combine them yourself in the next step. But you can always use the one combined image of all 3 states provided in the source files.
Step 2: Image – Combine all 3 navigation bar image states into 1 image
Right now, we have 3 separate PNG files for the 3 different states. It’s time to copy-paste them together into 1 image just as shown above.
A personal tip.
Usually, what I like to do in this step is (in Photoshop) make the background invisible so that all you have left are the buttons themselves like this:
![]()
Then, I go to Image > Trim:
![]()
Select Transparent Pixels and check all the sides to trim away:
![]()
So, now, you have a concise image of the buttons, nothing else. (Note that I made the background visible here):
![]()
Now that we’ve trimmed away correctly all the 3 states, let’s merge them into 1 image.
There is no standard way of doing this, you may be used to your own way of copy pasting but for the sake of example, start by opening up the normal state then press CTRL or CMD + a (select all) and CTRL or CMD + a (copy)just like below.
Create new document (CTRL or CMD + n).
Copy pasting tip
We copied the navigation bar before making the new document so that Photoshop will automatically know the width and height of the navigation bar.
You can see from the new document panel that the height of one navigation bar is 52. Since we’d like to put 3 states in this document, that’s 52 * 3 = 156. So let’s change the height of this new document to 156.
Then, paste the normal state of the navigation bar into the new document.
Move it to the very top of the document.
Repeat this process 2 more times or according to the number navigation bar states you have.
Paste and align the hover state of the navigation bar right below the normal state.
Lastly, paste and align the clicked state of the navigation bar right below the hover state.
If you have aligned all 3 states correctly, they will fit into the new 489 x 52 document perfectly just like so:
Step 3: Image – Measure the height of each buttons
Important: Get something to write on, whether a new text file on screen or a piece of paper for this next step ’cause we’ll need to write down the width and height for each button.
To easily determine the height of the buttons, we can look back on the height of each navigation bar for obvious reasons.
Tip for measuring in Photoshop
Use the Ruler Tool in Photoshop to easily measure things in your document.
![]()
In the screenshot below, the Ruler Tool is used to measure the height of the Home button.
![]()
Also, move the navigation bar below the navigation bar you want to measure one pixel down to see its edge.
![]()
After measuring the height of the buttons, we now know the height of each button is 52px.
And since all of the buttons have the same height, all of the buttons will all be 52px.
Step 4: Image – Measure the width of each buttons
Now, to measure the width of each buttons is a bit more complicated.
As mentioned above, the horizontal gap between each buttons should preferably be even so that when you divide the gap width into 2, one side will not be wider than the other. This is optional, though.
Here the width of the gap between each button is 16px.
When divided into 2 sides, each side of the gap will be 8px.
Tip to keep measurements in Photoshop
Make sure you have Rulers turned on (cmd + r) or go to View > Rulers.
Then, click on the vertical ruler and drag out a guide to the point where you would want your button to end.
![]()
Now that you know how wide your Home button will be, let’s measure it.
Use the Ruler tool again and measure from the left edge of the Home button to the guide we made as shown in the last tip, like this:
You can see the width in the Ruler tool panel:
Repeat the same process for all the other 2 buttons.
Step 5: Image – Width and height of each buttons
Step 6: CSS – setting up the navigation bar
Here comes the coding! I hope you already understand a bit of CSS and HTML. If not, then you can just go ‘head and copy-paste the codes below,
.
First let’s create a DIV for our navigation bar in our HTML document and name it “navBarContainer”.
<div id="navBarContainer"> </div>
Now, let’s style this DIV in an external CSS stylesheet.
Linking an external CSS stylesheet to an HTML document
Put this line of code into the <head> tag of your document:
<link rel="stylesheet" type="text/css" media="screen" href="stylesheet_name.css" />
Like this:
<html>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="stylesheet_name.css" />
</head>
</html>
Continuing on with styling the “navBarContainer” DIV.
In the external CSS, let’s write down the width, height and margin of the DIV that contains this navigation bar. It’s very easy to do so since we’ve already determined the width and height before.
#navBarContainer{
width: 489px;
height: 52px;
margin: 0 auto;
}
Note: the height for the navigation bar is 52 pixels tall, not 156 pixels. This is because we’d like to show only one navigation bar at a time.
Next, we’ll show how to use HTML lists to display links. This is a much more structured way for links.
Step 8: CSS – Using HTML lists (ul tags) for the navigation bar links
Let’s continue to code our HTML document.
We’ll now add the lists for each link:
<div id="navBarContainer">
<ul id="navBar">
<li id="navBarHome"><a href="#">Home</a></li>
<li id="navBarServices"><a href="#">Services</a></li>
<li id="navBarAboutus"><a href="#">About us</a></li>
</ul>
</div>
By default, HTML lists are structured on display as vertical bullets.
To make them flow horizontally, we’ll use “none” for the “list-style” attribute. We’ll also have to use “inline” for the “display” attribute.
ul#navBar{
width: 489px;
height: 52px;
margin: 0 auto;
list-style: none;
}
ul#navBar li{
display: inline;
}
So, instead of this:
We have this:
Step 9: CSS – adding images for each links
This first step is useful to still display the links as text whenever CSS has been turned off in browsers and the images aren’t available.
Add this code to do so:
ul#navBar li a{
height: 52px;
float: left;
text-indent: -9999px;
}
The “-9999px” was not a typo, this is too keep the text links off the screen if CSS is turned on. If not then the text links would overlap the navigation bar images.
Now to display some image for the home link!
ul#navBar li#navBarHome a{
width: 126px;
background: url(neonBlackNavBar.png) no-repeat 0px 0px;
}
Ta-da! Our first link is set up:
By the way, we changed our body background to #131313.
For the other 2 buttons, we apply the same codes but we change their background positions and widths.
Here’s the code for the “Services” button:
ul#navBar li#navBarServices a{
width: 179px;
background: url(neonBlackNavBar.png) no-repeat -126px 0px;
}
“WAIT! How’d you get that “-126px”?! Good question.
Since we used a sprite image, we need to ‘cut’ the images inside the sprite image.
We do this by specifying the background position.
Since the “Home” button x-axis was at 0px, the “Services” button (obviously, come on!
) must begin where the “Home” button ended. And that is 126 pixels.
Now, here’s the code for the “About us” button:
ul#navBar li#navBarAboutus a{
width: 184px;
background: url(neonBlackNavBar.png) no-repeat -179px 0px;
}
And we’re done!
Actually, no we’re not done, the image above is a pitfall.
The “About us” button does not start at -179px just because the “Services” button ended there.
The correct calculation is (width of “Home” button) + (width of “Services” button). Therefore, the “About us” button begins at -305px.
Now, we’re truly done, I promise.
Step 10: CSS – adding hover states to the navigation bar
Adding hover states owns the same principle as adding the other 2 links. We just move their background positions.
For the “Home” button, we simply move its y-axis background position to where the normal state navigation bar ended, like so:
ul#navBar li#navBarHome a:hover{
background-position: 0 -52px;
}
Now, when you hover over the “Home” button, you’ll see its hover state.
Let’s apply the same principles to the “Services” button.
ul#navBar li#navBarServices a:hover{
background: url(neonBlackNavBar.png) no-repeat -126px -52px;
}
We’ll get this:
Lastly, to the “About us” button.
ul#navBar li#navBarAboutus a:hover{
background: url(neonBlackNavBar.png) no-repeat -305px -52px;
}
Preview:
Step 11: CSS – adding clicked/active states to the navigation bar
Given the keyword “active” for clicked link states, I really, REALLY hope you can already do this part yourself.
Here are the codes for the hover states for all 3 links:
ul#navBar li#navBarHome a:active{
background-position: 0 -104px;
}
ul#navBar li#navBarServices a:active{
background: url(neonBlackNavBar.png) no-repeat -126px -104px;
}
ul#navBar li#navBarAboutus a:active{
background: url(neonBlackNavBar.png) no-repeat -305px -104px;
}
More Ways to Speed Up Your Website
If you’re a speed junkie, then you’ll love these great articles that explore even more ways to reduce load time of your sites:
- How To Minimize Load Time for Faster User Experiences
- Speed Up Your Website with Better Image Optimization in Photoshop
Free Goodies. Delivered to You.
Subscribe to Tutorial9, and we'll deliver you the newest freebies and tutorials for free.
Subscribe By Email Subscribe By RSSWrite for Tutorial9
- Do you want to get paid $150 for writing at Tutorial9?
- Are you a talented Photoshopper, Blogger, or Photographer?
- Want to help thousands of others by sharing your knowledge?
If so, we're interested in you, and we'll pay you. Find out how to write for Tutorial9.



121 Comments Leave a Comment
nice tutorial+
Nice tut, but when i tried using it, the menu bar breaks off into 4 lines. Its a navbar consisting of 7 links. I’m confused. I’ve tested it on chrome, ie, opera, etc: They’re all the same!
Sorry, for replying so late.
Wha? Are you sure? If you could post some screenshots, that’d be great,
.
At first it took a while to get the hand of this technique, but I can safely say that it’s worth it. I can’t believe anyone would still use those bloated JS image replacement techniques anymore.
I’ve been wanting to store an entire site’s images in one large .png sprite for a while, if only to see how efficient it is. YouTube does this, but I can’t trouble myself to do so.
Yes, I’m so glad we don’t have to use JavaScript for rollover images anymore!
Thank you, I will try it
Very welcome, you have fun, now! It’s a beautiful technique,
.
Awesome technique! Will definitely use it for other separate images on the site as well. Too cool…
hi nice tutorial really help me to do more faster opening the page. but still doing more
can u tell me? how we can fix the pixels in different browsers , because in internet explorer 6. my blog preview is not adjusted
plz see
http://alisoft7.blogspot.com
and tell me
thanks
thanks! fast and easy…
Am i missing something or do you have negative numbers specifying the position of the images?
Hey, Jason,
Nope, you’re not missing anything, we are, indeed, using negative numbers to reposition the images.
Let me try to explain it this way, let’s hope this makes sense.
Imagine the most top left pixel of the image is right on the point of origin on the X and Y-axis.
Once put that way, the rest of the bottom part of the image will be sitting on the negative Y-axis, won’t it?
We should also note that all digital interfaces are calculated this way. The top left of a window is always on the point of origin on the X and Y-axis.
Hello friend
. It definitely helps we to save time (both designers and web users)
it’s quite a bit abstract isn’t it =)
This is an awesome tut. Thanks very much for letting people know about this technique. This is the first time I work with CSS sprite and I’m very impressed
However there were some mistakes that I found out during the time followed your tut:
1. At the very first step with CSS you forgot to determine the width of the #navBarContainer (so this div is 52px high and… 0px wide). Therefore, I (and other poeple – I think) cannot see the result of the Home button (also the other buttons). I fixed it by adding this line ” width:auto; ” (I prefer to use auto, much more easier for the next steps). From that point, the line ” margin: 0 auto; ” cannot be used anymore. I used ” margin-left: 25em; ” to replace it (so the navBar will be aligned in the middle of the screen as normal)
2.Someone complained about multiple lines, I got this problem too when I followed your steps. But the two CSS lines that I fixed and added above solved this problem. It’s hard to explain how do they work but… this is CSS
Anyways, thank for this nice tutorial. Hope you will fix your page soon, so other one won’t be wondered anymore ;]
Hello, Son Le,
Thanks for the feedback and kind words, first of all.
Secondly, the width of the navBarContainer DIV was determined in Step 6 in the code. Not sure if you saw that but I’m sure it’s there,
.
Thirdly, about using margin-left. A little problem with that is when the font size increases then the navBar will not be horizontally aligned anymore.
Lastly, CSS is quite abstract, isn’t it?
Again, thank you for taking your time in writing the reply!
Nice tutorial. Thanks for sharing.
No problem at all.
Useful Tutorial, Great Post.
this tutorial is the worst tutorial ever you dont even explain why you got so much going on in the CSS names ex….. #navbar li #navbar a… to much shit is going on… your horrible and got me so confused….
Hey, Mikey,
Sorry you don’t quite understand the CSS coding. No worries, I didn’t understand them at all when I was only starting out either.
If it is the DIV names that are confusing you, well, you have to get used to giving your elements proper names when coding in CSS.
As for having a long code, we have to accept that as that is how it works.
Take your time when learning, Mike. You don’t become a CSS guru overnight.
Also, take 5 deep breaths before commenting on posts next time.
Nice tutorial man it will really gonna help..
can you tell me ..Is there any CSS property to make font smooth in IE?
Nice post …
but I have a query….Is there any CSS property to make font smooth in IE?
its very nice. Great post
great i never seen before.47
Great post . Thanks152
Great post . Thanks159
This was a great tutorial, it helped me understand a lot of it…but it only talks about horizontal navigation, what do I do to get it to work for a vertical one? I’m probably just thinking too hard or something. @.@ I’ve been searching for a while looking for vertical CSS Sprites.
This looks really interesting I can’t wait to try it!
Wow, the states on the hovers are terrific and also the copy and pasting tip, so simple yet not fully adapted yet! Thank you.
These are great tutorial. I wanna be.
Cool tutorial thanks for sharing
Thank you very much! This is very helpful, I have been wondering how to do this for a while.
THanks for the great tutorial.
one question though. everything works great in IE8, however when viewed in firefox, the images shift downward out of position when clicked….
any ideas???
Figured it out , i added :
margin:0;
padding:0;
to the second step in in part 9 ;
thanks for the tutorial , very helpful
Thanks for the article…
Your website is incredibly beautiful and useful. I have taken liberty to add this article to my CSS aggregator site. I hope you wont mind that.
Cheers for this, great tutorial.
Thanks for the tutorial, works a treat.
Hey I have recently built my first sprite navigation and I have to admit that it’s really simple once you get your head around it! Instead of uploading multiple image slices to the server your only sending 1 which means only 1 server request! This technique can be used for other things not just navigation. Thanks for sharing this blog it has really helped me.