Skip to main content

Reserve image space before the file downloads

Width, height and the edge case of different crops in responsive pictures

Updated 3 min readMarkdown
Reserve image space before the file downloads

Disclosure: An AI assistant generated the draft and example.

A download can change the layout even when the image fits perfectly once it arrives. Imagine a photo above a link: the page paints the link, the photo loads, and what you were about to click moves down. The fix is to give the browser enough information to reserve the right space before it knows the image’s natural size.

Use the actual aspect ratio

For an image file that is 1200 × 800 pixels, provide both dimensions:

<img src="workshop.webp" width="1200" height="800"
     alt="A workbench with a wooden frame and hand tools">
img {
  display: block;
  max-width: 100%;
  height: auto;
}

The attributes supply a 3:2 aspect ratio before the image loads. The CSS lets the image shrink with its container while preserving that ratio. At a rendered width of 600 CSS pixels, expect it to reserve around 400 CSS pixels in height. Neither attribute requires the screen to display the image at its full source size. A properly sized download is a separate question: for same-ratio files at different pixel widths, use srcset and sizes.

Use matching dimensions for each crop

A mobile crop is not always just a smaller copy. If mobile uses a portrait crop and desktop shows landscape, reserving the desktop ratio on a phone can leave the wrong-sized slot. Put dimensions on each applicable <source> as well as on the fallback <img>:

<picture>
  <source media="(max-width: 520px)" srcset="portrait.svg"
          width="600" height="800">
  <img src="landscape.svg" width="1200" height="800"
       alt="A geometric sample image">
</picture>

Check which source the browser selects at both viewport sizes. At a rendered width of 320 CSS pixels, a 3:4 slot needs about 427 pixels of height; a 3:2 slot needs roughly 213 pixels. Those values illustrate how much an incorrect ratio can move or resize adjacent content. They are not measured layout-shift scores.

When you deliberately need a fixed card crop instead, set a ratio on the card’s media wrapper and use object-fit: cover on a full-size image inside it. That makes the intended crop a layout decision, not something inferred from each image. Check that the crop still shows what the alt text describes.

A reproducible check

Save the example markup with two local image files of the stated ratios. Narrow the viewport until the portrait source is selected. Open your browser’s developer tools and inspect document.querySelector('img').currentSrc to confirm the selected asset. For network testing, serve the demo locally, disable cache and record a throttled reload with and without the dimension attributes. Compare where the text under the image appears before the image loads and after it arrives. Test both viewport widths. If your shop or CMS injects the main image in JavaScript after first paint, verify the image slot itself exists in the initial layout; correctly sizing an element that does not exist yet will not reserve space.

Fixing images will not remove shifts from fonts, embeds or late UI. Measure those separately instead of calling the whole page stable on the strength of one test.

References: Chrome’s guide to optimizing CLS and WHATWG HTML: the source element.

About the author

  • I work on SEO, website structure, content and AI-assisted workflows at Salestudia, a digital marketing agency based in Idstein, Germany. Here I share practical guides on web development, Shopify, technical SEO and analytics for small businesses.

Putting width and height on each source is the detail most guides leave out. One practical addition: when the crop changes at a breakpoint, test with the network throttled. On a fast connection the image arrives before the shift is visible, so a wrong slot goes unnoticed.