Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811320
Back to HTML Notes
Topic #410

Lazy Loading Images

Lazy loading defers downloading offscreen images until the visitor is about to scroll near them — instead of loading every image on the page immediately, even ones far below the fold.

Syntax

<img src="..." alt="..." loading="lazy">

Basic Example

<img src="/assets/images/testimonial-3.jpg" alt="Student testimonial photo" loading="lazy">

Browser output: visually nothing different — the image appears the same once loaded. What's different is when the browser actually downloads it: only as the visitor scrolls close to it.

How It Works

  1. Without loading="lazy", browsers download every <img> on the page immediately, competing for bandwidth with content the visitor can actually see first.
  2. With it, the browser defers offscreen images and fetches them just before they'd become visible, based on scroll position.
  3. This is entirely native, built-in browser behavior — no JavaScript library required.

Important Attributes

ValueMeaning
loading="lazy"Defer loading until the image nears the viewport
loading="eager"Load immediately — the default behavior, rarely needs to be written explicitly

Real-World Example

<img src="/assets/images/hero.jpg" alt="Course hero banner">
<!-- Above: loads immediately — it's the first thing visitors see -->

<img src="/assets/images/testimonial-1.jpg" alt="Student testimonial" loading="lazy">
<img src="/assets/images/testimonial-2.jpg" alt="Student testimonial" loading="lazy">
<!-- Above: both far down the page — safe to lazy-load -->

A realistic pattern: the hero image (visible immediately) loads normally, while testimonial images further down the page are deferred.

Common Mistakes

  • Adding loading="lazy" to an image that's visible immediately when the page loads (like a hero banner) — this can actually delay a critical image and hurt perceived load speed
  • Lazy-loading every image indiscriminately without considering which ones are above the fold

Best Practices

  • Never lazy-load the first, most important visible image on a page (often called the "LCP image")
  • Lazy-load everything else that's meaningfully below the initial viewport

Accessibility Considerations

No negative accessibility impact — screen readers and keyboard navigation are unaffected, since the image (and its alt text) still loads correctly once it's needed.

SEO Considerations

Lazy loading directly improves initial page load speed, a documented ranking factor — but lazy-loading the wrong image (one visible immediately) can hurt the Largest Contentful Paint metric instead of helping it.

Browser & Modern HTML Notes

Native loading="lazy" support arrived across all major browsers by 2020, replacing what used to require a third-party JavaScript library entirely.

Mini Practice

  1. Add loading="lazy" to three images positioned far down a sample page.
  2. Identify which single image on a typical course landing page should not be lazy-loaded, and explain why.

Module 4 Practice Lab

Quick Practice

Add three images to a sample page: one with full descriptive alt text, one purely decorative with alt="", and one wrapped in <figure>/<figcaption>.

Challenge

Take a page where every image loads at full desktop resolution on every device, and rewrite the images using srcset/sizes to serve appropriately sized files.

Mini Project — Responsive Image Gallery

Build a "Student Projects Gallery" page with 6 images: a hero image (not lazy-loaded, correct width/height), and 5 gallery images below it that are lazy-loaded, each wrapped in <figure>/<figcaption> with a one-line description, and at least one image using <picture> to serve a WebP format with a JPEG fallback.

Related HTML Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
Talk to Advisor Online now
1

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →