One of the most effective ways of speeding up a website’s render time is to reduce the number of HTTP requests required to fetch content. An effective method to achieve this is via CSS sprites, which is combining multiple images into one large image and using CSS to only display parts of it.
Here’s an example sprite:
The purpose of this post is not to show how it makes a site faster, but cover some best practices when creating a CSS sprite.
Don’t wait until you are done slicing to begin spriting.
The problem with waiting until you’ve built the site is all your CSS and images have already been created. You will have to go back and re-write your CSS. You’ll also spend a ton of time in Photoshop attempting to fit 20-30 graphics into a sprite at once, which is extremely painful and tedious. It’s much easier to build the sprite step by step.
Arrange images opposite of how they will be displayed
This tip is a little confusing and I didn’t learn this until I was halfway through creating a large sprite. If an image is supposed to appear on the left of an element:
Put that image to the right of the sprite (see sprite image from above). This way when you move the background image via CSS, there is no possible way any other image will display next to it. A common problem when creating CSS sprites is positioning images so they don’t appear as part of a background for the wrong element.
Avoid ‘bottom’ or ‘right’ in CSS when positioning
When positioning a CSS sprite behind an element, it’s very easy to use background-position: bottom -300px; or background-position: right -200px;. This will work initially, but the problem with it is once you expand your sprite in width or height, your positioning will be wrong as the images are no longer at the bottom or right of the sprite. Using explicit positioning in pixels avoids this issue.
Give each image plenty of space
As you can see from the example sprite, many tiny images are given lots of space. Why not just cram them all together to make the sprite smaller? Because the elements they are used on will most likely have variable content and need that extra padding so other images don’t show up.
Each list item has a graphical number as a background image. If you look at the sprite shown above, you can see how the images were staggered so that if the content increased, no other images would show up.
Don’t worry about sprite pixel size
Chances are if your site is decently designed, you will have a lot of images to put in your sprite. And you’ll need a pretty large sprite to space the images out appropriately. And that’s fine. Empty space in a sprite does not take up much of a file’s size. The sprite used for addons.mozilla.org is 1,000×2,000 pixels and is only 16.7kb!
Got other tips? Leave a comment!
NM wrote on :
rdoherty wrote on :
Dorus wrote on :
Bill wrote on :
Stuart Parmenter wrote on :
Vlad Vukicevic wrote on :
Mark Caudill wrote on :
Vlad Vukicevic wrote on :
morgamic wrote on :
morgamic wrote on :
Jim Plush wrote on :
morgamic wrote on :
Jim Plush wrote on :
Arun J wrote on :
Arun wrote on :
morgamic wrote on :
Maurice wrote on :
IS wrote on :
Mark Caudill wrote on :
rdoherty wrote on :
Boris wrote on :
morgamic wrote on :
Fabrizio Calderan wrote on :
Xarr wrote on :
Xarr wrote on :
Dorus wrote on :
Wolf wrote on :
Agent Scorpion wrote on :
Frato wrote on :
Andreas wrote on :
Joseph Hsu wrote on :
Gopal Raju wrote on :
Phil wrote on :
art wrote on :
ksieke wrote on :
ksieke wrote on :
Fred wrote on :
Melissa Church wrote on :
Steve-o wrote on :
Social Engine Templates wrote on :