srcset, do I still need to write "1x,2x,3x" if I've specified the width of each image?

Rick picture Rick · Feb 20, 2017 · Viewed 8k times · Source

should I write this

srcset="small.jpg 1x 500w, medium.jpg 2x 1000w, large.jpg 3x 1500w"

or this

srcset="small.jpg 500w, medium.jpg 1000w, large.jpg 1500w"

are the two expression the same?

Answer

dsluis picture dsluis · May 23, 2018

Your first code block is incorrect, see this MDN documentation:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Img#attr-srcset

A srcset attribute can contain either width descriptors(w) or pixel density descriptors (ex. 2x), but not a combination of both.

The second code block is correct.

Side note: the browser will automatically take into account the device's pixel density when choosing which of the three images to download.