Back to HTML

Image Dimensions and Preventing Layout Shift

Coding Tutorials Team 8 July 2026

Content that jumps around while a page loads is one of the most irritating experiences on the web. You begin reading, an image arrives, and the paragraph you were on shoots down the screen. Worse, you go to tap a link and something loads above it, so you tap something else entirely.

What causes the jump

The browser lays out the page as it parses, and it can only lay out what it knows the size of. Text has predictable dimensions. An image does not, until either the file arrives or something tells the browser how large it will be.

Given no information, the browser reserves no space and lays out the following content immediately. When the image finally loads, it forces everything below it downwards. That reflow is a layout shift, and on an image heavy page it happens repeatedly.

The metric

Cumulative layout shift measures how much visible content moves unexpectedly during the page lifetime, weighted by how much of the screen was affected and how far it travelled. It is one of the core web vitals, which means it feeds into search rankings as well as being an obvious usability problem.

Shifts caused by a user action, such as expanding an accordion, are excluded. It is only the unexpected movement that counts, which is a sensible distinction: the problem is content moving while someone is trying to read or click it.

The fix is two attributes

<img src="/img/photo.jpg" alt="A harbour at dusk" width="1600" height="900">

Supply the actual pixel dimensions of the file, without units. Modern browsers use the ratio between them to compute an aspect ratio and reserve correctly proportioned space before the file arrives.

This is why the old advice reversed. It used to be true that dimension attributes fought with responsive CSS, so the recommendation was to omit them. Browsers now treat them as a ratio hint rather than a fixed size, so the conflict no longer exists and the attributes are purely beneficial.

The CSS that makes it work

The mechanism depends on a stylesheet that allows the image to scale.

img { max-width: 100%; height: auto; }

Height set to automatic is the part people miss. With a fixed height in CSS the aspect ratio is overridden and the reservation is wrong. With automatic height, the browser uses the attribute ratio to work out the correct height for whatever width the image ends up at.

When you do not know the dimensions

User uploaded images and content from an external source often arrive without known sizes. Two approaches cover this.

The better one is to record the dimensions when the file is uploaded and store them alongside it, so the template can emit the attributes. Any image library reports them and the cost is one database column.

Where that is not possible, enforce a ratio in CSS instead. A container with a declared aspect ratio and an image set to cover its area produces stable layout regardless of the file, at the cost of cropping anything that does not match.

Other causes of shift

Images are the most common culprit and not the only one. Advertisements and embeds inserted after load push content down unless space is reserved. Web fonts swapping in can change text height if the fallback has different metrics. Banners and notices injected at the top of the page move everything beneath them.

The general principle is the same in each case: if something will appear later, reserve its space now. For an embed that means a container with fixed dimensions. For fonts it means choosing a fallback with similar metrics and adjusting its size to match.

The aspect ratio property

CSS now has a direct way to declare a ratio on any element, which is useful for containers holding content of unknown size such as embeds or user uploads. Declaring the ratio reserves the correct proportion of space regardless of what eventually fills it.

This complements rather than replaces the width and height attributes on images. The attributes work during initial parsing, before the stylesheet has necessarily arrived, which is precisely when the reservation is most valuable.

Measuring it

The performance panel in browser tools can record layout shifts and highlight which elements moved. Lab tools give you a score in a controlled environment, and field data from real visitors is the measure that actually counts, since a fast local connection hides problems that a slow one exposes.

Test on a throttled connection. Many layout shifts are invisible when everything loads in fifty milliseconds and painfully obvious on a poor mobile connection, which is where a good share of your visitors actually are.