Performance Optimization

Lazy Loading

Optimize page performance with lazy loading techniques. Load images only when needed, reduce initial page load time, and improve user experience across all devices.

How do you lazy load images in HTML?

Add loading="lazy" to <img> and <iframe> elements: the browser then loads them only when they're about to scroll into view. This needs no JavaScript and saves bandwidth. Don't lazy-load images near the top of the page, especially the main hero image, because that delays your Largest Contentful Paint.

<img src="gallery-1.jpg" alt="…" width="400" height="300" loading="lazy">
<iframe src="https://www.youtube.com/embed/…" loading="lazy" title="…"></iframe>

<!-- Above-the-fold hero: load ASAP -->
<img src="hero.jpg" alt="…" fetchpriority="high">

Performance-First Loading

Lazy loading defers the loading of images until they're needed, significantly reducing initial page load time, bandwidth usage, and improving Core Web Vitals scores.

Faster Loading

Reduce initial page load time by 60-80%

Better Metrics

Improve LCP, CLS, and FID scores

Viewport-Based

Load only when images become visible

Save Bandwidth

Reduce data usage by 50-70%

Lazy Loading Implementation

Complete lazy loading examples including native lazy loading, Intersection Observer, progressive loading, blur-up effects, and performance monitoring.

Code Editor

<p>Scroll down inside the preview. Each image loads only when it gets close.</p>
<div class="spacer">↓ keep scrolling ↓</div>
<img loading="lazy" src="https://picsum.photos/id/1050/400/200" alt="Lazy image 1" width="400" height="200">
<div class="spacer"></div>
<img loading="lazy" src="https://picsum.photos/id/1051/400/200" alt="Lazy image 2" width="400" height="200">
<p id="log"></p>

Preview

Performance Impact

Core Web Vitals

  • LCP: Faster largest contentful paint
  • CLS: Reduced layout shift
  • FID: Better first input delay
  • INP: Improved interaction to next paint

Network Benefits

  • Bandwidth: 50-70% reduction
  • Requests: Fewer initial HTTP requests
  • Mobile: Critical for mobile users
  • Data costs: Lower data consumption

User Experience

  • Speed: Pages load 2-3x faster
  • Smooth: Progressive image appearance
  • Engagement: Better bounce rates
  • SEO: Improved search rankings

Implementation Checklist

  • Use native loading="lazy" for modern browsers
  • Implement Intersection Observer fallback
  • Add proper placeholder content
  • Don't lazy load above-the-fold images
  • Include loading states and error handling
  • Test across different devices and connections

Lazy Loading — step by step

1 / 4
1

Use the native loading attribute

Add loading='lazy' to img and iframe elements to defer loading until they are near the viewport. This is supported in all modern browsers and requires no JavaScript. Use loading='eager' (the default) for above-the-fold images.

1 of 4
💡loading='lazy' Is a One-Attribute Performance Win
Adding loading='lazy' to images below the fold is one of the most impactful and easiest performance optimizations available. It can significantly reduce initial page weight and improve LCP on image-heavy pages — requiring just one HTML attribute with zero JavaScript and zero external dependencies.

🎯 Quick Check

Which images should NOT have loading='lazy'?

HTML Media Mastery Complete!

Congratulations! You've completed the comprehensive HTML Media tutorial series. You now have expert knowledge of images, video, audio, responsive techniques, and performance optimization. Ready for the next challenge?

Lazy Loading in HTML: summary

Key points

  • Always set width and height on lazy images so space is reserved and nothing jumps.
  • fetchpriority="high" tells the browser to fetch the most important image first.
  • For custom lazy-loading, such as content blocks, use IntersectionObserver.

Frequently asked questions

›Does lazy loading hurt SEO?

Not with native loading="lazy"; Google renders and indexes those images. It can hurt performance scores if applied to the LCP image.