Css sprites sound tough but they aren't, not here, not in this article. I want you to make one, I want a faster technorati, I want a faster hypem.com! Just one. Now your website visitors only have to download 1 image instead of 3-15! I do that for you guys and girls, won't you return the favor?
That's it! Your own Image Sprite!
Just a couple images pasted together in a photoshop file. Now that we understand the simplicity of a sprite, lets focus on using one, and making that as easy as possible, none of that tricky CSS stuff that I love.
Ok before you have CSS, you have XHTML. This is the XHTML that IS my askapache logo at the top left of this screen. It is conveniently a link to my home page. All you need to notice from this example is the id="apache"
. That is how we can control that particular tag from within CSS.
This css explains that the background-image to be displayed for the #apache link element is exactly 160x27 pixels and is 81pixes in from the left of the image css-sprite.
#apache {width:160px; height:27px; background:url(https://www.askapache.com/s/s.askapache.net/i/css-sprite.png) -81px 0 no-repeat; margin:0; padding:0; overflow:hidden;display:block; position:absolute; top:10px; left:10px; }
Ok so that should get you started, or if that makes you happy enough you are good to go!
See now why I said it would be easier if you keep your images all aligned at the top? This way you only have to figure out the distance from the left as well as the actual image dimensions. But as my sprite example shows, you don't have to do it the easy way, but just coming this far you've accomplished more than you realize, and probably gave you an advantage most of your competitors don't have. No doubt. Congratulations.
But, what if you are a stickler for semantically ideal coding guru and you don't like using background-images on an a element without using an image element? First, chill :p
I experimented a little bit on this awhile ago, and discovered a simple but IMO very elegant solution. To get it to work, use a 1x1 transparent .gif image and set that as the source of your image, everything else discussed above will then work.
Not many. Because you should only use this for your most requested images that are more design elements than something like a photo of your dog.