A page can finish drawing its text before the browser knows how much space an image needs. Reserving the image's space in CSS prevents layout shift and keeps the surrounding text in place while the image loads.
An image with a known aspect ratio can occupy its final box before its pixels arrive. The reservation must match the image's proportions, with one dimension left automatic so the box can resize with the page. A fixed height that happens to fit a desktop layout can still cause trouble on a narrow screen.
Chrome DevTools reports unexpected movement through its local Cumulative Layout Shift, or CLS, metric. A zero score for one load confirms that scenario; other viewport sizes, late banners, font swaps, and animations can still expose separate shifts.
Steps to fix CSS layout shift:
- Create a file named banner.svg in your text editor with this image content.
- banner.svg
<svg xmlns="http://www.w3.org/2000/svg" width="800" height="100" viewBox="0 0 800 100"> <rect width="800" height="100" fill="teal"/> <circle cx="720" cy="50" r="28" fill="gold"/> </svg>
The image is 800 pixels wide and 100 pixels high, giving it an 8:1 aspect ratio.
- Create index.html beside banner.svg with this page structure.
- index.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Layout shift repair</title> <style> </style> </head> <body> <main> <h1>Latest story</h1> <img id="banner" alt="Teal banner with a gold circle"> <p>This text should not move when the image loads.</p> </main> <script> </script> </body> </html>
- Insert these rules between the style tags in index.html.
body { font: 16px/1.5 system-ui, sans-serif; margin: 12px; } main { max-width: 560px; margin-inline: auto; } h1 { font-size: 20px; margin: 0 0 8px; } #banner { display: block; width: 100%; height: auto; } p { margin: 8px 0; padding: 8px; background: #eef2f6; }
The image fits the available width, but its height is not reserved yet.
- Insert this delayed image load between the script tags in index.html.
setTimeout(() => { document.querySelector("#banner").src = "banner.svg"; }, 1500);
The 1.5-second delay makes the missing reservation visible even when the image is already cached. Until the source is assigned, the browser may show the image's alternative text.
- Save index.html in your text editor.
- Open the saved index.html file in Chrome.
The address begins with file:/// and ends with the location of your saved file. No web server is needed.
- Open Chrome DevTools with F12.
On macOS, the shortcut is Command + Option + I.
- Select the Performance tab in DevTools.

- Reload index.html with the browser's Reload button.
The local metrics reset on navigation. The paragraph jumps down when the banner arrives after 1.5 seconds.
- Replace the #banner rule in index.html with a rule that reserves the image's 8:1 box.
#banner { display: block; width: 100%; height: auto; aspect-ratio: 8 / 1; }
The width determines the height before the image loads. For other images, the ratio must match the actual dimensions; a different ratio can leave empty space or distort the result. Matching HTML width and height attributes also let the browser reserve an image's proportions.
Related: How to set CSS aspect ratio for responsive media - Save the revised index.html file.
- Reload index.html at the same browser size.

- Confirm that the paragraph stays in place after the banner loads with local CLS at 0.
The empty image box is already present during the delay, so the arriving banner no longer pushes the text down. A nonzero CLS or visible movement means the original symptom remains.