HTML loading Attribute

Beginner
6 min read
Updated: Nov 2026
3 examples
Live preview

Definition and Usage

The loading attribute tells the browser when to fetch an <img> or <iframe>. Use lazy for below-the-fold media to save bandwidth; use eager (or omit the attribute) for critical above-the-fold content.

It is not a global attribute, but it is essential for performance. Pair lazy images with width/height (or aspect-ratio) and meaningful alt text.

Baseline · Widely available.

Native lazy loading for images is supported across modern browsers.

Do not lazy-load the LCP image.

Hero / largest contentful paint images should load eagerly so they are not delayed by the lazy threshold.

HTML Syntax

syntax.html
<img|iframe loading ="lazy|eager">

Applies to img and iframe (and similarly documented for related embedding cases). Default when omitted is eager-like immediate loading for images in view.

Attribute Values

ValueDescriptionTry it
lazyDefer fetch until near the viewportTry it
eagerLoad as soon as possible (default-like)Try it

Live Preview

Toggle loading on a sample image. Lazy only matters when the image starts offscreen — scroll demos work best in the Try It editor.

Attribute updates immediately; network deferral depends on viewport distance.

Result
CodeToFun logo sample

loading="lazy"

HTML Examples

Example 1 — Lazy-Load Below-the-Fold Images

Defer gallery or article images until the user scrolls near them.

HTML
<img src="/images/gallery-2.webp" alt="Workshop photo"
     width="800" height="500" loading="lazy" decoding="async">
Try It Yourself

How It Works

  1. Set loading="lazy" for offscreen images.
  2. Include width and height to reserve space and avoid CLS.
  3. Keep useful alt text for accessibility and SEO.
  4. decoding="async" can reduce main-thread decode jank.

Example 2 — Eager Hero / LCP Image

Critical above-the-fold images should not wait for a lazy threshold.

HTML
<img src="/images/hero.webp" alt="Product hero"
     width="1200" height="630" loading="eager" fetchpriority="high">
Try It Yourself

How It Works

  1. Use eager (or omit loading) for the primary hero.
  2. fetchpriority="high" can further prioritize the LCP candidate.
  3. Do not lazy-load the largest above-the-fold image.
  4. Optimize the file (modern formats, correct size) separately.

Example 3 — Lazy iframe Embeds

Defer third-party embeds until they approach the viewport.

HTML
<iframe
  src="https://example.com/embed"
  title="Demo embed"
  width="560" height="315"
  loading="lazy"
  referrerpolicy="no-referrer-when-downgrade">
</iframe>
Try It Yourself

How It Works

  1. loading="lazy" delays the iframe document fetch.
  2. Always set title for accessible framing.
  3. Prefer facades for heavy third parties when possible.
  4. Sandbox / referrerpolicy as your threat model requires.

Edge Cases & Pitfalls

Lazy loading is a performance tool โ€” misuse hurts LCP and UX.

lcp

Never lazy the hero

Above-the-fold / LCP images should be eager.

cls

Reserve space

Missing width/height causes layout shift when lazy images appear.

print

Print and SEO bots

Some contexts may still need the resource; test critical print styles.

css

Not for background-image

CSS backgrounds ignore the HTML loading attribute.

Browser Support

loading is an HTML attribute for browsers. It is not a Node.js, Deno, or Bun API.

✓ Baseline ยท Widely available

loading

Native lazy loading for img is widely supported; iframe lazy is available in modern engines.

96% Global support (approx.)
Google Chrome 77+
Full support
Mozilla Firefox 75+
Full support
Apple Safari 15.4+
Full support
Microsoft Edge 79+
Full support
Opera 64+
Full support
Samsung Internet 12+
Full support
Internet Explorer No support
Not supported
loading Widely supported

Bottom line: Use lazy for below-the-fold media; keep critical images eager.

Key Takeaways

  • Role: defer or prioritize fetching for img/iframe.
  • Values: lazy or eager.
  • Hero: keep above-the-fold images eager.
  • Not global: element-specific, but critical for performance.

One line: loading="lazy" waits to fetch offscreen images and iframes.

Frequently Asked Questions

On img and iframe it controls whether the browser may defer fetching until the element is near the viewport (lazy) or should load immediately (eager).
Usually no. Keep LCP / hero images as eager (or omit loading) so they start immediately.
No. It applies to specific elements such as img and iframe (and is widely used there). It is not a true global attribute.
No. Always set dimensions (or CSS aspect-ratio) to reduce layout shift when the image arrives.
No. It is an HTML attribute on elements, not for CSS backgrounds.
Yes for img in modern browsers. iframe lazy support is also broad in current engines.

Next: longdesc

See the deprecated longdesc attribute for long image descriptions.

longdesc tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P ๐Ÿ”—

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful