CSS Sprites – What, Why, How and When?
April 26th, 2010 by
What are CSS sprites?
CSS sprites are a technique where a single large image is used to display multiple images or icons.
How is this possible?
As you may know, there are two ways to display an image in a website – by using the <img> tag, or by defining CSS background-image property for an HTML element (e.g. Div).
In CSS sprites, a single large image is defined as the background of multiple HTML elements, and a different background position is set for each of those elements (read the How section for more details)
Why to use CSS sprites?
Using CSS sprites will speed up your website loading time and thus improve performance and user experience.
Instead of loading 8 images (as you will see in the example below), you load a single large image that contains all 8 images. Therefore, there are 7 less HTTP-Requests and the page loads faster (the client computer sends less requests to the hosting server, which saves time).
How to use CSS sprites?
Take a look at the following menu for example:
![]()
The above single large image (width:548px and height:118px) combines 8 different icons (each of the icons – width:137px and height:59px).
Note: the dashed line is there to demonstrate the dimensions of each menu button (they are all equal in size). It is otherwise not needed.
The first row contains the menu items in normal state and the second row contains the menus as they should appear when mouse over (hover).
Here’s the HTML:
<ul id="mainNav">
<li><a href="/home.php" id="home"></a></li>
<li><a href="/about.php" id="about"></a></li>
<li><a href="/forums.php" id="forums"></a></li>
<li><a href="/contact.php" id="contact"></a></li>
</ul>
And here is the CSS:
![]()
Explanation:
Lines 1 and 2 apply to all menu links.
Line one assigns the large image as background to all menu items, and line 2 defines their width and height (important).
Now come the lines that define background positions to each of the menu items. Note that the starting point of the background position is the upper left corner.
Line 3 relates to the upper home icon and is in fact dispensable because background position of 0 is the default.
Line 4 relates to the bottom home icon and counts 59px down from the start point (upper left corner).
Line 5 relates to the upper about icon and counts 0px down and 137px to the right from the start point. And so on.
Another option is using CSS sprites generator such as this one.
However, in many cases doing the manual work is quicker and more accurate than working with these generators. In addition, most of them won’t support 2 rows as in the menu example above.
When to use CSS sprites
CSS sprites can be used for any image that does not repeat in a website.
It is recommended to use it for elements that are used commonly in your site and not for elements that appear in a single page.
The example above is a classic use of CSS sprites, but there are other uses.
Here’s one example:
The tag cloud bellow should expand whenever new tags are added.

In this post I included the HTML and CSS for the background of the above tag cloud.
However, the CSS can be done even better – the top and the bottom of that box do not repeat (unlike the middle background that does repeat vertically).
Therefore, the top and bottom background can be combined into one image and implement CSS sprites (see the image below).
![]()
Another example is website layouts. In my previous design for WebTechWise I combined the header and footer to one large image:

The content area in the middle, had the following background that repeated vertically:
![]()
The downside of CSS sprites is the lack of search engine friendly attributes. You cannot use the alt and title attributes for CSS backgrounds as you do for HTML <img> tags.
Therefore, some web developers prefer avoiding sprites and having one more HTTP request, as long as the search engines can relate to it.
However, this is more a matter of preference than a consensus.
Do you use CSS sprites in your website? For which elements? Do you use CSS sprite generators?
Additional resources
- Building Faster Websites with CSS Sprites
- Sprites Contest
- 11 CSS sprite powered demos for your inspiration

Thanks for the tips. I am not expert in web programming, so I don’t understand the example above. Do I need to specify more about the tags home, about… How does the HTML code know which style in the CSS to apply?
Thanks.
Thanks for pointing that out Tek3D.
I forgot to add the IDs in the HTML but now I have added them, so every rule in the CSS applies to the corresponding id in HTML. Thanks
I would prefer to use sprites just with “graphic-elements” like icons. Text like “HOME”, “ABOUT US”, … should be done with CSS. If you have a multilingual website in different languages, you need this sprite-image several times. Structure your Text with HTML and style it with CSS!
You are generally right to use CSS on text. However, in many cases you simply cannot, because the font is not supported in all common browsers (CSS3 will somehow give a solution to this problem, but browsers like IE are far from supporting it properly).
I’ve used sprites few times, but i find it hard to deal with all those numbers. If the speed is not critical i choose to use more image files.
Once you get used to doing sprites it’s a matter of minutes. In addition, you can still use the generators
What CSS sprite generator do you use ?
It seems the concept is something like an Image Map but with the use of CSS but the main difference is that in Image map different areas of an image will link to different pages or objects while CSS Sprites will help in combining all images so as the decrease the number of images used. Am I right? Sorry if I am wrong. I am new to HTML/CSS
Image maps may work in some cases but you have less ability to customize which area gets which background. If you look at the example of the tag cloud, it won’t work with image maps, but only with sprites. Thanks for commenting
Great Post.
This is what i’m searching for.
one question
What if i want to use sprites simply without ul,li..
??
Thanks,
Dev
The UL and LI tags are not a must. Sprites works with many other HTML tags such as Div or Span.
If you want to be search-engine friendly and still use CSS sprites, use the Shea Method, which adds one non-semantic span tag for each sprite usage. Balance the tradeoff and figure it out.
Thanks for the tip Jayvie. Can you please provide a link to a resource that explains how to make the sprites friendly for SEO?
You can apply background-position to any block element. li, div, span, body, whatever. Here are two articles that helped me out when I first started using sprites:
http://www.alistapart.com/articles/sprites
http://css-tricks.com/css-sprites
You are right, Rochelle.
@Dev – this can also answer your question.
Do sprites really speed things up? I mean.. yes, there is less images to load but you’re now loading a larger image, right?
Actually, I suppose they are quicker because one image is less stress than 5 server processes.
Lou
That’s right. The less times your browser calls the server for files, the quicker.
Lucian – I don’t use a CSS sprite generator because I always prefer to create them manually. However, there are plenty of generators around, for example, http://bit.ly/1aas9A
Thank you for the link, i also prefer to do more things by myself, but time is also very important.
Great article Omer. One of the added benefits of using sprites is less HTTP and DNS lookups, Better caching which could lead to an overall better loading experience.
Exactly right, Robert. This is why a good caching plugin is also important
That’s right. The less times your browser calls the server for files, the quicker.
This is a really helpful article complete with great examples. Thanks for sharing.
is it use the sprite for image tag is there any example. since two days i searching for this not found any example using image tag
It was good and I got to know few good things.
I have also created a CSS sprite for my site at: http://kunals.com. Here, you can see an image on top left..here I have used this CSS sprite as painting.
One can move cursor over this image and it will turn into colored picture.
Very cool