HTML5 & Web APIs (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
How does the `loading="lazy"` attribute work on `<img>` and `<iframe>`?
senior LevelCode ExampleSystem Design
In Simple Words: Natively defers fetching off-screen images and iframes until the user scrolls near them:
html
<img src="large-image.jpg" loading="lazy" alt="Product" width="600" height="400" />
- The browser calculates distance using internal heuristics (typically 1,000px threshold).
- Completely eliminates the need for custom JavaScript IntersectionObserver libraries.
- Critical rule: Always provide
widthandheightattributes to prevent Cumulative Layout Shift (CLS) when the image loads!