CSS - using one background image with multiple images on it

Roman picture Roman · Mar 4, 2011 · Viewed 23.4k times · Source

I've observed that often the websites use only one background image which contains multiple images on it. For example, instead of using separately icons, all of the icons are put on one image and then the different parts of image are used in different section.

  • Is there any advantage to this?
  • How can this be used?

For example, for the following Stack Overflow sprite, how would I display just one of the images?

Stack Overflow Sprite Example

Answer

vbence picture vbence · Mar 4, 2011

The technique is called CSS Sprites. Basically you use CSS's background-position property and fixed height or width for your element.

If your elemnts are fixed width and fixed height at the same time you can freely create a more compact image. See this site for more complex examples.