← Back to HTML & CSS, page 3

html-css-0056 · Medium mid

How do loading="lazy", decoding and fetchpriority help page speed, and where should you not use them?

loading="lazy" delays images and iframes until they are near the viewport, decoding="async" lets decoding happen off the critical path, and fetchpriority raises or lowers a request's priority. Use lazy only below the fold and keep width and height set.

Full answer

Not every image on a page is needed at once. Images far below the visible area can wait until the user scrolls near them. That saves data and leaves bandwidth for what matters first.

  • loading="lazy" on img and iframe tells the browser to delay fetching until the element is close to the viewport. The browser decides how close, and the distance differs between browsers and connections. The default is eager.
  • decoding="async" allows the browser to decode the image without blocking other rendering. It does not change when the file is fetched.
  • fetchpriority="high" raises the priority of one important request, usually the main image at the top of the page, which is often the largest contentful paint element. fetchpriority="low" lowers it for less important images.

Never lazy-load the image the user sees first. Delaying it makes the main content appear later and hurts the Largest Contentful Paint. Also keep width and height on every image. A lazy image with no reserved space makes the page jump when it loads.

The hero image loads straight away with high priority. The gallery image is 6000px down, so it is not fetched until the user scrolls near it. In Chrome the image reports complete as false at first and true after scrolling to it. The map iframe also waits.

The default distance threshold is large on purpose, so images are ready before the user reaches them. Treat lazy loading as a hint, and check the Network panel to confirm what really loads on first paint.

html
<style>
  body { margin: 0; }
  .spacer { height: 6000px; background: linear-gradient(#fff, #ddd); }
</style>
<img id="hero" src="hero.jpg" width="800" height="400" alt="Students in a lab" fetchpriority="high">
<div class="spacer"></div>
<img id="below" src="gallery-1.jpg" width="800" height="400" alt="Library reading room" loading="lazy" decoding="async">
<iframe id="map" src="https://example.com/map" width="400" height="200" title="Campus map" loading="lazy"></iframe>

The image files are placeholders served by a local test server. The check read the attributes and confirmed that a lazy image below the fold was still not loaded before scrolling and loaded after scrolling. Real download timing was not measured. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • loading="lazy" delays offscreen images and iframes; eager is the default.
  • Do not lazy-load the first visible image.
  • fetchpriority="high" should go on one or two key requests only.
  • decoding="async" affects decoding, not fetching.
  • Width and height prevent layout shift for lazy images.

Common mistakes

  • Adding loading="lazy" to the hero image. It delays the most important image on the page.
  • Lazy-loading without width and height, so the layout shifts when each image appears.
  • Using fetchpriority="high" on many images. If everything is high priority, nothing is.

Follow-ups

  • Which Core Web Vital can lazy-loading the first image make worse?
  • Does decoding="async" delay the download of an image?

Tips

  • Say the rule in one line: eager for what the user sees first, lazy for the rest.
  • Check the Network panel with a throttled connection and a short screen to see what loads at first paint.