Sub topic 13
Lazy loading and the above-the-fold exception
This chapter assumes you can write an img with srcset and sizes, and that you know what "above the fold" means: the part of the page visible without scrolling. Here you add the loading attribute, which tells the browser when to fetch an image.
What lazy loading does
By default, a browser downloads every image it finds in the HTML as soon as it can. On a long page, that means fetching pictures the reader may never scroll to. Lazy loading defers a fetch until the image is near the viewport.
You enable it with one attribute on the img:
<img
src="gallery-800.jpg"
srcset="gallery-400.jpg 400w, gallery-800.jpg 800w, gallery-1600.jpg 1600w"
sizes="(min-width: 40em) 50vw, 100vw"
width="1600"
height="1067"
loading="lazy"
alt="Boats moored in a harbor">The attribute has two values: lazy and eager. eager is the default, so you only write it to be explicit.
Why width and height are required
A deferred image has not been downloaded, so the browser does not know its shape. Without width and height, the browser reserves no space for it. When the image arrives, the content below it jumps. That jump is layout shift.
With both attributes, the browser computes the aspect ratio from them and holds the right amount of space before any bytes arrive. Treat loading="lazy" and width/height as a pair.
The above-the-fold exception
Do not lazy-load the image that is visible on first load, such as a hero image. Lazy loading adds a delay. The browser must finish layout, find out that the image is in the viewport, and only then start the request. For the largest visible image, which usually determines the Largest Contentful Paint metric, that delay makes the page feel slower.
Use this rule of thumb:
| Image position | Attribute | Reason |
|---|---|---|
| Visible on first load | none, or `loading="eager"` | Fetch immediately |
| Main hero image | `fetchpriority="high"` | Ask the browser to fetch it before other images |
| Below the fold | `loading="lazy"` | Save bandwidth until needed |
The hero image then looks like this:
<img
src="hero-800.jpg"
srcset="hero-800.jpg 800w, hero-1600.jpg 1600w"
sizes="100vw"
width="1600"
height="900"
fetchpriority="high"
alt="A lighthouse on a rocky coast at sunset">Lazy loading with picture
Put loading on the img inside picture, never on a source. The img is the element that actually loads. The source elements only supply candidates. Attributes such as width, height, and alt also belong on the img.
<picture>
<source type="image/avif" srcset="photo.avif">
<source type="image/webp" srcset="photo.webp">
<img src="photo.jpg" width="800" height="600" loading="lazy" alt="A lighthouse on a rocky coast at sunset">
</picture>Common errors
- Adding
loading="lazy"to every image on the page, including the hero. This delays the most important image. - Leaving out
widthandheight, which causes layout shift as images appear. - Putting
loadingon asourceelement, where it has no effect. - Assuming a fixed trigger distance. Browsers decide how far ahead of the viewport to start fetching, so the exact point varies.
Exercise
A page has a full-width banner at the top and twenty product photos below it. Which images get loading="lazy", and what should you add to the banner?
Show answer
Lazy-load the twenty product photos, each with width and height. Leave the banner at the default eager loading and add fetchpriority="high", because it is visible on first load and probably the largest image.