Native Lazy Loading: The Loading Attribute for Images and Iframes (and When Not to Use It)

Native lazy loading for images and iframes: browser thresholds, why lazy-loading your LCP image hurts, layout shift, and when loading=lazy is wrong.

Executive Summary: Adding loading="lazy" to an image or iframe tells the browser to delay fetching it until it is close to the viewport. It now works in every major browser for both images and iframes, with Firefox 121 completing iframe support in December 2023. Use it for content below the fold, always with width and height set, and never on the hero image or anything else visible on first load. Lazy-loading the largest image on the screen is one of the most common ways teams make their Largest Contentful Paint worse while trying to make it better.
Viewport frame with visible images loaded and others waiting below the fold

A team adds loading="lazy" to every image on the site in one pull request, because a performance article said lazy loading is good. Total bytes on the home page drop by half. Largest Contentful Paint gets 600 milliseconds slower. The hero banner, which is the LCP element, now waits until the browser has laid out the page and confirmed that the image is in the viewport before it starts downloading.

Native lazy loading is the browser’s built-in ability to defer offscreen images and iframes, controlled by the loading attribute. Its two values are eager, the default, which fetches the resource immediately, and lazy, which defers it until the element is within a distance threshold of the viewport. It replaces the scroll listeners and IntersectionObserver scripts that sites used to ship for the same job, with no JavaScript and no data-src placeholders.

How does the loading attribute work?

<img src="/img/team-offsite.webp" alt="The team at the 2023 offsite in Goa"
     width="1200" height="800" loading="lazy" decoding="async">

<iframe src="https://www.youtube-nocookie.com/embed/VIDEO_ID"
        title="Product walkthrough video" width="560" height="315"
        loading="lazy" allowfullscreen></iframe>

That is the whole API. Browsers that do not support the attribute ignore it and load eagerly, so it is a safe progressive enhancement. Image support has been universal since Safari 15.4 in 2022; iframe support followed in Safari 16.4 in March 2023 and Firefox 121 in December 2023.

When does the browser load a lazy image?

Lazy loading does not wait until an image is visible. It starts the request when the element comes within a distance threshold of the viewport, so the image is usually ready by the time it scrolls into view. Chromium uses thresholds of roughly 1250 pixels on fast connections and 2500 pixels on slower ones; Firefox and Safari use their own, generally smaller, margins. You cannot configure these, and you should not need to.

Two consequences follow. First, on a short page, everything may be within the threshold, so lazy loading changes nothing. Second, on very fast scrolls, users can still outrun the threshold and see an empty box for a moment, which is why reserving space matters so much.

Why must you reserve space for lazy images?

A lazy image without dimensions occupies zero height until it loads, then pushes everything below it down: a layout shift that counts against Cumulative Layout Shift. Set width and height attributes with the image’s intrinsic size. Browsers use them to compute an aspect ratio before the file arrives, even in fluid layouts where CSS sets width: 100%; height: auto. The performance-safe CSS guide covers the CSS side of layout shift.

I wrote a small console script for finding the images on a page that are missing these attributes in find images missing width and height using JavaScript. Run it on any template before turning lazy loading on.

When should you not lazy load?

The LCP image and anything above the fold

Largest Contentful Paint is usually an image: a hero banner, a product photo, a featured article image. An eager image is discovered by the browser’s preload scanner as soon as the HTML arrives, before CSS and layout. A lazy image is only requested after layout confirms where it sits on the page. That delay lands directly on your LCP.

Leave above-the-fold images eager, and give the single most important one a priority hint:

<img src="/img/hero.avif" alt="Dashboard showing weekly revenue"
     width="1600" height="900" fetchpriority="high">

fetchpriority="high" moves the image ahead of other images in the download queue. Chrome has supported it since version 101 and Safari since 17.2 in December 2023; Firefox ignores it for now, which is harmless.

The first items in a grid or list

On a product listing, the first row of products is visible on load. A template that applies loading="lazy" to every card penalizes exactly the ones that matter. Make the first few eager, based on how many fit on a typical first screen, and lazy-load the rest. Server-side templates can do this with a loop index.

WordPress, which has added loading="lazy" to content images automatically since version 5.5, learned this the hard way: version 5.9 stopped lazy-loading the first content image, and version 6.3 began adding fetchpriority="high" to the image it expects to be the LCP element.

Images inside hidden carousels and tabs

Images in a carousel slide or a closed tab that is far off-screen horizontally, or hidden with display: none, will not load until they become visible. When the user swipes to the next slide, they see it load. For the next one or two slides, use eager loading or preload them when the user interacts with the carousel.

Content needed for printing

When a user prints a long article, images that never came near the viewport may not have loaded, and some browsers print empty boxes. For pages that are commonly printed, such as invoices, recipes and documentation, consider eager loading or a beforeprint handler that switches images to eager.

What does lazy loading not cover?

  • CSS background images. The attribute only exists on img and iframe. For large decorative backgrounds below the fold, use an img with object-fit: cover, or an IntersectionObserver that adds the background class when the element approaches.
  • video elements. Use preload="none" with a poster image instead.
  • Pages with JavaScript disabled. For privacy reasons, the specification only defers loading when scripting is enabled, so lazy images load eagerly in that case.
  • Heavy third-party embeds above the fold. Lazy loading only delays an iframe; once it loads, a YouTube or map embed still brings hundreds of kilobytes of script. For embeds that are visible on load, a “facade”, a static thumbnail that swaps in the real iframe on click, saves far more.

Lazy loading and decoding

decoding="async" is a separate hint that allows the browser to decode an image off the main thread rather than blocking a frame. It pairs naturally with lazy images below the fold. Its effect is small in modern browsers, which already decode most images asynchronously, but it does no harm.

A quick decision guide

Element Recommendation
Hero or LCP image Eager, with fetchpriority="high"
Logo and above-the-fold UI images Eager
First row of a product grid Eager
Article images below the first screen loading="lazy"
Comment avatars, footer images, related posts loading="lazy"
YouTube, map or social embed below the fold loading="lazy" on the iframe
Embed visible on load A click-to-load facade
Next slide in a carousel Eager or preloaded on interaction

Measure before and after

Check your change with real numbers. In Chrome DevTools, the Performance panel’s Timings track marks the LCP element and when it was requested; if the request starts late, it is probably lazy. Lighthouse also flags a lazily loaded LCP image directly. Compare field data, such as Chrome User Experience Report or your own Web Vitals reporting, for a few weeks after the change, because lab tests on a fast laptop hide the effect on slower phones.

Frequently Asked Questions

Is loading=lazy supported in all browsers?

Yes. Image lazy loading works in Chrome, Edge, Firefox and Safari, and iframe lazy loading became available everywhere when Firefox 121 added it in December 2023. Browsers without support simply load eagerly.

Should I lazy load all images?

No. Lazy-load images below the first screen, but keep above-the-fold images eager, especially the image that becomes your Largest Contentful Paint. Lazy-loading the LCP image delays its download and makes LCP worse.

Does lazy loading cause layout shift?

Only if the image has no reserved space. Set width and height attributes on every lazy image so the browser can calculate its aspect ratio before it loads, and the layout will not move when the file arrives.

What is the difference between loading=lazy and fetchpriority?

loading=lazy delays fetching an image until it is near the viewport. fetchpriority changes how urgently an image is fetched relative to other resources once the browser decides to fetch it. Use lazy for offscreen images and fetchpriority=”high” for the main above-the-fold image.

Lazy below, eager above

Native lazy loading is one attribute that cuts wasted bytes on long pages, as long as you apply it with judgment. Keep the first screen eager, prioritize the LCP image, set dimensions on everything, and let the browser defer the rest. Then measure, because the right line between eager and lazy depends on your layouts and your users’ devices. For the full reference, see web.dev on browser-level lazy loading and web.dev on Largest Contentful Paint. Related reading on this site: srcset and sizes and the picture element.

Originally posted on 16 January 2024 and post last modified on 16 January 2024

Front-End Development HTML5 Images Lazy Loading Web Performance

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *