Up to date
Published
2 min read

Trevor I. Lasn

Staff Software Engineer, Engineering Manager

Improve PageSpeed Insights Score with Lazy Loading Iframes

How to save bandwidth and speed up your site by lazy-loading iframes

Lazy-loading images is pretty common these days, but did you know you can do the same thing with iframes?

By deferring offscreen iframes from being loaded until the user scrolls near them, you can significantly improve your site’s performance.

Let’s break down how to implement lazy-loading iframes, why it’s useful, and what browser support looks like.

Before and After: Adding Lazy Loading to Iframes

Here’s an iframe before adding lazy-loading:

<iframe
src="https://trevorlasn.substack.com/embed"
class="w-full"
height="320"
title="Trevor I. Lasn newsletter"
frameborder="0"
scrolling="no"
>
</iframe>

Now, let’s add the loading="lazy" attribute:

<iframe
loading="lazy"
src="https://trevorlasn.substack.com/embed"
class="w-full"
height="320"
title="Trevor I. Lasn newsletter"
frameborder="0"
scrolling="no"
>
</iframe>

That’s it! With just one attribute, the iframe will load only when the user scrolls near it.

Why Lazy-Load Iframes?

Iframes are often used to embed third-party content, like YouTube videos, social media posts, or ads. Most of this content doesn’t appear immediately in the user’s viewport. But when loaded eagerly, users still pay the cost of downloading the iframe’s data and JavaScript, even if they never scroll down to it.

For example, loading a YouTube embed upfront can pull in over 500 KB of resources that users might not even need. By lazy-loading iframes, you defer loading them until users scroll near them. This saves bandwidth and improves page performance, especially for mobile users or those on slower connections.

How Lazy Loading Works

When you add loading="lazy" to an iframe, the browser delays loading it until it’s close to the user’s viewport. This is particularly useful for content that appears below the fold.

Here’s an example:

<iframe
src="https://www.youtube.com/embed/YJGCZCaIZkQ"
loading="lazy"
width="560"
height="315"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
>
</iframe>

Without lazy-loading, this iframe would load as soon as the page starts loading, even if the user never scrolls down to it. But with the loading="lazy attribute, it only loads when the user approaches it, saving bandwidth and improving your site’s Time to Interactive (TTI).

Browser Support

Lazy-loading for iframes is supported in most modern browsers:

  • Chrome: 77+
  • Edge: 79+
  • Firefox: 121+
  • Safari: 16.4+

This wide browser support means you can confidently implement lazy-loading without worrying about breaking your site for most users.


Found this article helpful? You might enjoy my free newsletter. I share dev tips and insights to help you grow your coding skills and advance your tech career.

Interested in supporting this blog in exchange for a shoutout? Get in touch.


Liked this post?

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

Webdev
8 min read

Invisible columns in SQL

It’s a small feature, but it can make a big difference.

Aug 26, 2024
Read article
Webdev
3 min read

CSS content-visibility: The Web Performance Boost You Might Be Missing

The content-visibility CSS property delays rendering an element, including layout and painting, until it is needed

Dec 5, 2024
Read article
Webdev
3 min read

CSS @supports: Write Future-Proof CSS

Detect CSS feature support and provide smart fallbacks with @supports

Dec 6, 2024
Read article
Webdev
3 min read

CSS ::target-text for Text Highlighting

A look at how browsers can highlight text fragments using CSS ::target-text, making text sharing and navigation more user-friendly

Dec 17, 2024
Read article
Webdev
3 min read

CSS Supports Nesting Now

CSS nesting is finally supported in all major browsers. Write cleaner, organized stylesheets without Sass or Less

Dec 6, 2024
Read article
Webdev
4 min read

Explicit is better than implicit

Clarity is key: being explicit makes your code more readable and maintainable.

Sep 4, 2024
Read article
Webdev
6 min read

Inside the CSS Engine: CSSOM Explained

A deep dive into how browsers parse and manipulate CSS, its impact on web performance, and why it matters

Oct 25, 2024
Read article
Webdev
3 min read

Form Validation That Doesn't Annoy Users: CSS :user-valid and :user-invalid

The new pseudo-classes :user-valid and :user-invalid give us a smarter way to style form validation states based on user interaction

Dec 12, 2024
Read article
Webdev
5 min read

SecretLint — A Linter for Preventing Committing Credentials

A guide to catching and preventing credential leaks in your code using Secretlint

Oct 22, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/lazy-loading-iframes. It was written by a human and polished using grammar tools for clarity.