Skip to content

Image in the first screen without its size

image-dimensions-missingminor
On this page

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

Wrong
<img id="hero" src="/images/hero.svg" style="width:360px" alt="نقش سدو أحمر ورملي" />
Right
<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:auto keeps them when CSS sets the width.
  • Or set aspect-ratio: 4 / 3 in CSS on the image or on a box around it.

How we detect

  1. 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: absolute or fixed).
  2. We report one without both width and height attributes and without a CSS aspect-ratio, whose box has its file's own proportions: nothing but the file gave it its size.
  3. 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.