I am using HTMLImageElement.sizes to define the width of my image.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/sizes
img {
background: orange;
border: 2px solid black;
aspect-ratio: 1/1;
object-fit: contain;
}
<img srcset="https://www.dofactory.com/img/html/vangogh-sm.jpg 120w,
https://www.dofactory.com/img/html/vangogh.jpg 193w,
https://www.dofactory.com/img/html/vangogh-lg.jpg 278w"
sizes="(max-width: 710px) 120px,
(max-width: 991px) 193px,
278px">
Although the size is set correctly depending on the viewport width the image size works only once the image stars to is fully loaded.
You can see that the image is only 0x0px during load in this video:
I am looking for a dry way which doesn’t require to define and maintain the size once in html and once in css as this is harder to keep in sync. Also the sizes attribute is required for performance reasons: Why can’t we just do this using css or javascript
Is there any trick to use the sizes already before the image has been downloaded?