I'm trying to use a (max-width: ...)
along with a default size specification in the sizes
attribute of an <img srcset...>
. However, this does not work as I'd expect. I created the following test case:
<p>
<b>Viewport width</b>: <span id="width"></span><br>
<b>Device pixel ratio</b>: <span id="ratio"></span><br>
<p>
<h3>Max-width + default (break @1600)</h3>
<img
srcset="https://via.placeholder.com/1100x200 1100w,
https://via.placeholder.com/2000x400 2000w"
sizes="(max-width: 1600px) 1100px, 2000px"
src="https://via.placeholder.com/300x400" class="full-width">
<h3>Without sizes</h3>
<img
srcset="https://via.placeholder.com/1100x200 1100w,
https://via.placeholder.com/2000x400 2000w"
src="https://via.placeholder.com/300x400" class="full-width">
What I expect the browser to do is to use the low res image below a viewport width of 1600px (or 800px on a double density screen) and the high res image in all other cases. However, both Firefox 104 and Chrome 105 ALWAYS render the high res image.
I've tested this in "Responsive" mode with network caching disabled and hard reloads every time, starting from the lowest resolution to the higher one, ensuring that the high res image wasn't loaded from the start. And as you can see that only works as expected when I don't specify any sizes
(in which case the browser simply switches when the viewport width exceeds the low res image width):
What am I doing wrong here and how do I get the expected behavior using the max-width
specification in sizes
?