- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML Iframes
HTML Layout
HTML Iframes
An iframe shows another web page inside yours: a map, a YouTube video, a payment form, a demo. It is a window into a separate page, which is exactly why it needs a title for screen readers and often a sandbox for safety.
The other page runs on its own. Your CSS does not style it, and your JavaScript cannot reach into it if it comes from another site.
Iframe Syntax
html
<iframe src="https://www.example.com" title="Iframe Example"></iframe>srcis the page to show.titledescribes it for screen readers, like "Map of our office". Every iframe needs one.
Iframe Attributes Explained
| Attribute | Description | Example |
|---|---|---|
| src | Specifies the URL of the page to embed. | <iframe src="https://example.com"></iframe> |
| height | Sets the height of the iframe. | <iframe src="page.html" height="400"></iframe> |
| width | Sets the width of the iframe. | <iframe src="page.html" width="600"></iframe> |
| title | Provides a title for accessibility. | <iframe src="page.html" title="Demo"></iframe> |
| name | Used to name the iframe (useful for targeting links). | <iframe name="myFrame"></iframe> |
| sandbox | Adds security restrictions (disables scripts, forms, etc.). | <iframe src="page.html" sandbox></iframe> |
| allowfullscreen | Allows the iframe to display full-screen content. | <iframe src="video.html" allowfullscreen></iframe> |
| loading | Controls loading behavior (lazy or eager). | <iframe src="page.html" loading="lazy"></iframe> |
| referrerpolicy | Controls the referrer information sent when loading content. | <iframe src="page.html" referrerpolicy="no-referrer"></iframe> |
Example: Embedding a Website
html
<iframe src="https://www.openstreetmap.org/export/embed.html?bbox=77.5,12.9,77.7,13.1&layer=mapnik" width="800" height="400" title="Embedded map"></iframe>An interactive map from OpenStreetMap appears inside the page.
Example: Embedding a YouTube Video
html
<iframe width="560" height="315"
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
title="YouTube video player"
frameborder="0"
allowfullscreen>
</iframe>The YouTube player appears inside the page. Video sites give you this code from their "Share → Embed" button; you just paste it.
Example: Using the sandbox Attribute
html
<iframe src="/samples/form.html" width="600px" height= "450px" sandbox></iframe>
<!--Default sandbox value is restricted to submit any form or script. To submit the form you need to allow it with sandbox. Try using sandbox="allow-forms" and it will let you submit the form. -->With a bare sandbox attribute, the embedded page cannot run scripts, submit forms or open popups. Add back only what it needs, for example sandbox="allow-forms allow-scripts". Use it for any content you do not fully trust.
Targeting Links to an Iframe
You can load content dynamically into an iframe using the name attribute.
html
<iframe name="myFrame" width="600" height="300"></iframe>
<a href="https://www.wikipedia.org" target="myFrame">Open Wikipedia in Iframe</a>Clicking the link loads the page inside the iframe, because the link's target matches the iframe's name.
Why some sites refuse to load
Many sites, including Google and most banks, forbid being shown in an iframe for security. Their server sends a header that blocks it, and you see an empty box or an error. There is no way around it from your page; link to them instead.
Good to know
- Add
loading="lazy"to iframes further down the page, so they do not slow down the first view. - Make an iframe responsive with CSS:
iframe { width: 100%; aspect-ratio: 16 / 9; height: auto; }. - Write
widthandheightas plain numbers in HTML, likewidth="600", not600px.
Browser Support
Chrome | Edge | Firefox | Safari | Opera | IE9+ |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
HTML Iframes exercises
A page in the page
Show the page https://html5andcss3.org/samples/frame-left.html in an iframe.
Result
