What Is an Iframe? [+ How to Embed Content With Iframes]

Download Now: Free HTML & CSS Coding Templates
Jamie Juviler
Jamie Juviler

Updated:

Published:

Embedded content is common on websites. Part of what makes the web so powerful is our ability to pull content from multiple servers to build a coherent website experience.

person sitting on the ground using a laptop to add an iframe HTML element to a web page

Take videos, for instance. How often do you see embedded YouTube videos on websites you visit? I’d guess pretty frequently. And if not, here you go:

Embedded videos can add a lot of value to your pages. They help engage, inform, and delight your visitors, as long as they appear seamless with your other content. Also, YouTube is a video hosting solution. You don’t need to store these large files on your own servers — simply upload them to your business YouTube channel.

If you want to embed YouTube videos — or any type of embeddable content — in your own website's HTML, one option is to utilize the iframe element.

Download Now: 50 Code Templates [Free Snippets]

No, iframe is not an Apple product for holding your pictures. It’s an important page element that allows for much of the cross-site compatibility we see online today. Here’s what you need to know:

This is a powerful capability in HTML — you can take any content from any website (with permission) and place it on your own site to enhance your content. 

How to Make an Iframe

To use an iframe, we use the <iframe> HTML tag. This tag requires a src (source) attribute, which specifies the URL of the HTML file to be embedded on the parent page. In its most basic form, an iframe looks like this in HTML:

<iframe src="URL"></iframe>

A simple piece of functional iframe HTML code looks like this:

<iframe src="https://www.example.com/" width="1500px" height="500px"></iframe>

This code will embed a web page onto its parent page:

 

This embedded page exists separately from the parent page in terms of its HTML, CSS, and JavaScript code. You can view that example page here.

You’ll also notice that this code snippet contains some extra attributes, width and height. These set the dimensions of the iframe region that displays the embedded file. width and height may be set as pixel values, or as percentages of the window, which scales the iframe proportionally to the size of the viewing window.

Like other HTML elements, iframes can be customized with other attributes. Here’s an overview of common iframe attributes:

attribute purpose
allow indicates what features the iframe is allowed to use (e.g. fullscreen, camera, autoplay)
allowfullscreen grants or denies permission for the iframe to appear in full-screen mode
height sets the height of the iframe (if not specified, the default height is 150 pixels)
loading sets lazy loading or eager loading for the iframe
referrerpolicy sets what referrer information should be sent in the request for the iframe
src the address of the resource included in the iframe
width sets the width of the iframe (if not specified, the default width is 300 pixels)

Iframe vs. Embed

Before we continue, you might have heard of another similar HTML5 element called embed. Like iframe, the embed element is used to embed an external resource in a web page. However, these two elements do slightly different things.

As mentioned, we use iframe to embed an HTML document onto a page. Alternatively, embed is used to embed other types of content, including PDFs, browser plugins, and Flash animations. The embed element can also be used to place media, but iframe is better for this purpose.

Because embed is really only useful for embedding outdated web technologies, you likely won’t need it — iframe is much more common and will almost always do the job. Next, let’s see what this element is capable of.

How are iframes Used?

Websites often use iframes to embed media from external websites. For example, here’s the embed code for a HubSpot YouTube video embedded at the top of this post:

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

Embedded maps are another common use of iframes. Google Maps allows you to copy embed code from any map location and display it on your website:

 

The map above is the result of this iframe code:

<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d1718.6454008049905!2d-71.07787073663287!3d42.36955552931532!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x89e370bc261e50e5%3A0xf3cba8437d505a26!2sHubSpot!5e0!3m2!1sen!2sus!4v1602001548652!5m2!1sen!2sus" width="600" height="450" frameborder="0" style="border:0;" allowfullscreen="" aria-hidden="false" tabindex="0"></iframe>

Also, display advertisements are often created with iframes. While images and gifs are static elements, a fully embedded HTML document allows for scripting. With this method, ads can be made interactive, as well as track impressions and clicks.

How to Make an Iframe Responsive

So far, we’ve seen how to implement iframes on desktop websites. But what about mobile pages? With more users viewing websites and videos through smartphones and tablets, it’s important to understand how iframes can adapt to smaller screens.

Luckily, making mobile-friendly iframes requires just a bit of CSS knowledge to pull off. Let’s take our YouTube video example again:

<iframe width="560" height="315" src="https://www.youtube.com/embed/S93nYy-Bxzo"></iframe>

For your own implementation, replace the YouTube URL within the quotes with your own YouTube embed URL.

Next, we’ll remove the width and height attributes from the HTML. When these are set, the iframe will always match these dimensions, regardless of device. We don’t want that.

<iframe src="https://www.youtube.com/embed/S93nYy-Bxzo"></iframe>

Now, we’ll enclose our iframe in a container div. This container will resize based on screen width:

<div class=”container> <iframe src="https://www.youtube.com/embed/S93nYy-Bxzo"></iframe> </div>

Lastly, apply the following CSS to the above HTML:

.container { position: relative; overflow: hidden; padding-top: 56.25%; } .container iframe { position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: 0; }

Let’s quickly unpack this styling:

The .container section applies to the <div> that holds the iframe. The padding-top property value of 56.25% is important for embedding YouTube videos specifically — it ensures the entire video is visible, no matter the width of the container. This value is calculated from the standard YouTube aspect ratio of 16:9 (i.e., 9 / 16 = .5625). For videos with different aspect ratios, the value of padding-top should be calculated the same way.

The .container iframe styling applies to the iframe itself. It sets the iframe to take up the entire space of its parent container.

When this CSS is applied, you’ll see the video conforms to the screen width:

an embedded youtube video, which adjust to the changing width of the browsing window

Using Iframes: Some Words of Caution

Iframes can be very useful, but you should be aware of their drawbacks.

First, make absolutely sure that the site or content you embed is trustworthy and secure. If attackers successfully inject harmful code into a web page that you embed, it could harm your website too.

Second, iframes can affect page performance if the embedded content takes too long to retrieve from its host. Avoid placing too many iframes on a single page (unless you’re writing an informative blog post about them), and consider taking advantage of the loading attribute:

<iframe src="URL" loading="lazy"></iframe>

“lazy” sets the iframe to load only after it enters the visitor’s view, which works to decrease the time of initial page load.

Finally, consider the effect of iframes on your pages’ SEO. Google recommends avoiding iframes and other rich media content on your website, as this content is harder to index than plain HTML content. If you still want to use an iframe, provide text-based links to any content linked in your iframe if you can so search engine crawlers can recognize them.

Iframe is one of the oldest HTML elements, and its resilience and usefulness tell us it’s here to stay for a while. For embedding content, this element is a must for your HTML tool belt.

Editor's note: This post was originally published in October 2020 and has been updated for comprehensiveness.

New Call-to-action

Topics: HTML

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Dozens of free coding templates you can start using right now

CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

START FREE OR GET A DEMO