CSS Sprites/The Good, The Bad and The Ugly
Join the DZone community and get the full member experience.Join For Free
last week i wrote up a little initiation on the css background position property, this week i'll use that to demonstrate the existing trouble with css sprites (or at least, describe what's bugging me about them). but before i get to that, let's see why css sprites are so popular these days and why people are pushing so hard to make use of them.
i'm not going to explain the whole sprite concept in full, if you want to read more about it there's a solid article on a list apart that's more than sufficient. basically, what happens is you add multiple images in one big image file (called a sprite) and you only show the right image in the right place.
there are mainly two main reasons for using css sprites. the first and most important reason is to get rid of a very annoying graphical glitch. when you change a css background image on hover you might witness a quick glitch as the change of image is not instantaneously. this is due to the fact that the image is actually loaded on hover and can't be show until it is fully present. using a css sprites fixes this as the hover state is already included inside the sprite and thus loaded from the start.
a second reason for using sprites is that they are better for loading times compared to the separate images included into the sprite. there is of course a little overhead when loading the page for the first time, but that is countered by the fact that css sprites reduce the number of http requests. and so using sprites will often result in a positive effect on the loading times of your page.
if this sounds interesting, it definitely is. but then there's the part where you need to handle the sprites in css to show up in the right spot. that's where the fun ends.
making sprites behave
sadly css is not really equipped to handle sprites well. even though the idea is terrific css lacks the most basic methods for dealing with sprites, ie clipping the needed part from the image. in css you can only show and not show the entire image on a certain element. so all you can do is make sure the unneeded images in your sprites are hidden away by other elements or outside the container's viewport. which is easier said than done.
another issue with sprites is that they are hard to integrate into an "em" design as distances in sprites are always defined in pixels, never in ems. this makes it very hard to predict how big an area will be in pixels.
hiding unwanted parts
if we want to hide unwanted parts of our sprite there are not many options. the only case in which is works flawlessly is when the container has a fixed pixel height and width. in this case, knowing how to assemble your sprite is easy. just make sure you reserve the maximum space needed in the design for each image in the sprite. sadly, these cases are rare and most of the time you have no idea how large an area might become.
the second and most popular option is to make sprites that are large enough so the other sprite images won't show up. this leads to 2000x2000 images which are in fact not huge in size but are a downright bitch to manage. to each his own but i hate working like this. and even then, you are never sure that they will always hide the remaining images as designs might change and em designs are built to grow with font-sizes. so while this method does work in most cases, it is never fool proof and quite a mess to manage.
a final method is to hide the remaining sprite images outside the viewport. sadly we are limited by the css background-position which only allows us to work in the upper left corner. even though it is possible to set css background images in the bottom and right corners, there is no way to do the precision work using negative values to hide the other images in our sprite. so unless you need your sprite somewhere in the upper right corner, this method is pretty useless.
if you do want to use a sprite for the upper left corner the safest setup is the one described above. by using these simple css rules you can effectively hide the unused images in all situations. with a proper implementation of the css background property all four areas could've been used to increase the number of images put into one single sprite, sadly that's only a fantasy for now.
the concept of css sprites is wonderful, sadly there is no good way of implementing them (yet). the next article will look at several (yet unsupported) methods that might aid us in the future to improve the implementation of sprites. after that i might reveal one additional method which is ugly as hell but does work in some cases. not something i'd ever do so still considering whether i find it interesting enough to explain.
Published at DZone with permission of Niels Matthijs, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.