Inline Other Pages With `<iframe>`
Iframes & Embeds
`<iframe>` embeds another page inside this one. Useful, but isolate it with `sandbox` and pay attention to performance.
What you'll learn
- Embed an external page
- Use `sandbox` for safety
- Recognize when an embed is the wrong tool
<iframe> embeds another HTML document inside this one. YouTube
videos, Google Maps, payment forms, demos — anything that lives
at a URL can be embedded.
The Basic Form
<iframe
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
width="560"
height="315"
title="Video demo"
allowfullscreen
></iframe> src— the URL to embedwidth/height— like an imagetitle— required for accessibility — short descriptionallowfullscreen— let the iframe go fullscreen
sandbox — Security
If you embed untrusted content, lock it down:
<iframe src="/demo.html" sandbox="allow-scripts"></iframe> sandbox with no value blocks everything. Add specific
permissions back:
allow-scripts— let the iframe run JSallow-forms— let it submit formsallow-same-origin— treat as same origin (for cookies, storage)allow-popups— let it open windows
Use the least privileges that work.
Lazy Loading
<iframe src="/big-embed" loading="lazy"></iframe> Like <img>, iframes can lazy-load. Big win for pages with
several embeds below the fold.
When NOT to Embed
- A “card” with content from another page — fetch and render the content yourself instead
- Anything you can express with native HTML — embedding is heavy
Common Embeds
| Service | Embed URL pattern |
|---|---|
| YouTube | https://www.youtube.com/embed/{ID} |
| Vimeo | https://player.vimeo.com/video/{ID} |
| Google Maps | https://www.google.com/maps/embed?... |
| CodePen / JSFiddle | their own embed.html URLs |
| Twitter / X | <blockquote> + their JS (not strictly an iframe) |
Communicating With Iframes
Cross-origin iframes can’t directly access the parent’s DOM. To
exchange data, use window.postMessage(). (Detailed JS topic;
covered in the JavaScript track.)
Up Next
The semantic structural elements — <nav>, <main>, <article>,
<section>, <aside>, <header>, <footer>.