Course contentsShow
← Web Development
Lesson 1953 of 2,98236. Performance Optimization TechniquesFree lesson

Lazy Loading Iframes

Applying lazy loading to iframes to prevent blocking the main thread on page load.

Lazy Loading Iframes

What you'll learn: How to defer loading iframes until they're needed, preventing them from blocking your page's initial load.

Why Iframes Block Your Page

Iframes (embedded external content like YouTube videos, maps, or ads) are heavy. When the browser encounters an <iframe>, it must fetch the external resource, establish new network connections, and process all that third-party code before continuing with the rest of your page. This can dramatically slow down your initial page load and hurt metrics like LCP and TTI.

Think of it like this: Imagine opening your front door (loading your page), but before you can step inside, you must wait for every delivery truck in the neighborhood to arrive. That's what happens when iframes load immediately!

The Native Lazy Loading Solution

Just like images, modern browsers support native lazy loading for iframes using the loading attribute:

<iframe 
  src="https://www.youtube.com/embed/dQw4w9WgXcQ"
  loading="lazy"
  width="560" 
  height="315">
</iframe>

When you add loading="lazy", the browser only downloads and renders the iframe when it's close to entering the viewport. The main thread stays free, your page loads faster, and users on slow connections save bandwidth.

When to Use It

Apply loading="lazy" to:

  • Embedded videos below the fold
  • Google Maps or other map embeds
  • Social media widgets
  • Advertisement iframes
  • Any iframe not immediately visible

Don't lazy load iframes that are critical and visible on initial load—that would delay important content unnecessarily.

Key Takeaway: Adding loading="lazy" to iframes prevents third-party content from blocking your main thread, dramatically improving page load performance with zero JavaScript required.