Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use a sprite sheet rather than individual images?

One thing I have noticed on some sites is that they use one BIIIIIIIG image containing lots of little images, then use CSS background-position to define the coordinates of each image, rather than use individual images.

Here's where I'm at:

Cons for using big sprite sheet

  • Need to load a large image to just display even one small image
  • Need to write (or generate) a long stylesheet with a class for each image
  • Cluttering CSS with lots of class definitions may impact performance
  • If one image changes (or another is added), cache problems may be encountered both on the image and the CSS associated with it
  • Requires a <div> with proper styling (display: inline-block; width: 32px; height: 32px; background-image: url('spritesheet.png');) which adds yet another class to the mix.
  • Many more that I can't remember now I'm typing them.

Pros for using big sprite sheet

  • ... Erm... none yet.

In fact the only thing that comes close to a pro here is that when I cut up the sheet into individual images the resulting folder took up 3Mb on disk (due to each file being <100 bytes and my allocation size being 4k). The actual files themselves come out less than half the size of the sheet and CSS, so even with the overhead of an HTTP request there is a significant space saving.

Basically, my question is: Does anyone have ANY pros for using a big sheet over individual images?

like image 399
Niet the Dark Absol Avatar asked Nov 08 '11 12:11

Niet the Dark Absol


People also ask

What advantage do sprite sheets have over individual sprites?

Benefit of sprite sheet Is that It loads data once only so the loading spike is only at the beginning while an individual sprite animation would require loading dynamically, spikes at each frame.

Why do people use sprite sheets?

Sprite sheets increase the performance of your game and reduce the loading and startup time. The game uses a few big image instead of hundreds of small images. This also allows sprite batching — the rendering system draws the sprites with a few draw calls instead of sending isolated commands for each sprite.

What is the difference between a sprite and an image?

2 Answers. An image is an umbrella term denoting any picture file used either as a texture or a sprite. A Sprite is the image used as the visual part of a moving object.

What is the benefit of using image sprites?

The main advantage of using image sprite is to reduce the number of http requests that makes our site's load time faster. The images also load faster making switching from one image to another on some event much smoother. Image Sprite are a collection of images placed into a single image.


1 Answers

The aim is to reduce HTTP requests. In addition, sometimes the compressed sprite will weigh less than the original images.

Recently I had a website with a lot of transparent gradients (white to trans, grey to trans) and some black and white on transparent images. By putting them all in a sprite and reducing the colors in the png to 8 I could get the sprite to be smaller in filesize than the original images (just... it was about a 0.5% saving). By reducing the number of HTTP requests from 10 to 1 though meant the site loaded faster (if measuring time from first connection to all data transferred).

In that case, a measurable increase was found.

I agree though that it's possible to mess things up and to end up with a larger sprite than needed, especially if you aren't using PNG compression.

Note two years after posting this – if you are using SSL, you should look into SPDY (my note in a further two years will mention HTTP 2.0 instead of SPDY!). SPDY negates the benefits of spriting.

like image 108
Rich Bradshaw Avatar answered Oct 08 '22 18:10

Rich Bradshaw