Lazy Loading and Image Decoding
Use loading=lazy to defer offscreen images and iframes, keep it off the main hero image, and know what decoding=async and img.decode() do. Tested in Chromium.
Lazy Loading and Image Decoding
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `loading="lazy"` tells the browser to fetch an image or iframe only when it is near the viewport. Without it every image is requested at load.
- Lazy does not mean 'only when visible'. In a test with an 800 by 600 viewport, images up to 3500px down were fetched at load, one at 5000px was not, and scrolling to 3000px fetched it.
- Never lazy-load the main image at the top of the page. A lazy image inside the viewport was still requested in a test, but it waits for layout, so it can delay the largest paint.
- An image with `display: none` was not requested in a test, and a lazy iframe 5000px down was not requested either. Always give lazy images `width` and `height` so they do not shift the layout when they arrive.
- `decoding="async"` lets the browser decode off the main path, and `img.decode()` returns a promise that resolves when the image is ready. On a broken image the promise rejects with `EncodingError`.
The cost of loading everything
A long article might have thirty images. A visitor who reads the first screen and leaves has still downloaded all thirty, which wastes their data and competes with the files the first screen needs. Lazy loading defers the files the visitor hasn’t reached. Before the attribute existed, this took an IntersectionObserver and JavaScript. Now it’s one attribute:
<img src="photo.jpg" alt="..." width="800" height="600" loading="lazy">
loading also works on <iframe>. The values are eager (the default) and lazy. According to MDN’s compatibility data, img loading shipped in Chrome 77, Firefox 75 and Safari 15.4, and iframe loading in Chrome 77, Firefox 121 and Safari 16.4. The web-features data marks the feature as widely available since June 2026.
What actually gets deferred
I tested it in headless Chromium 159 with an 800 by 600 viewport and ten images placed from 0 to 9000px down the page, first plain and then with loading=lazy.
- Plain: all ten were requested at load.
- Lazy: the images at 0 through 3500px were requested at load, and the ones at 5000px and 9000px were not.
- After scrolling to 3000px, the one at 5000px was requested.
So “lazy” doesn’t mean “when visible”. The browser starts the fetch a good distance before the image reaches the viewport, so it’s ready by the time you arrive, and how far is the browser’s choice (and can depend on connection speed). Other results from the same run: a lazy image inside the viewport was still requested, an image with display: none was not, and a lazy <iframe> placed 5000px down was not requested.
The hero image rule
Don’t put loading="lazy" on the image most visible at load, usually the largest one. It’s in the viewport, so it still loads, but a lazy image waits for layout before the browser decides, and that can delay the largest contentful paint, the metric most pages are judged on. Leave the first image or two eager. The web performance lesson explains the metric, and HTML performance and rendering cost comes back to loading order. Google’s “Lazy loading demystified”, linked below, covers when not to lazy-load and the search-visibility angle.
Lazy images need width and height for the reason covered in images and alt text: without them the box has no size until the file arrives, and the page jumps. In a test with a delayed file, an image with no dimensions was 0 by 0 until it loaded.
Decoding: a separate step
Downloading an image isn’t the end. The browser must decode it, turning a compressed JPEG or AVIF into pixels, and for a large image that can take real time. The decoding attribute is a hint about how: async says don’t hold up other content for it, sync says decode with the rest, and auto (the default) leaves it to the browser. In a test decoding="async" read back as async and the default as auto. The attribute is a hint, and I did not measure a timing difference.
In scripts, img.decode() gives you a promise. It resolves when the image is ready to display without a pause, which is useful for swapping an image in without a flash. In a test a loaded 800-pixel image resolved it, and a missing file rejected with an EncodingError. A related hint, fetchpriority="high", asks for an image to jump the queue and is meant for the hero image; MDN’s compatibility data lists it in Chrome 101, Firefox 132 and Safari 17.2, and I did not test its effect.
The blurry placeholder
The embedded video spends its second half on a polish technique: show a tiny blurred version of the image as a background while the real one loads, then fade it in with a small script. It’s optional. It improves how loading feels but not what loads, and it needs the image’s dimensions, which the previous lessons already ask for.
What the video covers
The embed is Web Dev Simplified’s “How To Load Images Like A Pro” (June 2023, about 16 minutes). Its chapters are the introduction (0:00), what the problem is (0:35), lazy loading basics (2:17) and advanced blurry loading (5:15). I could not read a transcript, so I describe it from the chapters and the matching article on the creator’s blog, which I did read: it teaches loading="lazy" in its basic section and a CSS-and-JavaScript blurred placeholder in its advanced one. It does not mention decoding or width and height, so those parts and every browser result above are this lesson’s own. The deeper links are Steve Griffith on async loading and decoding (2021) and Google Search Central’s 2025 talk on lazy loading and search.
Where this fits
This ends the image sequence that started with images and alt text and passed through srcset and picture. Next, audio and video elements bring media that plays.
Check yourself
Which images should get `loading="lazy"` and which should not?
Images below the fold, such as article illustrations and galleries, should be lazy. The hero or any image visible on first load should not, since delaying it can slow the largest contentful paint. When unsure, leave the first one or two images eager.
In a test the browser fetched an image 3500px below the viewport even though it was `loading=lazy`. Is that a bug?
No. Browsers load lazy images a distance before they scroll into view so they are ready on arrival, and the distance is the browser's choice. In the test a 5000px image was not loaded until scrolling brought it within range. Do not rely on a specific threshold.
Why does a lazy image still need `width` and `height`?
While it is unloaded the browser reserves no space for it, and when it arrives everything below it jumps. The attributes give an aspect ratio so the box exists from the start. In an earlier test with a delayed file, an image without them was 0 by 0 until it loaded.
What does `img.decode()` give you that the `load` event does not?
A promise that resolves when the image is decoded and ready to paint without a hitch, so you can swap it in smoothly. The `load` event only says the bytes arrived. In a test `decode()` resolved for a good image and rejected with `EncodingError` for a missing one.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.