What is lazy loading (loading='lazy')?

Lazy loading defers loading offscreen images and iframes until the user scrolls near them. Adding loading="lazy" to <img> or <iframe> enables it natively in all modern browsers, cutting initial page weight, saving bandwidth and speeding up the first render, with no JavaScript needed.

How it works

The browser keeps a distance threshold (roughly 1250 to 2500px below the viewport, depending on connection speed) and starts fetching an element when it comes within that range, so it is usually loaded before it becomes visible.

Rules for using it well

  • Never lazy-load the LCP image (the hero/banner image above the fold). Doing so delays Largest Contentful Paint. Use loading="eager" (the default) and fetchpriority="high" on it instead.
  • Always set width and height (or CSS aspect-ratio) so lazy images reserve space and do not cause layout shift (CLS).
  • Use it for below-the-fold images, galleries, comment avatars and embedded videos or maps.
  • Pair with decoding="async" to keep image decoding off the critical path.

Beyond images

  • Lazy-load components in JavaScript with dynamic import() (React lazy, Next.js dynamic).
  • IntersectionObserver gives custom control, for example infinite scroll or lazy background images.
  • content-visibility: auto in CSS skips rendering offscreen sections.

Interview tip

The most common mistake is putting loading="lazy" on every image, including the hero; call that out.

Code example

html
1<style>
2 body { font-family: system-ui; }
3 img { display: block; width: 100%; max-width: 480px; height: auto; margin-bottom: 16px; border-radius: 8px; }
4 .spacer { height: 1400px; background: linear-gradient(#fff8ec, #dbeafe); }
5</style>
6
7<!-- Above the fold: load immediately with high priority (likely the LCP element) -->
8<img src="https://picsum.photos/id/1018/960/400" width="960" height="400"
9 alt="Mountain valley at sunrise" fetchpriority="high">
10
11<div class="spacer">Scroll down…</div>
12
13<!-- Below the fold: fetched only when the user scrolls near it -->
14<img src="https://picsum.photos/id/1025/960/400" width="960" height="400"
15 alt="Pug wrapped in a blanket" loading="lazy" decoding="async">
16<iframe src="https://example.com" title="Example embed" loading="lazy" width="480" height="200"></iframe>
17
18<script>
19 const lazy = document.querySelector('img[loading="lazy"]');
20 lazy.addEventListener("load", () => console.log("Lazy image loaded at scrollY =", Math.round(scrollY)));
21</script>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.

Open in study app