The <iframe> (inline frame) element lets you embed one HTML page inside another. Think of it as a “window” that shows external or internal content without forcing the user to leave your site. It’s commonly used for YouTube videos, Google Maps, embedded docs, and dashboards.
sandbox attribute restricts capabilities of the embedded page.loading="lazy" for below-the-fold iframes to improve page speed.title so screen readers can identify the frame.allow attribute controls fine-grained access to features like camera, clipboard, etc.X-Frame-Options or frame-ancestors.Basic iframe syntax:
<iframe src="URL"
title="Short description"
width="600"
height="400">
</iframe>
Commonly used attributes:
srcdoc).lazy to defer loading until the iframe is near the viewport.allow-scripts, allow-forms, allow-same-origin as needed.accelerometer; clipboard-write; encrypted-media).no-referrer).
<iframe
src="https://www.example.com" <!-- URL to embed -->
title="Example site preview" <!-- Accessible name -->
width="600" height="400" <!-- Fallback sizing -->
name="exampleFrame" <!-- Target name for links -->
loading="lazy" <!-- Better performance -->
sandbox="allow-scripts allow-same-origin" <!-- Restrict capabilities -->
allow="clipboard-write; encrypted-media" <!-- Fine-grained permissions -->
referrerpolicy="no-referrer" <!-- Privacy -->
></iframe>
Below is a live iframe that attempts to load https://www.example.com. In real projects, you might embed videos, maps, or internal dashboards in the same way.
<iframe
title="Inline HTML via srcdoc"
srcdoc="<h3 style='text-align:center'>Hello from srcdoc!</h3>
<p style='text-align:center'>This HTML is inside the iframe tag.</p>"
style="width:100%; height:160px; border:2px solid #3399ff; border-radius:8px;"
sandbox
></iframe>
title so screen reader users understand what the frame contains.aspect-ratio) for responsive sizing instead of fixed pixel width/height.loading="lazy" on below-the-fold iframes to speed up initial page load.sandbox and add only the tokens you truly need.allow (e.g., just encrypted-media for media playback).X-Frame-Options or Content-Security-Policy headers.allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" plus allowfullscreen.
.iframe-wrap { aspect-ratio: 16/9; width: 100%; }
.iframe-wrap iframe { width: 100%; height: 100%; border: 0; }
<div class="iframe-wrap">....sandbox (no tokens) vs. a permissive one and observe which features stop working.srcdoc to display a small “Hello” card without any external URL. Try adding your own inline styles and HTML structure.<iframe> lets you embed another HTML document inside your page.src, title, loading, sandbox, and allow to control behavior, performance, and security.