Skip to content
Tyrone Software
Web Development

Your hero image is lazy-loaded, and LCP is paying for it

Abstract illustration for the article Your hero image is lazy-loaded, and LCP is paying for it

A code review rule of thumb says every image gets loading="lazy". It is a good default for images far down the page and a bad one for the image that ends up being the Largest Contentful Paint element.

Why lazy loading hurts here

Lazy loading tells the browser not to fetch the image until it is near the viewport. To decide that, the browser first needs layout, so the request starts later than it would for an ordinary <img> found by the preload scanner. For the hero image, that delay lands directly in the metric. Google's "good" threshold for LCP is 2.5 seconds, and a late request for the main image is one of the easier ways to lose part of that budget.

A small checklist for the LCP image

  • No loading="lazy" on it. Leave the attribute off, or set loading="eager" explicitly.
  • Add fetchpriority="high", which tells the browser this image matters more than other images competing for bandwidth.
  • Give it width and height so it reserves space and does not add layout shift.
  • Serve a size that matches the layout, not the original upload.
<img src="/img/hero.avif" width="1200" height="630"
     fetchpriority="high" alt="Product dashboard on a laptop">

The background-image problem

An image set through CSS background-image is only discovered after the stylesheet has been downloaded and parsed, because the preload scanner reads the HTML but not the CSS rules. If the hero has to be a background, preload it:

HTML link tag that preloads a responsive hero image
Preloading a hero image that is used as a CSS background.

If the image is responsive, the preload can carry imagesrcset and imagesizes so the browser picks the same candidate it would pick for an <img>.

Measure before believing this

Not every page benefits. If the LCP element is a heading, none of this matters. Check which element the browser reports as LCP in the Performance panel before changing markup.

Carousels and client-rendered heroes

A hero built as a JavaScript carousel often renders an empty container first and inserts the first slide's image after the script runs, sometimes after fetching slide data from an API. That image never gets the benefit of the browser's preload scanner, no matter what attributes you put on the <img> tag once it finally exists, because the scanner only sees markup that was present in the initial HTML response. Where a carousel is the design, render the first slide as plain server-rendered markup and let the script take over only for the slides after it.

The same problem shows up with client-side frameworks that hydrate the hero from a data fetch on mount. If the framework supports server-side or static rendering for above-the-fold content, use it for at least the LCP element, even if the rest of the page stays client-rendered. Checking this is quick: view the page's initial HTML source (not the rendered DOM) and confirm the hero <img src> is already there before any script runs.

Keep reading

Technologies

  • Python
  • PHP
  • Laravel
  • Django
  • PostgreSQL
  • MySQL
  • HTML5
  • CSS3
  • Bootstrap