For each post on the website there will be a headline. This image will have a width of 100vw and a height of 300px . I would like to provide several versions of this image using the srcset attribute of the srcset tag or the fully functional picture element (not sure if at this point, therefore, this question).
Doing this would be fairly easy with the srcset attribute:
<img srcset="lg.jpg 1200w, md.jpg 992w, sm.jpg 768w, xs.jpg 320w" sizes="100vw" src="xs.jpg">
The problem arises when I want to consider various proportions of pixels of the device. First, suppose I have 2x and 3x versions of all the images listed in srcset above.
Say I have a 2x phone with a 320px (e.g. iPhone 5). I would like the browser to load xs_2x.jpg . Similarly, let's say I have a 1x desktop with a widescreen monitor. I would like the browser to load lg.jpg .
But now let me say that I have a larger phone, one with a 3x , 414px -wide viewport (i.e. iPhone 6 Plus). In this case, I would like my browser to download sm_3x.jpg . But my browser will most likely download sm_2x.jpg , because the width of sm_2x.jpg (1536 pixels) is closer to the width of the phone (1242 pixels) than the width of sm_3x.jpg (2304 pixels). This will cause an image that is not 300px high on the device to violate the website requirement.
How can I implement dynamic, static heights, DPR-conscious responsive images?
source share