← All articles

9 min read

Lazy loading in the browser: load less, start faster

With one HTML attribute, browsers load images and iframes only shortly before they reach the viewport. Where that helps and where it slows your page down.

A browser window with image placeholders: the images in and directly before the visible area are loaded, the ones further down are still waiting.

A home page can be scrolled through in two seconds, yet in the network tab all the images are hanging. By default, the browser loads whatever is in the HTML: the hero image just like the team photo in the footer that many visitors never see. On a smartphone with a weak connection, your visitors pay for that with waiting time and data volume.

A single attribute helps. With loading="lazy", the browser fetches an element only when the visitor gets close to it, and you don’t need JavaScript for it. This article shows what that looks like, how browsers differ and where lazy loading makes your page slower instead of faster.

The attribute

loading has two values. eager is the default: the browser discovers the element while reading the HTML and starts downloading it right away. With lazy, it waits until the layout is in place and the element approaches the visible area. For img and iframe this works in all common browsers; for video and audio, support is still patchy.

<img src="team.avif" width="800" height="600" alt="The team in the office" loading="lazy">
<iframe src="map.html" width="600" height="400" title="Directions" loading="lazy"></iframe>
<video src="showreel.mp4" width="1280" height="720" loading="lazy"></video>

The download doesn’t start only when the image becomes visible, but a little before. How far ahead is up to the browser.

Try it out

The sample page has twelve images and is 6,000 pixels tall. The blue frame is your screen; the orange dashed area is the distance the chosen browser looks ahead. Every image inside it is requested. Scroll yourself or let the page run, and switch between the browsers. “Without lazy loading” shows the default behavior.

The visualization needs JavaScript. Without lazy loading, the browser loads all twelve images immediately; with loading="lazy" only those near the screen.

File sizes and screen height (800 pixels) are example values. If you scroll down in Chrome, you usually already have the images in front of you before they slide into view. That’s how it should be: nothing loads while someone is looking, and yet nothing is transferred that nobody needs.

How early the browsers start

Chrome bases preloading on the connection. The slower the network, the earlier the download starts. Firefox keeps a fixed distance of about 600 pixels, and Safari takes roughly one screen height. These values apply to images and are rules of thumb, because vendors change them from version to version.

Preload distance for images in pixels (rules of thumb)
Chrome (2G)6,000 px
Chrome (3G)2,500 px
Chrome (4G)1,250 px
Firefox600 px
Safari1 screen

For Safari, the hatching shows an assumed screen height of 800 pixels; the real distance depends on the device. Browsers treat iframes differently again: Chrome looks further ahead for them than for images, while Safari starts only when the frame is visible.

For the amount of data, this makes a clear difference. The next chart shows what the sample page from above requested right after the page load, with nobody scrolling:

Sample page: data requested at page start in MB
Without lazy loading4.4 MB
Chrome (3G)2.5 MB
Chrome (4G)1.9 MB
Safari1.4 MB
Firefox1.1 MB

You don’t have to set the distance yourself. The browser knows your visitors’ connection better than any configuration you could set up in advance.

The hero image is not part of this

The most common mistake: loading="lazy" ends up in the template across the board, and with it on the big image at the very top. That image usually determines the Largest Contentful Paint (LCP), the moment visitors see the main content. The browser discovers an eager image on its first pass through the HTML, while the stylesheet is still loading. A lazy image waits for the finished layout. The download starts later, and the LCP slips back by exactly that delay.

Schematic: when does the hero image download start? (example values, not a measurement)
Default (eager)LCP approx. 450 ms
The download starts early, while the stylesheet is still loading.
With loading="lazy"LCP approx. 700 ms
The browser waits until the stylesheet and layout are done, and only then begins.
+250 ms later

The image takes equally long to load in both cases; it just starts later. How big the gap really is depends on stylesheet, connection and device. On a slow mobile connection it is larger than in the chart.

The hero image therefore stays eager. In addition, fetchpriority="high" helps, which tells the browser that this download takes priority over others:

<img src="hero.avif" width="1600" height="900" alt="Description" fetchpriority="high">

With a <picture> element, set both attributes on the inner <img>. The browser ignores them on the <source> elements.

Reserve space

A lazily loaded image has no size yet when the layout is calculated. If the browser doesn’t know the dimensions, the text below jumps down as soon as the file arrives. That annoys visitors and worsens the CLS value (Cumulative Layout Shift). The fix is unspectacular: width and height in the HTML or an aspect-ratio in the CSS. Then the browser keeps the space free before anything has loaded. For lazy media this is mandatory, and for every other image a good habit.

The rule of thumb

Where is the element? What you set
Hero or LCP image no lazy loading, but fetchpriority="high"
Other images on the first screen nothing, eager is the default
Everything below loading="lazy"

iframes and videos

For iframes, the attribute often pays off more than for images. An embedded map, a video player or a chat widget brings its own chain of requests and slows the page down more than a single photo. An iframe far down the page should almost always be lazy.

For video and audio, check caniuse.com before using it. Where the browser doesn’t know the attribute, it loads as usual, so nothing bad happens. For a background video far down the page, you can use the next section if in doubt.

When the attribute isn’t enough

For everything loading doesn’t cover, there is the IntersectionObserver. It reports when an element comes near the screen, and rootMargin determines how early that happens. This suits maps, chat widgets, CSS background images and JavaScript that you only load when needed:

const target = document.querySelector('#chat');

const observer = new IntersectionObserver(
	([entry]) => {
		if (!entry.isIntersecting) return;
		observer.disconnect();
		import('./chat-widget.js').then((module) => module.mount(target));
	},
	{ rootMargin: '600px' },
);

observer.observe(target);

Not just loading, rendering too

Even when all the data has arrived, the browser still has to calculate and paint a long page. With content-visibility: auto, it skips layout and rendering for everything outside the screen:

.article-section {
	content-visibility: auto;
	contain-intrinsic-size: auto 600px;
}

contain-intrinsic-size provides an estimated height so the scrollbar doesn’t jump. The content stays in the DOM, and search and screen readers keep working. In an example from web.dev, a long travel blog, the render time on first load dropped from 232 to 30 milliseconds.

Render time on first load in the travel blog example from web.dev
Without232 ms
With content-visibility30 ms

How to check your page

You can see whether lazy loading works the way you think in Chrome DevTools:

  1. Open the Network panel and filter by “Img” and “Media”. You’ll find iframes under “Doc”.
  2. Turn on “Disable cache”, reload the page and don’t scroll yet. The list shows what was requested at page start.
  3. Now scroll. Everything that newly appears in the list was deferred.
  4. Also look in the Elements panel. The attribute is often set by a framework, plugin or CMS, and then it appears nowhere in your own code. Pay particular attention to whether it sits on the hero image.

Conclusion

Lazy loading is one of the cheapest optimizations there is: one attribute, no script. But it only helps in the right place. Below the first screen it saves data and loading time; on the hero image it costs you both. After every rebuild, check the HTML that is actually delivered and not just your source code, because the attribute is often set automatically.

You’ll find more on the basics in the MDN documentation on the img element, and in web.dev on lazy loading images and content-visibility. Why loading time as a whole decides over visitors and inquiries is covered in From 🥱 to 😲: why your website’s loading time matters.