Skip to main content

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 on img and iframe
  • 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.

http://127.0.0.1:5500/index.html

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.

The Attributeโ€‹

ValueMeaning
lazyWait until the element is near the visible area
eagerLoad 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 img and iframe have 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.

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 a title.
  • Reserve space with width and height, 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โ€‹

  1. Open DevTools Network, reload, and look at which images load at once. Below-the-fold ones should wait.
  2. Scroll slowly, and watch them load just before they appear.
  3. Check the main image loads early and is not marked lazy.
  4. Throttle the connection to a slow setting, and look for jumps in the layout.
  5. Turn off images, or test with a slow network, to check the alt text.
  6. Run a performance audit, and compare before and after.
  7. Check the Console for warnings.

Best Practicesโ€‹

  1. Lazy-load images and frames below the first screen.
  2. Never lazy-load the main image. Use fetchpriority="high" instead.
  3. Always set width and height.
  4. Keep alt and title in place.
  5. Combine with srcset and modern formats to cut file size.
  6. Use decoding="async" for large images that are not critical.
  7. Prefer the native attribute to a script library.
  8. 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, height and alt
  • 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 sandbox where suitable, a clear title, and a trusted source. See iframe security.
  • Never build src from 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-src and frame-src decide 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โ€‹

  1. Lazy-loading the main image.
  2. Leaving out width and height, causing layout jumps.
  3. Lazy-loading everything, including small first-screen icons.
  4. Hiding the real address in data-src with a script.
  5. Expecting it to work on CSS backgrounds.
  6. Writing loading="true" or another invalid value.
  7. Forgetting alt and title.
  8. Using lazy loading instead of resizing large files.
  9. Not testing on a slow connection.
  10. Loading third-party frames without checking their privacy cost.

Practice Timeโ€‹

  1. In the checker, load the flawed example. List every problem it finds.
  2. Fix each one in the text box, until you see OK.
  3. Load the good example. Which image has no loading attribute, and why?
  4. Complete the Lazy or Eager? matcher.
  5. Open a page of your own, and find the main image. Is it lazy?
  6. In DevTools Network, throttle to a slow connection, and scroll. When does each image load?
  7. Bonus: add width and height to 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 width and height to 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.