Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Performance – Lazy Loading Images & Ifr | HTML Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. HTML Course

Lesson 37 of 55

·HTML

Performance – Lazy Loading Images & Iframes

9 min

Course progress67%

What you'll learn

Keep these ideas in mind as you work through the lesson.

5 points
  • Native `loading='lazy'` attribute for images and iframes
  • Impact on page load performance and Core Web Vitals (LCP, CLS)
  • Fallback strategies for browsers that lack native lazy loading
  • Combining lazy loading with `srcset` for responsive images
  • Best practices to avoid layout shift

Deferring Resource Loading

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).

Polyfill for older browsers

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.

Best practices

  • Combine native lazy loading with responsive srcset to serve appropriately sized images.
  • Never lazy‑load above‑the‑fold hero images; they should load immediately.
  • Use a blur‑up placeholder or low‑quality image placeholder (LQIP) to avoid layout shift (CLS).
  • For iframes, also add sandbox and allow attributes to improve security.

Quick checklist for lazy loading

  1. loading='lazy' added to off‑screen images/iframes? ✅
  2. Polyfill present for browsers without native support? ✅
  3. Responsive srcset used for images? ✅
  4. Hero images excluded from lazy loading? ✅
  5. Placeholder or LQIP used to avoid CLS? ✅

Tip: Test with Chrome DevTools → Network throttling (e.g., Slow 3G) to see the impact on LCP and CLS.

Make it stickPractice what you've learned1 quiz · 2 practical tests · Optional activities from HTML CourseBrowse activities
Previous lessonWeb Components – Shadow DOMContinue to lesson 38Performance – Preload, Prefetch, and PreconnectBack to HTML Course curriculum