Iframe in HTML is created with the <iframe> element. It embeds another HTML page or external resource inside the current page. Iframes are commonly used for YouTube videos, Google Maps, forms, dashboards, ads, documentation previews, payment widgets, and embedded tools.
An iframe is powerful because it can show content from a different page without merging that page into your HTML document. At the same time, iframes need careful handling because they affect performance, accessibility, layout, privacy, and security. A careless iframe can slow a page, confuse users, or create security risks.
In this article, we will understand iframe syntax, the src and title attributes, width and height, responsive embeds, lazy loading, sandboxing, permissions, security concerns, and best practices for using iframes correctly.
What is an Iframe in HTML?
An iframe, short for inline frame, displays another document inside the current HTML page. The embedded page has its own browsing context. This means it can load its own HTML, CSS, JavaScript, media, and resources separately from the parent page.
The iframe element has an opening tag and a closing tag. The most important attribute is src, which defines the page or resource to embed. A useful iframe should also have a descriptive title attribute for accessibility.
<iframe src="https://example.com" title="Example website preview"></iframe>Use an iframe when you need to embed a separate page or third-party resource. Do not use iframes as a normal layout shortcut.
Basic Syntax of iframe in HTML
The basic iframe syntax includes the src attribute and the title attribute. The title is important because screen reader users need to understand what the embedded frame contains before entering it.
<iframe
src="https://example.com"
title="Example website">
</iframe>Without a good title, users of assistive technology may hear only that a frame exists, without knowing its purpose. Use a short, specific title such as Map showing store location or Video lesson: HTML basics.
| Attribute | Example | Purpose |
|---|---|---|
src | src="page.html" | Defines the embedded page or resource. |
title | title="Contact form" | Describes the iframe for accessibility. |
width | width="800" | Sets iframe width. |
height | height="450" | Sets iframe height. |
loading | loading="lazy" | Delays loading until needed. |
Embedding a Web Page with iframe
An iframe can embed another page from the same website or a different website. Same-site iframes can be useful for previews or internal tools. Cross-site iframes are common for third-party embeds such as videos, maps, and widgets.
<iframe
src="/examples/html-preview.html"
title="HTML preview"
width="800"
height="400">
</iframe>If the embedded page is from another domain, that site can decide whether it allows being embedded. Some websites block iframe embedding using security headers such as X-Frame-Options or Content-Security-Policy. If a site blocks embedding, your iframe will not display it properly.
Embedding YouTube Videos and Maps
Many platforms provide iframe embed code. YouTube, Google Maps, CodePen, and many form tools generate iframe snippets that you can paste into a page. Always review the code before using it, especially the width, height, title, loading, permissions, and privacy-related attributes.
<iframe
width="560"
height="315"
src="https://www.youtube.com/embed/video-id"
title="HTML tutorial video"
loading="lazy"
allowfullscreen>
</iframe>This example embeds a YouTube video. The allowfullscreen attribute allows the user to open the video in full-screen mode. The loading="lazy" attribute delays loading until the iframe is near the viewport, which can improve page performance.
Width, Height, and Responsive Iframes
Traditional iframe code often includes fixed width and height. Fixed sizes can work on desktop but may overflow on mobile screens. A responsive iframe adjusts to the available width while keeping a useful aspect ratio.
<div style="position:relative; padding-bottom:56.25%; height:0; overflow:hidden;">
<iframe
src="https://www.youtube.com/embed/video-id"
title="Responsive video"
style="position:absolute; top:0; left:0; width:100%; height:100%;"
allowfullscreen>
</iframe>
</div>This pattern creates a 16:9 responsive embed. In production, CSS classes are cleaner than inline styles, but the example shows the concept. The wrapper controls the aspect ratio, and the iframe fills the wrapper.
Lazy Loading Iframes
Iframes can be heavy because each iframe may load a separate page, scripts, media, fonts, tracking resources, and network requests. The loading="lazy" attribute tells the browser to delay loading the iframe until it is closer to the visible area.
<iframe
src="https://example.com/embed"
title="Embedded demo"
loading="lazy">
</iframe>Lazy loading is useful for maps, videos, social embeds, and lower-page widgets. Avoid lazy loading an iframe that must be visible and usable immediately at the top of the page.
Quick lazy loading rule
Use loading="lazy" for non-critical iframes below the first screen. It can reduce initial page weight.
sandbox Attribute in iframe
The sandbox attribute applies restrictions to the content inside an iframe. By default, a sandboxed iframe has many capabilities disabled. You can selectively allow specific features with sandbox tokens. This is important when embedding content that should not have full power.
<iframe
src="demo.html"
title="Sandboxed demo"
sandbox="allow-scripts allow-forms">
</iframe>This iframe allows scripts and forms, but still restricts other behavior. Sandbox settings should be chosen carefully. Giving too many permissions removes much of the benefit. For untrusted content, be strict.
| Sandbox Token | Meaning |
|---|---|
allow-scripts | Allows scripts to run inside the iframe. |
allow-forms | Allows form submission. |
allow-same-origin | Allows the iframe to keep its origin rules. |
allow-popups | Allows popups from the iframe. |
allow-modals | Allows modal dialogs. |
allow Attribute and Permissions
The allow attribute controls browser features that an iframe may use, such as camera, microphone, fullscreen, clipboard, autoplay, or payment. Third-party embed code often includes this attribute. Review it instead of blindly accepting unnecessary permissions.
<iframe
src="https://example.com/video"
title="Video player"
allow="fullscreen; autoplay"
allowfullscreen>
</iframe>Only grant permissions that the embedded content truly needs. For example, a map does not need microphone access. A video player may need fullscreen, but not camera access.
Security and Privacy Concerns
Iframes create a boundary between the parent page and embedded content, but they still introduce risk. Third-party iframes can load scripts, set cookies, track users, slow the page, or display content you do not fully control. That is why iframe usage should be intentional.
- Embed only trusted sources.
- Use
sandboxfor untrusted or limited content. - Use
loading="lazy"for below-the-fold embeds. - Review
allowpermissions before publishing. - Give every iframe a meaningful
title. - Avoid too many iframes on one page because each can add performance cost.
Common Mistakes with Iframes
A common mistake is embedding a third-party page without a title. This creates accessibility problems. Another mistake is using fixed iframe widths that break mobile layouts. A third mistake is adding many iframes to a page without considering performance.
Some beginners also try to use iframes as a replacement for normal HTML layout. That is not a good use case. For page sections, use semantic HTML and CSS layout. Use iframes only when the embedded content is truly a separate document or external resource.
Best Practices for iframe in HTML
- Always add a descriptive
titleattribute. - Use responsive iframe wrappers for videos and maps.
- Use
loading="lazy"for non-critical embeds. - Use
sandboxwhen embedding limited or untrusted content. - Avoid granting unnecessary permissions through
allow. - Do not use iframes for normal page layout.
- Test iframe behavior on mobile screens.
When to Use an Iframe and When to Avoid It
Use an iframe when the embedded content is truly separate from the current page. Good examples include a map, a hosted video player, a payment widget, an external form, or a live preview that must stay isolated from the parent document. In these cases, the iframe boundary is useful because the external resource can work independently.
Avoid iframes for normal article content, layout sections, repeated cards, menus, or text that should belong to the page itself. Important content inside an iframe can be harder to style, harder to index, slower to load, and less consistent across devices. If the content is yours and belongs to the page, write it directly in HTML instead of embedding it through an iframe.
A practical rule is simple: if the content needs isolation, an iframe may be correct. If the content only needs layout, use semantic HTML and CSS.
Iframe SEO and Performance Notes
Content inside an iframe is not the same as normal page content. If the embedded item contains important information, add surrounding text that explains it on the parent page. For example, a map iframe should be accompanied by the address, and a video iframe should have a short description or transcript when relevant.
Performance can also become an issue because each iframe may load a separate page with its own scripts and network requests. Use iframes only when they add real value, lazy load non-critical embeds, and avoid stacking many third-party frames on a single tutorial page.
FAQs
What is iframe in HTML? 3
An iframe is an inline frame that embeds another HTML page or external resource inside the current page.
Which attribute is required for iframe? 3
The src attribute defines what the iframe loads. A descriptive title attribute should also be included for accessibility.
Are iframes bad for SEO? 3
Iframes are not automatically bad, but content inside an iframe may not be treated like normal page content. Use iframes for embeds, not for important article text.
How do I make an iframe responsive? 3
Use a responsive wrapper that maintains aspect ratio and set the iframe to fill that wrapper. This is common for videos and maps.
What does sandbox do in iframe? 3
The sandbox attribute restricts what iframe content can do. Specific permissions can be re-enabled using sandbox tokens.
Continue learning HTML in order
Follow the topic sequence with the previous and next lesson.