Sub topic 13
Lazy loading and the above-the-fold exception
This chapter assumes you can write srcset and sizes, and that you know why width and height attributes reserve space (covered in earlier chapters). It also assumes basic familiarity with the browser's network panel.
What lazy loading does
By default, a browser starts downloading every image it finds in the HTML as soon as it can, including images far below the visible part of the page. A long article or gallery can therefore spend bandwidth on files the reader never scrolls to.
Lazy loading defers an image request until the image is near the viewport. You enable it with one attribute:
<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 does not change which file the browser picks. Candidate selection from srcset and sizes works as before. Lazy loading only changes when the request for that chosen file starts.
The loading attribute has two values:
| Value | Behavior |
|---|---|
| `eager` | Fetch immediately. This is the default when the attribute is missing or invalid. |
| `lazy` | Fetch when the image is close to the viewport. |
Where `loading` goes with `picture`
A picture element is not an image by itself. The img inside it does the loading. Put loading on the img, not on the source elements, where it has no effect.
<picture>
<source type="image/avif" srcset="boats-400.avif 400w, boats-800.avif 800w" sizes="100vw">
<source type="image/webp" srcset="boats-400.webp 400w, boats-800.webp 800w" sizes="100vw">
<img
src="boats-800.jpg"
srcset="boats-400.jpg 400w, boats-800.jpg 800w"
sizes="100vw"
width="800"
height="533"
loading="lazy"
alt="Boats moored in a harbor">
</picture>The browser still runs the source selection first and then applies the lazy rule to the result.
What "near the viewport" means
The specification does not define a distance. Each browser chooses its own threshold, and it may change with connection speed or between versions. Chromium, for example, loads images some distance before they scroll into view so they are usually ready when the reader arrives. Do not write code that depends on an exact distance.
Two consequences follow:
- A lazy image slightly below the fold may still load during the initial page load, because it falls inside the threshold.
- A lazy image far down the page produces no request until the reader scrolls toward it.
The above-the-fold exception
The "fold" is the bottom edge of the viewport when the page first renders. Images that are visible there should not use loading="lazy".
The reason is timing. A browser that sees an eager image can start the request during early HTML parsing. For a lazy image, the browser must first know where the image sits on the page, which requires layout. So the request starts later. For the main image on screen, that delay pushes back the moment the page looks complete.
The image most affected is usually the Largest Contentful Paint (LCP) element. LCP is a performance metric that records when the largest visible piece of content finishes rendering. A hero image is a common LCP element. Lazy-loading it makes LCP worse, which is the opposite of what you want.
flowchart TDReplace that sketch with the real diagram below.
A practical rule
Apply loading="lazy" to images that are clearly below the fold on every screen size. Leave it off for the first image or two, and for anything that might be visible on a large monitor.
"Above the fold" depends on the device. On a phone, only the top of the first section is visible. On a tall desktop display, three images may be visible. If you are unsure, test at both extremes, or choose eager loading, which costs little for one or two images.
Loading priority
Eager loading means "do not delay." It does not mean "load first." The browser still ranks requests, and an image may compete with CSS, fonts, and scripts. The fetchpriority attribute lets you raise or lower an image's rank:
<img
src="hero-800.jpg"
srcset="hero-400.jpg 400w, 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">Use fetchpriority="high" sparingly, normally for the single LCP image. If several images are all high priority, none of them stands out. The attribute is a hint. A browser that does not support it ignores it, and the image still loads normally.
Why `width` and `height` matter more with lazy loading
A lazy image has not been downloaded when the browser lays out the page, so the browser cannot learn its size from the file. Without width and height, the image occupies no space until it loads. Two problems follow:
- Layout shift. When the file arrives, the content below it jumps down. This is the layout shift described in the chapter on reserving space.
- Too many loads. If many unsized lazy images each collapse to zero height, they all sit near the top of the page, inside the viewport threshold. The browser then loads all of them at once, which defeats the purpose.
With the attributes set, the browser reserves the correct aspect ratio. A common pattern is to pair the attributes with CSS so the image still scales fluidly:
<style>
img {
max-width: 100%;
height: auto;
}
</style>The width and height attributes supply the aspect ratio, and height: auto lets the height follow the rendered width.
Edge cases
- Scripting disabled. Browsers only defer lazy images when scripting is enabled. This stops scroll position from being used as a tracking signal. With scripting off, lazy images load as if they were eager. Do not rely on lazy loading to save bandwidth for those users.
- Images in carousels and tabs. An image in an off-screen slide may load early if the slide is within the browser's threshold, and may load late if it is far away. If a slide must be ready the moment the user clicks, consider eager loading for the next slide only.
- Images that move with CSS. An image positioned off-screen by CSS can still be near the viewport as the browser measures it. Test rather than assume.
- Lazy loading and
currentSrc. Before a lazy image loads,currentSrcmay be empty or not yet reflect a request. Check it after the image has loaded, as shown in the later chapter on debugging. - Print. A lazy image that has never scrolled into view might not be loaded when the user prints. Browsers generally handle this by loading such images before printing, but check your target browsers if printing matters.
Verifying the behavior
Open the browser's developer tools and the Network panel, filter by images, and reload the page without scrolling. Check three things:
- The above-the-fold images appear in the list early.
- Images far down the page do not appear yet.
- As you scroll, new image requests appear shortly before each image enters view.
If a below-the-fold image appears immediately, check whether it has width and height, and whether it is closer to the fold than you thought.
Summary of the rules
- Add
loading="lazy"to images below the fold. - Omit it for images visible on first load, especially the LCP image.
- Put
loadingon theimg, even insidepicture. - Always set
widthandheighton lazy images. - Use
fetchpriority="high"for one important image at most, and never together withloading="lazy".
Exercise
A page has a full-width hero image at the top, followed by a gallery of twelve photos further down. A teammate added loading="lazy" to all thirteen img elements and removed their width and height attributes "to simplify the markup." Name two problems this causes and describe the fix.
Show answer
First, the hero image is above the fold and is probably the LCP element, so lazy loading delays its request and makes LCP worse. Remove loading="lazy" from the hero (and consider fetchpriority="high"). Second, removing width and height means the unloaded gallery images take up no space. That causes layout shift when they load, and it can make many gallery images sit inside the viewport threshold at once, so they all load immediately. Restore width and height on every image, and keep loading="lazy" only on the twelve gallery images.
Exercise
In a picture element with two source elements and an img, which element should carry the loading attribute?
Show answer
The img. The source elements only supply candidates, and the img is the element that actually loads and displays the image.