Images for Responsive

The type of image should influence your choice of file format. If its a photo or similar type of image, you are maybe better sticking with jpg (if you don’t need alpha). PNG is very efficient (and loss-less :smiley: ) for images with lots of flat colour, like icons logos and graphics. They are not so efficient with photo-like images, with lots of detail or where neighbouring pixels rarely match. SVG’s are another alternative to png.

The point Paul was making was that if you change your browser window size, you end up loading multiple versions of an image, which is inefficient. I imagine the same would happen if you used media queries to dish out different images. Perhaps if you used device-width in the query (which @RyanReese always advises against) it may only load one image per device, but not using device-width for general queries for the reasons Ryan gives, but just ones for images. I have not tried this, just an idea. What do others think?

http://c2.com/cgi/wiki?PrematureOptimization

Above is a a very old article well worth reading, although it is not specifically about image optimization it does stress about measuring to highlight performance bottlenecks.

Once the web-page is available there are numerous free tools available with detailed explanations on how to increase performance.

It’s not just me. @PaulOB recommends staying away from device-width as well. There is NO reason to go down that road. I wish it didn’t even exist :frowning: .

http://timkadlec.com/2012/04/media-query-asset-downloading-results/

Look at test 4 / results.

Results are sort of spotty but just doing a general override in CSS to serve up different images seems to be the best to save bandwidth.

Although that test was done a few years ago so perhaps it’s gotten better. I’d really have to assume it is better.

Just write regular width media queries (not device-width!) and serve up different images per screen size.

Yes, I know yo don’t like it (read your article), I was just throwing up an idea and wondered if it would work. To use device width only for selecting an image size to send the user.
So you have a big monitor, you get a big image, you mess with the browser width and the same big image stays, no more smaller ones get downloaded.
Or you have a small mobile, you get small images and no big ones.

1 Like

You should just be able to swap out background images via media queries regularly and browsers should only download the one that will actually be served up to the user.

It was in response to Pauls comments about the picture element:

I assumed that media queries would have the same effect, loading 3 different versions of an image if you change the browser size. Maybe its not even a problem, but it got me thinking.

Ah. Yeah device-width might have a use there, but we have to consider the fact that 99% of users probably won’t resize their browser. If they do, they are probably split screening it or something, and in which case, they are probably on a desktop where bandwidth isn’t an issue (and if they do resize it like split screening, they probably don’t want the page to break by not serving up a different image because you did device width).

All in all, seems like something I’d avoid. I’d disagree with Paul here and say they aren’t adding to the problem.The loading problem Paul mentioned probably won’t have any effect on the average user.

Yes if you swap the background images you will get the browser loading three images if the user resizes their browser.

This is where I have issues with all these picturefill and image swapping techniques as it assumes the device will open at one size. Although, as I mentioned before this is OK for mobile because they can’t resize (unless landscape triggers another image of course) but desktops will now have 3 times the weight as I believe that very few people do not adjust their browser during a session (although I have no concrete info to back this up).

I have done background images like that. But one thing I was not sure about was if mobiles would load the big image too. The css being desktop first, I apply the big background to the default css, not conditioned by any query. Then the small image in the max-width query.

body {background-image: url(../images/bigpic.jpg);}
@media screen and (max-width: 600px) {
         body {background-image: url(../images/smallpic.jpg);}
}

But I suppose the browser reads the whole css file before loading only what it needs to, is that right?

They shouldn’t. From the link I provided earlier, mobiles are pretty decent with it. And I can only assume it’s gotten better in the past 3 years.

Fill me in here what you are talking about. I thought you lectured me in another thread to use device-width in my view port code?

No, use “width” not “device-width”.

I’d love it if you could find a post with me advocating that :wink: .

2 Likes
    <meta name="viewport" content="width=device-width, initial-scale=1">

Yes, you need to include that.

I’ve never not advocated putting that in. My article actually reminds people to do that.

He means in media queries, like this:

@media screen and (max-width: 600px) {}

Not like this:

@media screen and (max-device-width: 600px) {}
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.