All articles
165 articles · updated weekly See our Tools
All articles
Tutorials

Lazy loading images and components: without breaking LCP

Lazy loading done right speeds up the page; done wrong it hurts LCP. When to use loading=lazy, why never on the hero, how to avoid CLS and lazy-load components.

Lazy loading images and components: without breaking LCP
COVER · Tutorials

Lazy loading became a synonym for performance and, because of that, people put it everywhere, including where they shouldn't. The most common mistake I see is loading="lazy" on the hero image, precisely the element that is usually the page's LCP. The result is the opposite of what you wanted: the browser delays the download of the most important image, LCP gets worse, and the metric you meant to improve goes down the drain.

Lazy loading means deferring the download of a resource (image, iframe, script or component) until it is close to being needed, usually when it approaches the visible area of the screen. In HTML, the loading="lazy" attribute on <img> and <iframe> is enough; in JavaScript, IntersectionObserver and dynamic import(). This post shows when it speeds up the page, when it gets in the way, how not to cause CLS, and how to defer components without weighing down the initial load.

What lazy loading solves

On a page with 40 images, only 3 or 4 show up on the first screen. Without lazy loading, the browser downloads all 40 at once: the requests compete for bandwidth with what the user is looking at, and whoever is on mobile data pays for images they may never scroll to. With lazy loading, the browser downloads only what is near the viewport and leaves the rest until the user gets there.

The gain shows up on long pages: galleries, product listings, articles with many figures, pages with video and map embeds. And it disappears, or turns into a loss, when you defer something the user needs to see right away.

How to use loading="lazy" on images and iframes

You don't need a library. The native attribute handles most cases:

<img
  src="/photos/product-12.webp"
  alt="Blue running shoe"
  loading="lazy"
  width="800"
  height="600"
/>

<iframe
  src="https://www.youtube.com/embed/VIDEO_ID"
  title="Product demo"
  loading="lazy"
  width="560"
  height="315"
></iframe>

The browser decides when to download based on the distance to the viewport, and does it better and cheaper than any script of yours. The spec defines two values: lazy and eager, which is the default. A little-known detail, documented in the MDN <img> reference: loading is only deferred when JavaScript is enabled, as an anti-tracking measure.

Support, according to MDN's compatibility data (version 8.1.8, from November 2, 2026):

Feature Chrome Edge Firefox Safari (macOS and iOS)
loading="lazy" on <img> 77 79 75 15.4
loading="lazy" on <iframe> 77 79 121 16.4
fetchpriority on <img> 101 101 132 17.2

In other words: for images, every up-to-date browser supports it; for iframes, Firefox only got there at the end of 2023. Old browsers simply ignore the attribute and load immediately, so there is no risk of breaking anything. Don't ship a lazy-load library just for this.

Never lazy-load the hero image

The above-the-fold image (the banner, the hero, the first visible figure) is almost always the LCP element. Marking it loading="lazy" tells the browser it can wait, and the browser only decides to fetch it after computing layout and finding out it is in the viewport. You deliberately delayed the image that defines how fast the page feels.

This isn't theory. Felix Arntz and Rick Viscomi measured the effect in The performance effects of too much lazy-loading, on web.dev, with CrUX data and an A/B test on WordPress pages: turning off lazy loading on archive pages improved LCP from 2,029 ms to 1,759 ms on desktop (13%) and from 1,657 ms to 1,403 ms on mobile (15%). Their conclusion was that lazy loading only the images below the fold fully reverses the regression.

On the hero, do the opposite: keep the default loading and signal high priority.

<img
  src="/hero.webp"
  alt="Main product dashboard"
  fetchpriority="high"
  width="1200"
  height="630"
/>

Three practical rules:

  • fetchpriority="high" on one image only. If everything is a priority, nothing is. Put it on the image that actually is the LCP.
  • A CSS background image has no native lazy loading and is discovered late. The browser only knows it exists after downloading and applying the CSS. If the hero is a background-image, prefer switching it to an <img>, or add <link rel="preload" as="image" href="/hero.webp" fetchpriority="high"> to the <head>.
  • If you're not sure the image is above the fold, leave it without lazy. Downloading one extra image costs less than delaying LCP.

To detect the problem, run Lighthouse or PageSpeed Insights. Since version 8.4, Lighthouse flags "Largest Contentful Paint image was lazily loaded"; in recent versions, which group audits into insights, the warning shows up under "LCP request discovery", which checks that the LCP image is discoverable in the initial HTML, has fetchpriority=high and is not lazy-loaded. How to read those reports without getting lost is in the post on what the PageSpeed score shows and what it hides.

The WordPress case: where so many lazy heroes came from

Many of the lazy-loaded heroes on the web came from a WordPress default, which was fixed in stages:

If your site runs an old WordPress, or a lazy-load plugin that overrides core behavior, the hero may well still be lazy. It is worth opening the HTML and looking.

Reserve space so you don't cause CLS

Lazy loading without dimensions is a CLS (Cumulative Layout Shift) factory. When the image only takes up space after it loads, everything below it jumps the moment it appears. The user was reading, the layout rearranges, and they lose their line, or click the wrong thing.

The fix is to reserve the space up front. The most robust way is declaring width and height in the HTML: the browser computes the aspect ratio and reserves the box before the first byte arrives. When the image is fluid, combine it with CSS:

img {
  max-width: 100%;
  height: auto; /* keeps the width/height ratio from the HTML */
}

.card-img {
  width: 100%;
  aspect-ratio: 4 / 3; /* for when you don't have dimensions in the HTML */
  object-fit: cover;
}

CLS, LCP and INP go together; if you want to understand how the three metrics relate, read the Core Web Vitals guide.

IntersectionObserver for custom cases

The native attribute covers images and iframes. When you need to defer something else (initializing a map, mounting a heavy chart, loading a background image, triggering an animation), IntersectionObserver comes in. It tells you when an element crosses the viewport, without a scroll listener firing dozens of times per second.

const observer = new IntersectionObserver((entries) => {
  for (const entry of entries) {
    if (entry.isIntersecting) {
      loadContent(entry.target);
      observer.unobserve(entry.target);
    }
  }
}, { rootMargin: "200px" });

document.querySelectorAll("[data-lazy]").forEach((el) => observer.observe(el));

rootMargin: "200px" starts loading 200px before the element enters the screen, so the content arrives on time instead of showing up late. And call unobserve after loading, so you don't leave observers hanging around.

A word on SEO: JavaScript lazy loading that only fires on scroll or click is risky. Google's lazy-loading documentation says Google Search does not interact with your page, so content that only appears after a user action may not be indexed. Loading when the element enters the viewport, as in the example, is the recommended pattern.

Lazy loading components with import()

The same idea applies to JavaScript. If a component only shows up on a specific route, or only when the user opens a modal, it shouldn't be in the bundle every visit downloads. Dynamic import() makes the bundler split the module into its own chunk, downloaded only when that line runs:

button.addEventListener("click", async () => {
  const { default: Editor } = await import("./Editor.js");
  mount(Editor);
});

Route-based splitting is the most common case and the one with the biggest payoff, and frameworks like SvelteKit, Next and Nuxt already do it when you use their routing. Modals, customizers, rich editors and chart libraries, everything most visitors never open, should be an import() tied to the trigger that actually opens it. On this site, the theme customizer is only imported when the user opens the panel; the full case, with its effect on TBT, is in what fixed a 1.6s TBT on SvelteKit. Less JavaScript in the initial load means a freer main thread, which helps TBT and INP.

The tradeoffs of deferring everything

Third-party iframes are the cheapest win: a YouTube or map embed pulls in a lot of JavaScript and many requests, and deferring it until the viewport is often the best line of HTML on the page.

But don't lazy-load everything. Every deferred element may show up as an empty gap while the content arrives, or late when the user scrolls fast. Deferring a component the user sees immediately trades a metric for a worse experience. And there is a limit no lazy loading solves: it doesn't fix a 4 MB image. Deferring a huge file only defers the slowness; when it finally loads, it still hogs the connection.

Frequently asked questions

Does lazy loading hurt SEO?

With the native loading="lazy" attribute, no: the image is still in the HTML and Google finds it. The risk is JavaScript lazy loading that only loads content after a scroll or click, because Google Search doesn't interact with the page. And lazy-loading the hero hurts indirectly, since it worsens LCP, which is part of Core Web Vitals.

Does lazy loading improve or worsen LCP?

It depends on where you apply it. On images below the fold, it helps indirectly by freeing bandwidth for visible content. On the hero image, it hurts: the study published on web.dev measured LCP 13% to 15% slower on WordPress pages with lazy loading above the fold. The rule is no lazy on the hero, with fetchpriority="high", and lazy for the rest.

Do I need a library for lazy loading?

For images and iframes, no: the native attribute is supported by every current browser, and old browsers just ignore it. Use IntersectionObserver only to defer things that aren't images or iframes, such as charts, maps and CSS background images.

Should I lazy-load every component?

No. Lazy-load what most visitors don't use right away: modals, heavy editors, charts, embeds. Components that show up on the initial load belong in the main bundle, because deferring them only adds noticeable delay.

Eager on the hero, lazy below the fold

Lazy loading is a surgical tool: below the fold, it saves bandwidth and frees the network for what matters; on the hero, it delays LCP. Keep the LCP image without lazy and with fetchpriority="high", reserve space with width/height or aspect-ratio on every deferred image, use the native attribute for images and iframes, IntersectionObserver for the rest, and import() for JavaScript nobody asked for yet. And check the result in Lighthouse instead of assuming.

The editorial content ends here. Lazy loading is half the solution; the other half is a light file. Before publishing, I run photos through the Quick Tools image compressor, size them to how they're displayed with the image resizer, and clean up icons and illustrations with the SVG optimizer.

RD
Author
Rafael Duarte
Backend developer with a background in fintech and B2B SaaS — worked on teams that scaled APIs from zero to millions of requests. Carries enough production scars to hold strong opinions on tools, patterns and architecture decisions. Not an academic: he read the UUID RFC when he had to choose between v4 and v7 for a write-heavy table.
View profile