https://jakearchibald.com/2022/img-aspect-ratio/ Jake Archibald wrote...who? Avoiding layout shifts: aspect-ratio vs width & height attributes Posted 11 July 2022 By default, an takes up zero space until the browser loads enough of the image to know its dimensions: When you run the demo, you'll see the
immediately. Then, after a few seconds, this paragraph and subsequent page content shifts downwards to make room for the image. This makes the user experience massively frustrating, as content moves out from under the user's eyes/finger/pointer. For over a decade, we had to use silly hacks to manually apply an aspect ratio, and then, bloody typical, two better solutions arrived at roughly the same time. They are CSS aspect-ratio, and width & height presentational hints. So, which should you use? First, let's take a look at how the features work, as there's quite a bit of misinformation out there... CSS aspect-ratio If you do this: .aspect-ratio-demo { aspect-ratio: 16 / 9; } ...you get this: 16 / 9 This feature became cross-browser compatible once it landed in Safari 15, late 2021, having arrived in Chrome & Firefox earlier that year. It works on any element, but here's a demo with : img { aspect-ratio: 16 / 9; width: 100%; } This time, the image reserves space for its content as soon as it appears in the document, so stuff doesn't shift around once it loads. The other solution is... Width & height presentational hints If you set dimensions on your image: ... And set height to auto: img { height: auto; } ...the image will have an aspect ratio applied, even before it loads. This landed in Chrome and Firefox back in 2019, and became cross-browser compatible when it landed in Safari 14 a year later. So, this feature has been around a little longer than CSS aspect-ratio. In addition to , this feature also works on