Lazy Loading Images loading=lazy
Defer off-screen images with the native loading attribute.
Lazy Loading Images loading=lazy is a free HTML Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the HTML Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The loading Attribute
<img loading="lazy" src="..."> tells the browser to defer downloading the image until it is about to enter the viewport. Native support across all modern browsers makes this the simplest performance win for image-heavy pages.
How "Near the Viewport" is Defined
Browsers use a small heuristic margin (usually a few thousand pixels below the fold). The image starts loading early enough that it is fully painted by the time the user scrolls to it — lazy loading is invisible to careful scrollers.
When Not to Lazy Load
Never set loading="lazy" on images above the fold or your LCP (largest contentful paint) candidate. The deferred load delays the most important visual element, which hurts both perceived speed and Core Web Vitals.
Width and Height Required
For lazy loading to be effective, give the <img> explicit width and height (or aspect-ratio CSS). Without dimensions the browser cannot reserve space, scrolling reflows the layout, and the lazy-loading heuristic misfires.
<img loading="lazy" src="cat.jpg" width="800" height="600" alt="A cat">iframe Lazy Loading
The same attribute works on <iframe>. <iframe loading="lazy"> defers loading of below-the-fold maps, videos and social embeds — each of which would otherwise pull in megabytes of third-party JavaScript on every page view.
eager is the Default
The default value is loading="eager" (load immediately) and is rarely written explicitly. Setting eager on above-the-fold images can be useful as documentation, signalling to reviewers that the eager choice was deliberate.
Combine with srcset
Lazy loading composes perfectly with responsive images. The browser still picks the right candidate from srcset; it just defers the download. There is no extra coordination required between loading="lazy" and srcset.
<img
loading="lazy"
srcset="img-400.jpg 400w, img-800.jpg 800w, img-1200.jpg 1200w"
sizes="(min-width: 768px) 800px, 100vw"
src="img-800.jpg"
alt="Hero"
width="800" height="600"
>Decoding Hint
Pair lazy loading with decoding="async" to let the browser decode the image off the main thread. This avoids blocking input during scroll when many images arrive simultaneously after a long scroll burst.
Background Images
CSS background-image cannot be lazy-loaded natively. For above-the-fold hero backgrounds, that is fine; for below-the-fold backgrounds use IntersectionObserver to swap in the URL when the element nears the viewport.
Print Behavior
When the page is printed, the browser eagerly loads every image regardless of loading="lazy" so the printed copy is complete. You do not need to special-case lazy images for print stylesheets.
Measuring Savings
Lighthouse's "Defer offscreen images" audit lists every image that should have loading="lazy". Network panel filtering by "Img" plus "Waterfall" view shows the staggered loads that lazy loading produces — initial requests drop dramatically on image galleries.
Knowledge Check
Why is it harmful to set loading="lazy" on a hero image at the top of the page?
Summary
loading="lazy" on offscreen <img> and <iframe> elements is a one-attribute performance win. Pair with explicit width/height (to reserve space), srcset (for responsiveness), and decoding="async" (to keep scrolling smooth). Never apply it to the LCP image or anything visible on the initial viewport.
Frequently asked questions
Is the “Lazy Loading Images loading=lazy” lesson free?
Yes — the full text of “Lazy Loading Images loading=lazy” is free to read here on the web, and the HTML Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the HTML Academy course, upgrade to CoddyKit PRO.
What will I learn in “Lazy Loading Images loading=lazy”?
Defer off-screen images with the native loading attribute. You practise HTML Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start HTML Academy?
No prior experience is required. HTML Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Lazy Loading Images loading=lazy” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this HTML Academy lesson?
Yes. Every HTML Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The async and defer Attributes
- Preload Prefetch and Preconnect
- Lazy Loading Images loading=lazy
- Resource Hints modulepreload