CSS Sprites were first introduced in 2004 by Dave Shea in the article CSS Sprites: Image Slicing’s Kiss of Death. The word “sprite” derives from the old 8-bit days where bitmaps were moved around the screen in games. They were called sprites and a common technique was to gather them all into a grid on a large image, where the code could pull out the desired one by their respective coordinates.
So instead if loading a lot of small images, we just load one big image. This will reduce the number of HTTP requests, which in turn improves page loading speed and may reduce undesired flickering effects.
How to implement this? The trick is clever use of the CSS property “background-position”. Have a look at this simple example markup:
And the following CSS:
The image nav.gif contains a grid of different bitmaps. In short, the background position is shifted to use various bitmap positions in the grid image. The code above is very simplified, but you get the main idea. Read Dave Shea’s article for a detailed description of the technique.
There are also other ways to use it. Douglas Bowman wrote an article in 2003 entitled Sliding Doors of CSS where he created flexible CSS tabs. This can be combined with CSS sprites, have a look at Sliding Doors Meets CSS Sprites. You can also create CSS-based image maps.
As always, apply common sense since the technique is not always an improvement. Some images may be difficult to combine in a grid and be careful with variable-width elements such as input fields. If the grid image is very large, it could cause a long download time as well as wasting browser memory while decompressing. It can also be a pain to make it work in every browser and it may increase complexity in your web project.
Related posts
Comments
No comments yet.
Leave a reply