In HTML, an inline frame (represented by the <iframe> tag) is used to embed another HTML document directly within the current web page. Iframes are widely utilized across the web to embed YouTube videos, interactive Google Maps, third-party payment gateways, advertising units, and isolated web applications.
An <iframe> requires an opening tag and a closing tag. Any text placed between <iframe> and </iframe> serves as fallback text for obsolete browsers that do not support inline frames.
<iframe src="URL" title="Descriptive Title" width="600" height="400">
Your browser does not support inline frames.
</iframe>
<!-- Basic iframe -->
<iframe src="test.htm"></iframe>
<!-- iframe with explicit dimensions -->
<iframe src="test.htm" width="500" height="400"></iframe>
<!-- Modern borderless iframe using CSS -->
<iframe src="test.htm" style="border: none; width: 100%; height: 350px;"></iframe>
| Attribute | Requirement | Purpose & Best Practice |
|---|---|---|
src |
Required | Specifies the web URL or relative path of the embedded external document. |
title |
Mandatory (WCAG) | Provides an accessible title for assistive screen readers describing the iframe's content. |
sandbox |
Security | Enables extra security restrictions on untrusted third-party code (restricting scripts, popups, and form submissions). |
loading |
Optional (HTML5) | Set to loading="lazy" to defer loading offscreen iframes until the user scrolls near them, speeding up initial page load. |
allowfullscreen |
Optional | Allows video players inside the iframe (like YouTube or Vimeo) to expand to full-screen mode. |
srcdoc |
Optional (HTML5) | Specifies inline HTML content to display inside the frame, overriding the src attribute. |
name |
Optional | Assigns a target identifier so hyperlinks (<a target="...">) can load new pages inside this frame. |
You can use an iframe as a destination window for hyperlinks on your page. By giving the iframe a name attribute, any anchor tag with a matching target attribute will open its link inside that iframe rather than navigating away from the current page:
<!-- Navigation buttons -->
<p>
<a href="https://veewom.com/html/intro.htm" target="contentFrame">Load Introduction</a> |
<a href="https://veewom.com/html/tags.htm" target="contentFrame">Load HTML Tags</a>
</p>
<!-- Target frame -->
<iframe name="contentFrame" src="https://veewom.com/html/intro.htm" width="100%" height="300" style="border: 1px solid #ccc;"></iframe>
Embedding external websites poses security risks: malicious scripts inside the embedded frame could attempt to hijack your cookies, redirect your users, or trigger unwanted downloads. The sandbox attribute locks down the frame by default:
sandbox (empty): Applies maximum security restrictions—disables JavaScript, blocks form submissions, blocks popups, and isolates the frame in an unique origin.sandbox="allow-scripts": Allows the embedded frame to run JavaScript.sandbox="allow-forms": Permits the embedded frame to submit forms.sandbox="allow-same-origin": Treats the frame content as being from the same origin.sandbox="allow-popups": Allows the embedded frame to open new browser tabs/windows.<!-- Securely sandboxed embedded widget -->
<iframe src="https://trusted-partner.com/widget"
title="Partner Live Price Widget"
sandbox="allow-scripts allow-forms"
width="400" height="250">
</iframe>
Iframes with hardcoded pixel widths break mobile layouts by causing horizontal scrolling. Modern CSS provides the aspect-ratio property to ensure embeds scale responsively across all screen sizes:
<style>
.responsive-iframe {
width: 100%;
max-width: 720px;
aspect-ratio: 16 / 9;
border: none;
border-radius: 8px;
}
</style>
<iframe class="responsive-iframe"
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
title="HTML5 Tutorial Video"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
Assignment Scenario: Create a responsive contact and showcase page containing an embedded YouTube tutorial video and an interactive Google Maps location frame, both fully accessible with title attributes and modern responsive styling.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Responsive Iframe Embed Practice</title>
<style>
.embed-card {
max-width: 680px;
margin: 25px auto;
background: #ffffff;
border: 1px solid #e0e0e0;
border-radius: 8px;
padding: 20px;
font-family: Arial, sans-serif;
box-shadow: 0 4px 12px rgba(0,0,0,0.06);
}
.video-container {
width: 100%;
aspect-ratio: 16 / 9;
margin: 15px 0;
}
.video-container iframe {
width: 100%;
height: 100%;
border: 0;
border-radius: 6px;
}
</style>
</head>
<body style="background-color: #f4f6f9; padding: 20px;">
<div class="embed-card">
<h2>Veewom Campus & Video Lecture</h2>
<p>Watch the orientation lecture below or locate our institute using the interactive map:</p>
<!-- Responsive YouTube Embed -->
<div class="video-container">
<iframe src="https://www.youtube-nocookie.com/embed/ScMzIvxBSi4"
title="Introduction to HTML5 Web Architecture"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
</div>
<!-- Google Map Embed -->
<h3>Campus Location Map</h3>
<div class="video-container" style="aspect-ratio: 4 / 3;">
<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3671.954!2d72.5714!3d23.0225!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x0!2zMjPCsDAxJzIxLjAiTiA3MsKwMzQnMTcuMCJF!5e0!3m2!1sen!2sin!4v1600000000000"
title="Veewom Learning Center Location Map"
loading="lazy"
referrerpolicy="no-referrer-when-downgrade">
</iframe>
</div>
</div>
</body>
</html>
<iframe> (inline frame) embeds an independent external HTML document inside the body of an existing web page. In contrast, the legacy <frameset> tag replaced the entire document body with a fixed grid of frames, which caused severe usability and bookmarking issues and was officially deprecated in HTML5.
X-Frame-Options: SAMEORIGIN (or DENY) or using the Content Security Policy (CSP) directive frame-ancestors 'self'.
title attribute (e.g. "Google Maps navigation frame"). Without a title, users have no idea what content the frame contains without tediously navigating into its inner DOM tree.
sandbox attribute puts the iframe into an isolated environment with restricted privileges: it disables JavaScript, prevents form submissions, blocks window popups, and disables plugins. Developers can selectively re-enable specific features by passing values like allow-scripts or allow-forms.
src attribute points to an external URL or file path to download. The srcdoc attribute allows you to embed raw HTML code directly inline inside the attribute string. If both are specified, the browser renders the srcdoc content and uses src as a fallback for older browsers.
X-Frame-Options: DENY or SAMEORIGIN) that strictly forbid other websites from displaying their pages in an iframe. You can only embed pages from servers that allow cross-origin framing or provide dedicated embed endpoints (like YouTube's /embed/ URL).
<iframe src="..." style="border: 0;"></iframe> or in a stylesheet set iframe { border: none; }. The legacy HTML attribute frameborder="0" is obsolete in HTML5.
loading="lazy" on all iframes located below the fold so they only fetch when scrolled near view.
window.postMessage() API.