Images without dimensions make the page jump as they load. Learn how width, height and aspect-ratio reserve space, and when to lazy-load or prioritise an image.
The short answer
#Give every <img> a width and height attribute that match the image's real proportions, and let CSS keep it responsive with height: auto. The browser uses the two numbers to work out the aspect ratio and reserves the right amount of space before the file arrives, so nothing below it moves.
_10<img src="/team.jpg" alt="The team at the summer meetup" width="1200" height="800" />
_10img {_10 max-width: 100%;_10 height: auto;_10}
Why it happens
#When the browser lays out a page, it needs to know how tall each element is. An image with no dimensions has no known size until enough of the file has downloaded for the browser to read its header. Until then the image takes up zero height. Once the size is known, the browser makes room for it and everything below jumps down. That jump is what Cumulative Layout Shift (CLS), one of the Core Web Vitals, measures.
In the past, responsive images made this worse. Developers removed width and height so that max-width: 100% would work, which meant the browser had no information at all. Modern browsers fixed this: they turn the width and height attributes into a default aspect-ratio for the image. With height: auto in your CSS, the image scales with its container and still reserves the right space.
How to fix it
#Add width and height attributes
Use the image's intrinsic pixel size, or any pair of numbers with the same ratio. The values do not fix the displayed size once your CSS sets max-width: 100% and height: auto; they only tell the browser the shape.
For responsive images with srcset, all candidates should share one aspect ratio, so one width and height pair covers them. For art direction with <picture>, each <source> can have its own width and height when the crops differ.
_10<picture>_10 <source media="(max-width: 600px)" srcset="/hero-square.jpg" width="600" height="600" />_10 <img src="/hero-wide.jpg" alt="Mountains at sunrise" width="1600" height="900" />_10</picture>
Use aspect-ratio in CSS
When you do not know the image size in advance, or the image fills a box of a fixed shape, set the shape on the element with the CSS aspect-ratio property. Pair it with object-fit so the picture is cropped rather than stretched.
_10.card-image {_10 width: 100%;_10 aspect-ratio: 16 / 9;_10 object-fit: cover;_10}
This works for anything that loads late, such as embedded videos, ads or iframes, not only images.
Lazy-load images below the fold
loading="lazy" tells the browser to wait until an image is near the viewport before fetching it. It saves bandwidth on long pages. It does not cause layout shift as long as the image also has dimensions, because the space is still reserved.
_10<img src="/gallery-12.jpg" alt="Rock pool at low tide" width="800" height="600" loading="lazy" />
Do not lazy-load the LCP image, and give it fetchpriority
The Largest Contentful Paint (LCP) element is often a hero image near the top of the page. Lazy-loading it delays its download until after layout, which makes LCP slower. Leave loading off that image (the default is eager) and add fetchpriority="high" so the browser fetches it ahead of other images. fetchpriority is supported in all current major browsers; older ones ignore it safely.
_10<img src="/hero.jpg" alt="Product dashboard" width="1600" height="900" fetchpriority="high" />
Use fetchpriority="high" on one or two images at most. If everything is high priority, nothing is.
Common pitfalls
#- Setting
heightin CSS to a fixed value whilewidthis fluid. The image distorts. Useheight: autooraspect-ratiowithobject-fit. - Adding
loading="lazy"to every image by default, including the hero. Check which image is the LCP element and leave it eager. - Using dimensions with the wrong ratio. The browser reserves the wrong space, and the page still shifts when the real image arrives.
- Forgetting web fonts and late-loading banners. Images are a common cause of CLS but not the only one.
- In Next.js, the
Imagecomponent requireswidthandheight(orfillinside a sized parent) for the same reason.
Related reading
#Was this resource helpful?