Iframes
An iframe is a window in your page that shows another web page. It is how you embed a YouTube video, a Google Map or a payment widget.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 28, 2026
Learning objectives#
- Embed content with <iframe>
- Make iframes accessible with title
- Restrict embedded pages with sandbox
- Make iframes responsive
Explanation#
<iframe src="URL"> loads a full web page inside a rectangle on your page. Each iframe is its own separate page with its own scripts.
Always give an iframe a title describing its content — screen readers announce it.
sandbox applies strict restrictions (no scripts, no forms, no pop-ups). You then add back only what is needed, e.g. sandbox="allow-scripts".
loading="lazy" delays off-screen iframes. allow="fullscreen" grants specific features to the embedded page.
Many sites refuse to be embedded (they send an X-Frame-Options or CSP header) to protect users from click-jacking.
Syntax#
<iframe src="https://example.com" title="Description" width="600" height="400" loading="lazy"></iframe>
Code example: Responsive embedded map and sandboxed page#
<style>
.embed { aspect-ratio: 16 / 9; width: 100%; max-width: 560px; border: 0; }
</style>
<iframe class="embed"
src="https://www.openstreetmap.org/export/embed.html?bbox=77.20,28.60,77.24,28.63&layer=mapnik"
title="Map of central New Delhi"
loading="lazy"></iframe>
<iframe sandbox title="Sandboxed example"
srcdoc="<h3>I am inside a sandbox</h3><script>document.write('scripts are blocked')</script>"
width="300" height="100"></iframe>
A responsive map of New Delhi and a small framed box showing only the heading, because its script was blocked by the sandbox.
Step-by-step explanation#
- aspect-ratio: 16 / 9 with width: 100% makes the iframe scale on any screen.
- title describes the map for screen-reader users.
- loading="lazy" saves bandwidth if the map is far down the page.
- srcdoc puts HTML directly into the iframe instead of loading a URL.
- The empty sandbox attribute blocks the script inside, so only the heading appears.
Real-world use case#
Contact pages embed maps; blogs embed YouTube videos; this site runs your code examples inside sandboxed iframes so they cannot touch your account.
Common mistakes#
An embedded page can run scripts and open pop-ups. Use sandbox and add back only the permissions you need.
width="800" overflows phones. Use width: 100% with aspect-ratio in CSS.
Banks, Google Search and many others block framing. A blank iframe usually means the site refuses to be embedded.
Best practices#
- Always add a title
- Use sandbox for any third-party or user content
- Lazy-load iframes below the fold
- Never combine allow-scripts and allow-same-origin for untrusted content — it cancels the sandbox
Remember this#
iframe = a window into another web page.
sandbox = “lock everything”, then allow-* to unlock one thing at a time.
title on every iframe — “what is in this window?”
Practice exercise#
Your task: Embed https://example.com in a responsive, lazy-loaded iframe with a title, allowing scripts but nothing else.
Need a hint?
Show solution
<iframe src="https://example.com" title="Example Domain homepage"
sandbox="allow-scripts" loading="lazy"
style="width:100%; aspect-ratio:4/3; border:1px solid #ccc"></iframe>
Mini quiz#
What does an empty sandbox attribute do?
Want more? Practise all HTML MCQs · HTML interview questions
My notes#
Saved in this browser.