tags:

views:

1049

answers:

11

So I was looking at the facebook HTML with firebug, and I chanced upon this image and came to the conclusion that facebook uses this large image (with tricky image positioning code) rather than many small ones for its graphical elements. Is this more efficient than storing many small images?

Can anybody give any clues as to why facebook would do this.

+18  A: 

The theory behind that is explained here: http://css-tricks.com/css-sprites/, also have a look here, as it underlines why do you really need one big image. I also say that all the article is interesting :)

Alberto Zaccagni
the link to developer.yahoo.com is excellent. +1
Jim Schubert
+1 for the good links
Matt Joslin
A: 

Simple answer, you only have to 'fetch' one image file and it is 'cut' for different views, if you used multiple images that would be multiple files you would need to download, which simply would equate into additional time to download everything.

Cutting up the large image into 'sprites' makes one HTTP request and provides a no flicker approach as well to 'onmouseover' elements (if you reuse the same large image for a mouse over effect).

Jakub
+29  A: 

These are called CSS sprites, and yes, they're more efficient - the user only has to download one file, which reduces the number of HTTP requests to load the page. See this page for more info.

ceejayoz
+2  A: 

Opening connections is more costly than simply continuing a transfer. Similarly, the browser only needs to cache one file instead of hundreds.

Nerdling
A: 

Google also does it - I've written a blog post on it here: http://www.stevefenton.co.uk/Content/Blog/Date/200905/Blog/Google-Uses-Image-Sprites/

But the essence of it is that you make a single http request for one big image, rather than 20 small http requests.

If you watch a http request, they spend more time waiting to start downloading than actually downloading, so it's much faster to do it in one hit - chunky, not chatty!

Sohnee
+4  A: 

Since other users have answered this already, here's how to do it, and another way is here.

graham.reeds
A: 

Css Sprites tecnique is a method for reducing the number of image requests using background position. Best Practices for Speeding Up Your Web Site

CSS Sprites: Image Slicing’s Kiss of Death

RioTera
+1  A: 

One of the major benefits of CSS sprites is that it add virtually 0 server overhead and is all calculated client side. A huge gain for no server side performance hit.

Barfieldmv
+15  A: 

The problem with the pro-performance viewpoint is that it always seems to present the "Why" (performance), often without the "How", and never "Why Not".

CSS Sprites do have a positive impact on performance, for reasons that other posters here have gone into in detail. However, they do have a downside: maintainability; removing, changing, and particularly resizing images becomes more difficult - mostly because of the alterations that need to be made to the background-position-riddled stylesheet along with every change to the size of a sprite, or to the shape of the map.

I think it's a minority view, but I firmly believe that maintainability concerns should outweigh performance concerns in the vast majority of cases. If the performance is necessary, then go ahead, but be aware of the cost.

That said, the performance impact is massive - particularly when you're using rollovers and want to avoid that effect you get when you mouseover an image then the browser goes away to request the rollover. It's appropriate to refactor your images into a sprite map once your requirements have settled down - particularly if your site is going to be under heavy traffic (and certainly the big examples people have been pulling out - facebook, amazon, yahoo - all fit that profile).

There are a few cases where you can use them with basically no cost. Any time you're slicing an image, using a single image and background-position tags is probably cheaper. Any time you've got a standard set of icons - especially if they're of uniform size and unlikely to change. Plus, of course, any time when the performance really matters, and you've got the budget to cover the maintenance.

If at all possible, use a tool and document your use of it so that whoever has to maintain your sprites knows about it. http://csssprites.org/ is the only tool I've looked into in any detail, but http://spriteme.org/ looks seriously awesome.

Iain Galloway
I myself split my image sprites to several files for maintainability.
vsync
Why would you manually create a sprite map? Anyways, let's say you did. Rather than rearranging everything, add your new stuff at the edge of the picture so no measurements are impacted.
Nerdling
@Nerdling: Because just about every article people have linked to here tells you to? Anyway, it's less the sprite map, and more the background-position tags that are a pain to maintain. Sure, add new stuff at the edge, but what happens when the client wants a button that ended up in the middle of the map to be bigger?
Iain Galloway
What's so hard to maintain about them? Are you updating them manually? Automate it! You should be able to write a build script which will take a set of images, merge them into a sprite map, and then spit out the relevant CSS to use them.
Yuliy
Absoloutely, hence me linking the tools. I just don't want people to read the linked articles and get the wrong idea.
Iain Galloway