← Back to HTML & CSS, page 3

html-css-0050 · Hard senior

How do aspect-ratio, image dimensions and reserved space prevent layout shift?

Layout shift happens when the browser did not know a box's final size at first layout. Set width and height attributes on images, use aspect-ratio for embeds and placeholders, reserve space for late content, avoid inserting banners above content, and match fallback fonts. Cumulative Layout Shift measures the effect.

Full answer

Layout shift happens when content already on screen moves because something above or around it changed size, for example an image that finishes loading, an ad slot that fills, or a web font that swaps in. Cumulative Layout Shift (CLS) is a Core Web Vitals metric that scores it. The cause is nearly always that the browser did not know the final size when it first laid out the page.

The fix is to tell the browser the size or the shape in advance.

  • Images and video: set width and height attributes that match the file's pixel size. Browsers use them to compute an aspect ratio before the file arrives, so with max-width: 100%; height: auto in CSS, the box has the right height straight away.
  • Any box that will get content later (embeds, maps, cards with images): use CSS aspect-ratio, for example aspect-ratio: 16 / 9, or a min-height.
  • Banners and notices: reserve their space, or insert them below the current viewport. Do not push content down after the user starts reading.
  • Fonts: choose font-display carefully, preload key fonts, and use a fallback with similar metrics so the swap does not reflow text. size-adjust and related descriptors in @font-face help match them.
  • Animations: change transform, not top or height.

In a 400px column, the photo is 200px high (600 by 300 scaled down), the embed placeholder is 225px high (400 divided by 16 and times 9) before any video loads, and the thumbnail is a 100px square with its picture cropped by object-fit: cover.

To find shifts, open the Performance panel and look at the layout shift entries, or use PerformanceObserver with the layout-shift entry type. Chrome's Lighthouse reports CLS too. A good target is a CLS of 0.1 or less.

html
<style>
  body { margin: 0; width: 400px; font-family: sans-serif; }
  img { max-width: 100%; height: auto; display: block; }
  .embed { aspect-ratio: 16 / 9; background: #333; color: #fff; }
  .thumb { aspect-ratio: 1; width: 100px; object-fit: cover; }
</style>
<img id="photo" width="600" height="300" alt="Campus"
     src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='600' height='300'%3E%3Crect width='600' height='300' fill='%2399ccff'/%3E%3C/svg%3E">
<div class="embed" id="embed">Video placeholder</div>
<img class="thumb" id="thumb" width="300" height="200" alt="Lab"
     src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='200'%3E%3Crect width='300' height='200' fill='%23cc99ff'/%3E%3C/svg%3E">

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Cause: content arrives after layout and pushes other content.
  • width and height attributes give images an aspect ratio before loading.
  • aspect-ratio reserves space for embeds and placeholders.
  • Reserve space for ads and banners; do not insert above what the user reads.
  • Check shifts in the Performance panel or with a layout-shift observer.

Common mistakes

  • setting an image's CSS to width: 100%; height: 300px with no regard for the ratio. The image is then stretched or the layout is wrong at some screen widths.
  • lazy-loading the image at the very top of the page. Above-the-fold images should load early. Lazy-load the ones below.

Follow-ups

  • does CSS aspect-ratio replace the width and height attributes on img?
  • How do web fonts cause layout shift and what reduces it?
  • Why should above-the-fold images not be lazy-loaded?

Tips

  • Make width and height attributes part of the image component, so every image reserves space without anyone remembering.
  • Say the metric (CLS) and its target of 0.1 or less, then the fixes in order of how often they matter.
  • Describe one example from images and one from fonts.