keepcalculator

06 Numbers and Powers

Lazy Loading Images: Best Practices for Faster Websites

Card No. 06·37871 · Updated · Standard formula, shown below

Lazy loading images means deferring the loading of off-screen images until the user scrolls near them, which directly speeds up initial page load and saves bandwidth. The best practice is to use the native loading="lazy" attribute for most images, combined with explicit width and height attributes to prevent layout shift, and to reserve space for the image before it loads. This article explains how lazy loading works, when to use it, common pitfalls, and how to implement it correctly for the best performance.

How Lazy Loading Works

When a browser loads a page, it normally downloads every image in the HTML, even those far below the viewport. Lazy loading changes this behavior: the browser only fetches images when they are about to enter the viewport (usually within a small threshold, like 1250 pixels). This is done using the loading attribute (native lazy loading) or via JavaScript with the Intersection Observer API.

Native lazy loading is the simplest method—just add loading="lazy" to the <img> tag. The browser handles the rest. JavaScript-based lazy loading gives more control (e.g., custom thresholds, fade-in effects) but requires more code and careful handling of noscript fallbacks. For most sites, native lazy loading is sufficient and recommended.

When to Use Lazy Loading (and When Not To)

Lazy loading is ideal for images that are not immediately visible—typically any image below the fold. This includes product images in a long catalog, blog post illustrations, and gallery thumbnails. However, you should not lazy load:

  • Above-the-fold images (e.g., hero images, logos) because they are critical for initial render and should load immediately.
  • Images that are part of the largest contentful paint (LCP) element, as delaying them can hurt your Core Web Vitals score.
  • Images that are likely to be viewed immediately (e.g., in a carousel’s first slide) to avoid a visible flash.

Also, avoid lazy loading images that are already in the viewport on mobile, as the viewport is small and the image might be visible before the scroll event fires.

How to Implement Lazy Loading Correctly

Here are the steps to implement lazy loading properly:

  1. Add loading="lazy" to all <img> tags that are below the fold. For example: <img src="photo.jpg" loading="lazy" alt="description">.
  2. Set width and height attributes on every image (even lazy ones) to reserve space and prevent layout shift (CLS). Without these, the page may jump as images load.
  3. Use decoding="async" for off-screen images to allow the browser to decode them without blocking rendering.
  4. Provide a fallback for older browsers that don’t support native lazy loading. You can use a small JavaScript library like lazysizes, or a simple Intersection Observer script, but ensure it doesn’t double-load images.
  5. Test with JavaScript disabled to ensure images still appear (native lazy loading is ignored, but your fallback should still work).

Common Mistakes and How to Avoid Them

One common mistake is lazy loading images that are already in the viewport on page load, which can cause a delay in showing them. Another is forgetting to set dimensions, causing layout shift. Also, some developers use lazy loading on background images via CSS, which requires a different approach (e.g., using content-visibility or a JavaScript observer).

Another pitfall is lazy loading images that are critical for SEO, like product images in a carousel that Google might not crawl if they are not loaded. However, Google’s crawler generally renders JavaScript, so it will see lazy-loaded images if implemented correctly, but it’s safer to use native lazy loading for SEO-critical images.

Finally, avoid lazy loading images that are part of the LCP element. If your hero image is the LCP, load it eagerly with fetchpriority="high".

Lazy Loading vs. Other Image Optimization Techniques

Lazy loading is just one piece of image optimization. To make your site faster, combine it with:

  • Image compression (e.g., WebP, AVIF) to reduce file size.
  • Responsive images using srcset and sizes to serve the right size for the viewport.
  • CDN caching to serve images from edge servers.
  • Preloading critical images with <link rel="preload"> for above-the-fold images.

Lazy loading is not a substitute for these; it complements them. For example, a compressed 20KB image that loads lazily is faster than a 200KB image that loads lazily.

Impact on SEO and Core Web Vitals

Lazy loading can improve your site’s performance metrics, particularly Largest Contentful Paint (LCP) and Cumulative Layout Shift (CLS), if done correctly. By deferring off-screen images, you reduce the initial bandwidth and CPU usage, allowing the main content to render faster. However, if you lazy load the LCP image, you can harm LCP. So, always keep critical images eager.

For SEO, Google can index lazy-loaded images as long as they are in the DOM and the URLs are accessible. Native lazy loading is fully supported by Google’s crawler. Avoid using JavaScript that replaces src with data attributes if you’re concerned about indexing; use native or a well-tested library.

Frequently Asked Questions

Does lazy loading work on all browsers?

Native lazy loading (loading="lazy") is supported in all modern browsers (Chrome, Firefox, Edge, Safari 16.4+). For older Safari and other legacy browsers, you need a JavaScript fallback. You can detect support with ('loading' in HTMLImageElement.prototype) and conditionally load a polyfill.

Can lazy loading hurt SEO?

No, if implemented correctly. Google’s crawler renders JavaScript and will see lazy-loaded images as long as they are in the DOM. However, avoid lazy loading images that are critical for page relevance, like the main product image, as they might be considered part of the LCP and should load eagerly.

What is the difference between lazy loading and async loading?

Lazy loading defers the download of an image until it’s near the viewport. Async loading (decoding="async") tells the browser to decode the image without blocking rendering, but the image is still downloaded immediately. They are complementary.

Should I lazy load images in a carousel?

Yes, but only for slides that are not initially visible. The first slide should load eagerly to avoid a delay. For subsequent slides, use lazy loading to save bandwidth, but ensure the carousel script can handle the loading state.

How do I lazy load background images?

Background images set via CSS cannot use the loading attribute. You can use a JavaScript Intersection Observer to add a class that sets the background image when the element is near the viewport. Alternatively, use an <img> tag with loading="lazy" and position it absolutely behind the content.

Does lazy loading work with responsive images?

Yes, you can combine loading="lazy" with srcset and sizes. The browser will still choose the appropriate source based on the viewport, and only fetch it when needed. Just ensure the width and height attributes reflect the rendered size to avoid layout shift.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →