Lesson 37 of 55
HTMLPerformance – Lazy Loading Images & Iframes
9 min
Course progress67%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 37 of 55
HTML9 min
Keep these ideas in mind as you work through the lesson.
Modern browsers support the native loading attribute.
<img src='large.jpg' alt='Large picture' loading='lazy' />
<iframe src='https://example.com' loading='lazy' title='Example site'></iframe>
Elements load only when they approach the viewport, reducing initial page weight and improving Largest Contentful Paint (LCP).
if ('loading' in HTMLImageElement.prototype === false) {
const script = document.createElement('script');
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/lazysizes/5.3.2/lazysizes.min.js';
document.body.appendChild(script);
}
Lazysizes adds the lazyload class to images and iframes that lack native support.
srcset to serve appropriately sized images.sandbox and allow attributes to improve security.loading='lazy' added to off‑screen images/iframes? ✅srcset used for images? ✅Tip: Test with Chrome DevTools → Network throttling (e.g., Slow 3G) to see the impact on LCP and CLS.