How to adjust your images for retina/hi-res displays by using srcset

By adding the srcset attribute to your img elements, you can indicate to the browser that an image is available in other resolutions.

I've saved the same image three times: one with a width of 500 pixels, one with a width of 1000 pixels, and the last one at 2000 pixels wide.

for reference: the three images side by side, displayed at the same width.

saved at 250 pixels wide
saved at 500 pixels wide
saved at 1000 pixels wide

And now, the image with the "srcset" attribute

displayed at 500 pixels wide or less (max-width: 500px).

compare to the three images above.

The html looks like this:

<img src="etc/images/stainedglass-250.jpg" srcset="etc/images/stainedglass-500.jpg 500w, etc/images/stainedglass-1000.jpg 1000w, etc/images/stainedglass-2000.jpg 2000w">

You write srcset with the structure "filename-1 image-width, filename-2 image-width, etc." and the browser does the rest!

The browser will choose the image which has the best resolution for the size displayed and the screen's ppi (pixels per inch).

This is useful because:

And so everybody's happy! (mostly)

For more info, check out css-tricks!