11
votes

Our client wants to have a different banner image on smaller screens than on larger screens. Not just shrink/stretch to fit, but actually substitute a different image. The full-size image is fairly complex -- several people, two logos, and some decorative text -- and so for the smaller image they want to crop out some of the people, drop the logos, etc. So they want the biggest, most complex image for desktops, a smaller simpler image for mid-size devices, and then smaller and simpler still for the smallest.

What is the best way to do this?

My first thought is to include all three images, and use @media min/max widths to make two of them invisible at any given size. Like:

@media (max-width: 400px) { .smallimg {display: block} .midimg {display: none} .bigimg {display: none} }
@media (min-width: 401px) and (max-width: 700px)  { .smallimg {display: none} .midimg {display: block} .bigimg {display: none} }
@media (min-width: 701px)  { .smallimg {display: none} .midimg {display: none} .bigimg {display: block} }

This ought to work but it would download all three images every time, which seems rather a waste of bandwidth.

I could change the images from img tags to css background images. Would that be better? Would that avoid downloading all three or would it still do that?

I was thinking of writing some JavaScript to dynamically update the URL in the img tag based on the screen size, but that seems like a bunch of complexity.

I briefly thought of making the logos and text separate images and breaking the actual picture into pieces and then trying to assemble them all as overlapping images. But that sounds like a lot of work to get right, and then I'd have to make sure it looks right at all possible sizes, not so easy to just shrink or stretch, etc. And while it's probably do-able in this particular case, I'd prefer a general solution that I can use in the future.

Anybody done something like this and have some ideas about the best way to do it?

2
Why don't you test it? Just set some images as backgrounds and hide them. Then you can check from the browser whether you are actually doing the request for the hidden div or not. - Francisco Presencia
@FranciscoPresencia Using background images will not load the images if the block is hidden (see stackoverflow.com/questions/12158540/…). - Albert Xing
@Jay If the smaller images are "crop"s of the largest image you can load the largest image as the background of the banner once and use background size and background positioning for different screen sizes to prevent loading of several images. - Arbel
Could you change the z-index or opacity of each image? That way they don't have to reload due to 'display: none'. Just a thought. - Kevin

2 Answers

4
votes

you may use css background in media queries. The images are only loaded if the CSS requires it to be, so if nothing matches the selector then it won't bother loading the image.

This article will surely help you.

1
votes

I would checkout Zurb Foundation's Interchange for handling responsive images. The best cross browser approach i have found.

http://foundation.zurb.com/docs/components/interchange.html