The portfolio page in the Lab has a hero at the top, six gallery photos, and a map at the very bottom. A visitor who only looks at the hero and leaves still downloads all of it, 1,260 KB, mostly for things they never scroll to.
Lazy loading fixes that: it puts off a download until the thing is actually about to be seen. Images and iframes far below the first screen aren't downloaded when the page opens; they wait until you scroll close to them. It's like a restaurant waiter who brings the next dish when you're nearly done with the last one, instead of everything at the start until the table is full. The starter still has to arrive first.
One attribute: loading="lazy"
No JavaScript needed. One attribute on an <img> or <iframe> tag is enough, and every modern browser understands it. Here's the portfolio page from the Lab:
<h1>My Portfolio</h1>
<!-- Hero: visible right away, so NO lazy -->
<img id="hero" src="hero.jpg" width="1200" height="900"
fetchpriority="high" alt="Portfolio cover">
<!-- Gallery: far down the page, so lazy -->
<div class="gallery-section">
<img class="gallery" src="work-1.jpg" loading="lazy"
width="800" height="600" alt="First work">
<img class="gallery" src="work-2.jpg" loading="lazy"
width="800" height="600" alt="Second work">
<img class="gallery" src="work-3.jpg" loading="lazy"
width="800" height="600" alt="Third work">
</div>
<!-- Map: an iframe can be lazy too -->
<iframe src="map.html" loading="lazy" width="600" height="400"
title="Location map"></iframe>
loading="lazy": wait to download until the element gets close to the screen. The default isloading="eager", which downloads straight away. So writingeageror writing nothing gives the same result.- Every gallery image needs its own
loading="lazy". The attribute doesn't carry over from the image before it. - An
<iframe>(a map, a YouTube video) is often heavier than an image, because it loads a whole page with its scripts. Delay it too. - Keep writing
widthandheightso the space is ready and nothing shifts when the image arrives.
In the Lab, the six gallery photos are 80 KB each, the hero is 180 KB and the map about 600 KB. Without lazy, the browser downloads 1,260 KB as soon as the page opens. With lazy on the gallery and the map, only 340 KB.
When the browser starts downloading
Lazy doesn't mean "downloaded only once it's visible". Chrome starts the download once the element is within 1,250 px of the bottom of the screen (on a fast connection like 4G) or 2,500 px (on a slow connection), so the image is ready by the time you get there.
In the Lab, the phone screen is 640 px tall, so the limit is 640 + 1,250 = 1,890 px from the top of the page:
| Element | Top position | Starts downloading |
|---|---|---|
| work-1 | 1,580 px | right when the page opens (under 1,890) |
| work-2 | 1,842 px | right when the page opens |
| work-3 | 2,104 px | after scrolling 214 px |
| work-6 | 2,890 px | after scrolling 1,000 px |
| map | 3,300 px | after scrolling 1,410 px |
The formula: top position minus the screen height minus 1,250. That's why those 340 KB hold the hero plus work-1 and work-2.
Don't lazy-load the top image
The image you see the moment the page opens (often called above the fold) is usually the LCP element (Largest Contentful Paint), the largest element on the first screen. If it's lazy, the browser isn't allowed to download it before it knows where it sits, so it has to wait for the CSS to finish and the layout to be worked out. Time that could have gone into downloading is wasted.
In the Lab: the hero without lazy gives an LCP of 1.5 seconds. With lazy on the hero, LCP becomes 1.9 seconds. So if every image gets lazy, the hero included, LCP actually gets slower.
For the LCP image, do the opposite: add fetchpriority="high". The browser gives it High priority in the download queue right away, instead of waiting until it knows the image is visible. In the Lab, when the gallery isn't lazy, the six gallery photos fight the hero for the connection and LCP becomes 1.8 seconds. With fetchpriority="high" on the hero, LCP is back to 1.5 seconds.
Delaying other things: IntersectionObserver
loading="lazy" only works on <img> and <iframe>. To delay anything else, say a comments component or data from an API, use IntersectionObserver: it calls your function when an element gets close to the screen.
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) { // the element is near the screen
loadComments(entry.target); // only fetch its data now
observer.unobserve(entry.target); // once is enough
}
});
}, { rootMargin: '200px' }); // start 200 px before it shows
observer.observe(document.querySelector('#comments'));
rootMargin: '200px'stretches the screen's edge by 200 px, so the work starts a little before the element is visible.unobservestops watching, so the data isn't fetched twice.
⚠️ Lazy in the wrong place, or missing where it belongs
- Lazy on the hero. Never give the top image that's visible right away
loading="lazy". Your LCP gets worse. - Only the first image is lazy. Every gallery image needs its own attribute. If one is left out, that one still downloads at the start.
- Forgetting the iframe. A map or YouTube embed is often the heaviest part of the page. Give it
loading="lazy"too. - Lazy without
widthandheight. The browser doesn't know the height, so the page shifts when the image arrives.
TL;DR: loading="lazy" on <img> and <iframe> delays the download until the element gets close to the screen (in Chrome: 1,250 px on a fast connection, 2,500 px on a slow one). In the Lab, the first download drops from 1,260 KB to 340 KB. Put it on every gallery image and iframe far down the page, together with width and height. Never on the top image: a lazy hero pushes LCP from 1.5 to 1.9 seconds. Give the hero fetchpriority="high" instead. To delay anything else, use IntersectionObserver.
⚡ One attribute delays the download
Write:
<img class="gallery" src="work-1.jpg" loading="lazy"
width="800" height="600" alt="First work">
Meaning: loading="lazy" on an <img> or <iframe> delays the download until the element gets close to the screen. Every gallery image needs its own attribute. In the Lab, the first download drops from 1,260 KB to 340 KB.
⚡ Don't lazy-load the top image
Guess first: why shouldn't the image you see right away get lazy?
The image that should show straight away arrives late, and LCP gets worse. In the Lab, a lazy hero pushes LCP from 1.5 to 1.9 seconds.
Write:
<img id="hero" src="hero.jpg" width="1200" height="900"
fetchpriority="high" alt="Portfolio cover">
Meaning: Give the hero fetchpriority="high" instead, so it gets high priority in the download queue right away.
⚡ Delaying other things
Meaning: loading="lazy" only works on <img> and <iframe>. For a comments component or data from an API, use IntersectionObserver, which calls your function when the element gets close to the screen.