Tested using Google Chrome in Incognito mode and reloading the page with "empty cache and hard reload" each time.
I have the following html responsive image:
<img class="content-img" src="/app/uploads/2018/07/1400x750.png"
srcset="/app/uploads/2018/07/1400x750.png 1400w,
/app/uploads/2018/07/1400x750-768x411.png 768w,
/app/uploads/2018/07/1400x750-1280x686.png 1280w,
/app/uploads/2018/07/1400x750-520x279.png 520w,
/app/uploads/2018/07/1400x750-420x225.png 420w,
/app/uploads/2018/07/1400x750-340x182.png 340w,
/app/uploads/2018/07/1400x750-600x321.png 600w"
sizes="(max-width: 666px) 100vw, (max-width: 1399px) 38vw, 535px"
>
Expected Behaviour:
1. Viewport Widths 0px - 666px:
- Browser should take the full viewport pixel width e.g. 450px, and select the smallest src from the srcset where width is greater than 450px, in this case '/app/uploads/2018/07/1400x750-520x279.png'
2. Viewport Widths 667px - 1399px:
- Browser should take 38% of the viewport width e.g. 380px @ 1000px viewport, and select the smallest src from the srcset where width is greater than 380px, in this case '/app/uploads/2018/07/1400x750-420x225.png'
3. Viewport Widths 1400+ px:
- Browser should take the default of 535px and select the smallest src from the srcset where width is greater than 535px, in this case '/app/uploads/2018/07/1400x750-600x321.png'
Actual Behaviour:
Testing in Google Chrome, using dev tools inspect element on the img for all of the above examples, the resulting "CurrentSrc" in each case is:
/app/uploads/2018/07/1400x750-520x279.png (CORRECT)
/app/uploads/2018/07/1400x750-1280x686.png (INCORRECT (expecting 420px width)
/app/uploads/2018/07/1400x750.png (INCORRECT (expecting 600px width)
I'm left scratching my head, other similar questions all seem to boil this down to a Google Chrome caching issue, but I've been careful to eliminate that issue when testing and I still don't get the src images I expect.
I'm only 90% sure I've written the correct "sizes" attribute for the behaviour I want. Note that the logic is slightly complex due to lining up with responsive CSS breakpoints and trying to load sensible image widths in context.
px
insizes
is affected by the display (e.g. retina). So for example, on a MacBook Pro, whensizes
is set to200px
, the browser is actually getting the image with400w
insrcset
. I'm not sure if it's the cause of the problem. – He Yifei 何一非