One issue I encountered with the <source>
element is that even when specifying the width
and height
attributes for both the <source>
and image fallback, there is still significant layout shift. This could be due to using images with varying dimensions, resulting in noticeable differences in size.
To provide a simplified example:
<picture>
<source
sizes="(min-width: 900px) 900px, (min-width: 640px) 600px, (min-width: 340px) 300px, calc(100vw - 56px)"
srcset="/images/demo-300.avif 300w, /images/demo-600.avif, /images/demo-900.avif 900w"
type="image/avif" width="300" height="400" />
<img src="images/demo-300.jpg" width="300" height="400" />
</picture>
Unlike the <img>
tag where specifying the intrinsic size helps browsers allocate space for rendering, this approach doesn't seem to work with the <source>
tag. Are there any suggested workarounds to address this behavior?