Native Lazy Loading in Modern HTML
A page can hold dozens of images and several frames, but a visitor only sees the first screen at first. Lazy loading tells the browser to wait on the rest until the visitor scrolls near them. Today it is a single HTML attribute, loading="lazy", with no JavaScript library.
You may already have met lazy loading in the Images and Performance sections. This lesson looks at it as part of the modern HTML toolbox: what the attribute does, where it stops, and how to avoid its two classic mistakes.
What You Will Learnโ
- How
loading="lazy"works onimgandiframe - Which elements to lazy-load, and which to leave alone
- Why the main image must never be lazy
- How to prevent the page from jumping
- What lazy loading cannot do, and how to check your markup
The Big Ideaโ
By default, a browser starts downloading every image and frame it finds, even ones far below the screen. That wastes data and slows the content people actually came to see. With loading="lazy", the browser delays a resource until it is near the visible area.
- Markup Checker
- Lazy or Eager?
- The Code
Loading checker...
Your text is only parsed into an inert document, so nothing is loaded or run. It is never sent anywhere or inserted into this page.
Choose the right loading choice for each image or frame.
<!-- The main image: load it right away, and ask for priority -->
<img src="hero.jpg" alt="Mountain at sunrise"
width="1200" height="600" fetchpriority="high">
<!-- Images below the first screen: load them later -->
<img src="photo-2.jpg" alt="A quiet lake"
width="600" height="400" loading="lazy" decoding="async">
<!-- Frames work too -->
<iframe src="https://example.com/map" title="Office map"
width="600" height="400" loading="lazy"></iframe>
The Attributeโ
| Value | Meaning |
|---|---|
lazy | Wait until the element is near the visible area |
eager | Load straight away. This is the default, so you rarely write it |
It works on img and iframe. Any other value is invalid and treated as the default.
How "near" is decidedโ
The browser chooses the distance, and it varies between browsers and even between connection speeds. Roughly, it starts loading a little before the element scrolls into view, so most visitors never see an empty gap. You cannot set that distance in HTML.
Rule One: Never Lazy-Load What Is Visible on Arrivalโ
The most important rule: do not put loading="lazy" on images that appear on the first screen, especially your main image (often the Largest Contentful Paint element). Lazy loading holds it back until the browser has done its layout work, so the most important picture loads later, not sooner.
For the main image, do the opposite:
<img src="hero.jpg" alt="..." width="1200" height="600" fetchpriority="high">
fetchpriority="high" is covered in Modern HTML Features.
Rule Two: Always Give a Sizeโ
A lazy image has not loaded yet, so the browser does not know how big it is. Without width and height, the space is zero, and when the image finally arrives the page jumps. Always set both, and let CSS scale the image:
img { max-width: 100%; height: auto; }
The browser uses the two attributes to work out the aspect ratio and reserves the right space. The markup checker above warns when they are missing.
Frames Tooโ
An iframe is often much heavier than an image: it loads a whole page, with its scripts. A map or video lower down is a great candidate:
<iframe src="https://example.com/video" title="Product tour"
width="560" height="315" loading="lazy"></iframe>
Remember to keep the title, and see iframe security for sandbox and other safety attributes.
What Lazy Loading Cannot Doโ
- It needs JavaScript to be on. If scripting is disabled, browsers may load these resources normally, so images still show. Check MDN for details.
- It does not shrink files. A 5 MB photo is still 5 MB when it loads. Use proper formats and sizes with
srcset. - It does not work for CSS background images. Only
imgandiframehave the attribute. - It cannot be tuned in HTML. You cannot pick the distance. If you need that control, you need JavaScript, such as an observer.
- Hidden and off-screen items may load early. Layout decides what counts as "near", so a carousel with many slides can load more than you expect.
Lazy Loading and Searchโ
Search engines can generally find native lazy-loaded images, because the src is in the HTML. Avoid older tricks that put the real address in data-src and rely on scripts, since a crawler may not run them. Native lazy loading is simpler, and safer for search.
Accessibilityโ
Lazy loading does not change meaning, but keep the basics:
- Every image still needs
alt, and every frame atitle. - Reserve space with
widthandheight, so people with a screen magnifier or low vision are not thrown by a jumping page. - Do not lazy-load content that must be reachable by find in page or by a skip link target, if its absence would confuse people. Test.
- Respect users on slow connections, who benefit most. Provide a readable layout while images arrive.
Checking Your Setupโ
- Open DevTools Network, reload, and look at which images load at once. Below-the-fold ones should wait.
- Scroll slowly, and watch them load just before they appear.
- Check the main image loads early and is not marked lazy.
- Throttle the connection to a slow setting, and look for jumps in the layout.
- Turn off images, or test with a slow network, to check the
alttext. - Run a performance audit, and compare before and after.
- Check the Console for warnings.
Best Practicesโ
- Lazy-load images and frames below the first screen.
- Never lazy-load the main image. Use
fetchpriority="high"instead. - Always set
widthandheight. - Keep
altandtitlein place. - Combine with
srcsetand modern formats to cut file size. - Use
decoding="async"for large images that are not critical. - Prefer the native attribute to a script library.
- Measure, instead of lazy-loading everything by habit.
Quick Checklistโ
- The first-screen images are not lazy
- The main image has
fetchpriority="high" - Below-the-fold images and frames have
loading="lazy" - Every image has
width,heightandalt - Every iframe has a
title - The layout does not jump while loading
- The real address is in
src, not hidden in a script-only attribute
Security and Privacy Notesโ
- Lazy loading is not a privacy feature. Delayed requests are still requests. A third-party image or frame contacts its server when it loads, and shares the visitor's address and possibly cookies. See Third-Party Content Security.
- Frames carry risk. Lazy-loaded iframes still need
sandboxwhere suitable, a cleartitle, and a trusted source. See iframe security. - Never build
srcfrom visitor input. A user-controlled address can load unwanted content. Allow only known sources, and see XSS Basics. - Your Content Security Policy still applies to lazy-loaded resources.
img-srcandframe-srcdecide what may load. - Loading later can reveal behaviour. A server can see which images were requested, so it can tell roughly how far someone scrolled. Keep this in mind for sensitive pages.
Global Audience Notesโ
- Slow and metered connections are common worldwide. Not downloading what a visitor never scrolls to saves real money and time.
- Phones with small screens show fewer images at once, so lazy loading often helps most there.
- Right-to-left and vertical layouts change what is "near", but the attribute works the same. Test.
- Alt text and titles should be in the page's language, and are the fallback when an image is slow to arrive.
- Do not assume fast networks when you test. Throttle.
Common Mistakes to Avoidโ
- Lazy-loading the main image.
- Leaving out
widthandheight, causing layout jumps. - Lazy-loading everything, including small first-screen icons.
- Hiding the real address in
data-srcwith a script. - Expecting it to work on CSS backgrounds.
- Writing
loading="true"or another invalid value. - Forgetting
altandtitle. - Using lazy loading instead of resizing large files.
- Not testing on a slow connection.
- Loading third-party frames without checking their privacy cost.
Practice Timeโ
- In the checker, load the flawed example. List every problem it finds.
- Fix each one in the text box, until you see OK.
- Load the good example. Which image has no
loadingattribute, and why? - Complete the Lazy or Eager? matcher.
- Open a page of your own, and find the main image. Is it lazy?
- In DevTools Network, throttle to a slow connection, and scroll. When does each image load?
- Bonus: add
widthandheightto an image that has none, and compare the layout while loading.
Quick Recapโ
Click a question to check your answer.
1. What does loading="lazy" do?
It tells the browser to delay an image or iframe until it is near the visible area.
2. Which elements support it?
img and iframe. It does nothing for CSS background images.
3. Why should the main image not be lazy?
Lazy loading delays it, but it is the first thing people need to see. Load it right away, and add fetchpriority="high".
4. Why set width and height on lazy images?
The browser reserves the right space, so the page does not jump when the image arrives.
5. Does lazy loading make a large file smaller?
No. It only changes when the file loads. Use proper sizes, formats and srcset to cut the size.
6. Is lazy loading a privacy measure?
No. The request still happens later, and still tells the server about the visitor.
Key Takeawaysโ
loading="lazy"is one attribute that saves data and speeds up the first screen.- Use it below the fold, never on the main image.
- Always give
widthandheightto prevent jumps. - It works on images and iframes, not on CSS backgrounds.
- It is not smaller files, and not privacy.
- Measure on a slow connection.
That completes the Modern HTML section. You can now use the platform's own tools, from dialog, popover and inert to templates, slots, web components and lazy loading, and know when each one fits. Return to the section overview to review any lesson.