Mink Machine

CSS Sprites

CSS Sprites was first introduced in 2004 by Dave Shea in the article CSS Sprites: Image Slicing’s Kiss of Death. At least it was the first time I read about the technique, and went “whoa”.

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.

CSS Sprites are used on many high-traffic sites today. For instance, have a look at the grid image used by Google:

How to implement this? The trick is clever use of the CSS property “background-position”. Have a look at this simple example markup:

<ul id=”nav”> <li><a class=”item1″ href=”#”>Item 1</a></li> <li><a class=”item2″ href=”#”>Item 2</a></li> </ul>

And the following CSS:

#nav li a {background-image: url(“nav.gif”)} #nav li a.item1 {background-position: 0px 0px} #nav li a:hover.item1 {background-position: 0px -50px} #nav li a.item2 {background-position: 0px -100px;} #nav li a:hover.item2 {background-position: 0px -150px;}

Where nav.gif contains a grid of different images. In short, the background position is shifted to use various image positions in the grid image. The code above is very simplified, but you get the main idea. Read Dave Sheas 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.

[2006]

Featured stories

Wazzup in Vaduz

Wazzup in Vaduz

"Vaduz Castle is overlooking the town from a hill, a short walk from the center. It’s really a postcard view with the alps in the background, which I’m sure the prince enjoys as he sips his morning coffee while towering above his loyal subjects."
The fairytale castles of Sintra

The fairytale castles of Sintra

"The Portuguese town of Sintra is famous for many beautiful castles. Palácio Nacional da Pena is arguably the most stunning of the lot. "
Eating my way through Basque country

Eating my way through Basque country

"Not too bad to wash it all down with a local Txakoli, the young white wine typical of the Basque country."
Chilling in Chile

Chilling in Chile

"My weary feet has reached Valparaiso. After almost a month of traveling across the South American continent I realize that this is as far south as I will come, due to weather conditions."
Bunker safari

Bunker safari

"The area contains 54 excavated bunkers along with gun emplacements, scattered around the town’s most visible landmark."
Visions of Singapore

Visions of Singapore

"By extensive use of landfill with sand bought from Indonesia and Cambodia, the bay area has expanded greatly in the last few years and tall buildings pop up everywhere."

Instagram

Seville
Venice
Gothenburg
Fever Ray
Rome
Basel
Singapore
Kuala Lumpur