Cumulative Layout Shift (CLS) and Images
You'll learn how to measure and fix layout jumps caused by images loading late, improving your site's Core Web Vitals score.
What Is Cumulative Layout Shift?
Imagine reading an article when suddenly a photo loads and pushes all the text down the page—you lose your place and maybe click the wrong button. That annoying jump is layout shift. CLS measures how much your page's content unexpectedly moves around during loading.
Google's Core Web Vitals use CLS as a key metric for user experience. Images are one of the biggest culprits because browsers don't know how much space to reserve until the image file loads.
How Images Cause CLS
When you add an <img> without dimensions, the browser initially renders it with zero height. When the image loads, the browser suddenly needs to make room for it, shoving everything below it downward. Each shift adds to your CLS score—and a high score hurts your SEO and user experience.
The Fix: Reserve Space Before Loading
You've already learned the solution in previous lessons:
- Always set
widthandheightattributes on your<img>tags - Use
aspect-ratioCSS when dimensions vary responsively - Combine with
object-fitto control how images fill their reserved space
<img src="photo.jpg"
width="800"
height="600"
alt="Mountain view"
loading="lazy">
The browser uses these dimensions to calculate aspect ratio and reserves the correct space immediately—before the image loads.
Measuring CLS
Use browser DevTools (Lighthouse) or Google's PageSpeed Insights to measure your CLS score. Aim for under 0.1 (good), avoid above 0.25 (poor).
Key Takeaway: Images cause layout shift when the browser doesn't know their size upfront—always specify dimensions with width/height attributes to reserve space and keep your CLS score low.