views:

127

answers:

3

On a website I'm creating, I have about 100 various thumbnails (64x64) that get displayed at different times. On some pages, only 5-15 thumbnails may be displayed. On others, all 100 are loaded.

I'm considering using a technique like CSS sprites to display the images. That is, rather than have image tags for each thumb, do something like:

<span class=thumb1"></span>

And then use CSS to take a slice of one single image with all the thumbs stitched together. That is, one image with all 100 thumbs (in this scenario, a 640x640 image).

My questions:

  • Is this a good idea?
  • If yes, should I do it on all pages the images occur, or only on the pages with all the images?
  • Is there another technique other than sprites that may be better than simply including the images with img tags?
+2  A: 

I'm not quite sure what you mean with "sprites", but this is what I think you mean: instead of 100 images seperately, you create 1 image, with a 10x10 raster. Each coordinate (x,y) contains one of the images you like to show. Now, if you display an image, you use CSS to set background-location: i.e. x * -64px and y * -64px, perhaps using JavaScript to calculate the x and y for each image-span, or Server-Side scripting to print out a dynamic CSS.

  • Yes, this is a good idea: it reduces load time, since one only has to download one big image, instead of hundreds of smaller ones.
  • It depends. If you have caching-abilities for a page, then you can "stitch" all thumbnails into one image file. If you have a very dynamic webpage, then it's quite harsh to build this stitched image every time the thumbnales are updated.
  • Not sure, if this is what you ment with "sprites", then no, if you ment something else with "sprites", then yes: this answer is an example of one.
Pindatjuh
This is what I meant by sprites, I edited the question to be clearer.The images are relatively static. Some are added/updated occasionally, but not so frequently as to be too expensive or annoying.
jeremy
+1  A: 

It is a good idea if

  • Speed matters

  • You don't care about accessibility (screen readers reading the ALT text of an image, etc, all that is gone when you use sprites)

  • You don't care that your thumbnails are not going to be printed by default in any browser

  • You can do it without it becoming a maintenance nightmare (which image was on position 461 again?)

As to your second question, it is probably advisable to put all sprites into one or very few container images to minimize loading times.

Pekka
Accessibility need not be a victim of using sprites to improve page load performance. Ideally, you would include text in the span tag from the OP and text-indent this text far off screen. This would allow it to still be read by screen readers.The other detraction points are valid though.
atxryan
Yep, CSS sprites are OK when you replace CSS background images but if you're replacing HTML images, you lose the alt attribute and a lot of information for SEO, browsers not displaying images and people relying on assistive technology like screen readers.If you want to improve performance with HTML images, move your images to images.domain.com using a Lighttpd server without cookies, etc You don't need Apache or IIS for that and browsers may download more files than while only using www.domain.com
Felipe Alsacreations
+3  A: 

If you think that an ordinary user would visit at least two different pages with these thumbnails than my opinion is that using sprites would really be a good idea!

I would in fact make a single big image with all the thumbnails and then use it in all the pages!

Why?

  • Fewer http requests (from 100 to 1)! And this is one of the most important thing about web site optimizations (read here from Yahoo Performance Team speed optimization rules )
  • This way users will download the whole image only the first time and then they will get a super quick loading of that images in all the following pages
  • The most famous websites on the internet already do that! See sprites used in Yahoo, Amazon or Youtube pages.
  • You can add other UI or layout images to your sprite

Optimize the resulting PNG:

After you have generated your sprite, if a PNG, you can optimize the PNG even more using this tool: http://www.sitepoint.com/blogs/2009/09/18/squishing-the-last-drops-from-your-pngs/

When not to use sprites:

  • When part of the images in the sprite change frequently
  • In this specific case: when the majority of users would need less than the (about) 10% of the images
Andrea Zilio
Thanks, this is a very useful answer! It was the Yahoo! performance stuff that inspired this question. I'm curious about the "10%" figure. Do you have more information about that?
jeremy
No jeremy, it's not a rule and there not exist a rule like this. It really depends on your situation and it would more correct to think in terms % of sprite size your users will need than the number of images your users will need. Anyway, in your case where you have 64x64 pixels images and pages that uses 5 to 15 or all images I think that a rule like this is OK and so the 10% is a my opinion based on your specific case. (I've specified this in the answer, I hope it's more clear now)
Andrea Zilio