Why it matters
- A browser lays out the page before its images arrive. An image whose size it does not know takes no room until then; when it loads, everything below it moves down.
- That jump is a layout shift, which Cumulative Layout Shift (CLS), one of the Core Web Vitals, measures. On a phone it moves the button someone was about to tap.
- web.dev says: "Always include width and height size attributes on your images and video elements", or "reserve the required space with CSS aspect-ratio or similar".
Example
<img id="hero" src="/images/hero.svg" style="width:360px" alt="نقش سدو أحمر ورملي" />
<img id="hero" src="/images/hero.svg" width="400" height="300" style="width:360px;height:auto" alt="نقش سدو أحمر ورملي" />
How to fix
Write the file's own width and height, and let CSS scale the image:
html
<img id="hero" src="/images/hero.svg" width="400" height="300" style="width:360px;height:auto" alt="نقش سدو أحمر ورملي" />- The attributes give the browser the proportions;
height:autokeeps them when CSS sets the width. - Or set
aspect-ratio: 4 / 3in CSS on the image or on a box around it.
How we detect
- We render the page at phone size in each engine and look at the images in the first screen, 32 pixels or more each way, that are part of the page's flow (not
position: absoluteorfixed). - We report one without both
widthandheightattributes and without a CSSaspect-ratio, whose box has its file's own proportions: nothing but the file gave it its size. - An image whose CSS gives it other proportions, such as a fixed height with
object-fit, keeps its space and is not reported. Images further down are not judged.