Thanks to some recent changes in browsers, it’s now well worth setting width
and height
attributes on your images to prevent layout shifts and improve the experience of your site visitors.Web performance advocates have often advised to add dimensions to your images for best performance to allow the page to be laid out with the appropriate space for the image,before the image itself has been downloaded. This avoids a layout shift as the image is downloaded something Chrome has recently started measuring in the new Cumulative Layout Shift metric.
Well, a dirty, little, secret not that well-known outside the hard-core web performance advocates is that, until recently, this actually didn’t make a difference in a lot of cases,
as we’ll see below. However, adding width
and height
attributes to your <img>
markup have become useful again after some recent changes in the CSS world, and the quick adoption of these changes by the most popular web browsers.
The CSS contain
property gives you a way to explain your layout to the browser, so performance optimizations can be made. However, it does come with some side effects in terms of your layout.