Say I have the following image:
<img src="//picsum.photos/100" srcset="//picsum.photos/100 100w">
It appears that the sizes
attribute I have not included defaults to 100vw
, hence the small image is upscaled to the width of the viewport.
What do I do if I don't want this behavior, but instead want the image to default to its intrinsic size?
I would expect that this image would default to 100px in width on a normal display, and 50px on a 2x (retina) display.
If I specify my own sizes
attribute of 100px
, this doesn't solve the problem of displaying it at 50px
when on a retina display.
The reason I need this behavior is because in my system users are allowed to upload images of any size and place them on a page, and I am generating an srcset
with multiple steps up to the max size of their image, and I need a way for the image to display at the correct width given the size of the image and the pixel density of the user's screen.
Can this behavior for auto-width images be achieved using srcset
?
In my research I've found this article which addresses the issue directly. The author recommends adding the width
attribute with the maximum size of the image to revert what the sizes
attribute does to the image's intrinsic size. However, he does not address how to make this work with differing pixel densities.