On this page11 sections
Lecture 019: Iframes
| Category | 4: Page Structure |
| Lecture | 019 of 048 |
| Estimated video length | 10 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
An iframe (inline frame) shows another web page inside a box on your page. It's how sites embed YouTube videos, Google Maps and payment forms. The code examples on this site run inside iframes too.
Learning Objectives
- Embed a page with
<iframe src="…"> - Set its size, border and title
- Open links inside an iframe with
nameandtarget - Understand why some sites refuse to be framed, and what
sandboxdoes
1. A First Iframe
Example
<iframe src="https://example.com" width="400" height="200" title="The example.com home page"></iframe>src: the page to showwidth/height: the size of the box (pixels by default)title: describes the frame for screen readers. Always include it
2. Size and Border With CSS
Example
<iframe src="https://example.com" title="Example site"
style="width: 100%; height: 220px; border: 2px solid #4c6ef5; border-radius: 8px;"></iframe>Remove the default border with border: none;.
3. Embedding a Map or Video
Sites like Google Maps and YouTube give you a ready-made iframe under Share → Embed.
Example
<iframe width="400" height="225"
src="https://www.youtube-nocookie.com/embed/NrO0CJCbYLA"
title="Introduction to Dart (YouTube)"
allow="fullscreen" loading="lazy"></iframe>loading="lazy" waits until the visitor scrolls near the frame before loading it.
4. Links That Open Inside an Iframe
Give the iframe a name, then point a link's target at that name.
Example
<iframe name="viewer" src="https://example.com" title="Viewer" width="400" height="160"></iframe>
<p>
<a href="https://example.org" target="viewer">Show example.org in the box</a>
</p>5. When Sites Refuse
Many sites (banks, Google Search, most social networks) send a header that forbids being shown in an iframe, to protect their users. The frame then stays blank or shows an error. Only embed pages that offer an embed option.
6. sandbox: Locking an Iframe Down
sandbox blocks scripts, forms, pop-ups and more inside the frame. You switch things back
on one by one:
<iframe src="widget.html" sandbox="allow-scripts" title="Widget"></iframe>Use it when embedding content you don't fully trust.
Common Beginner Mistakes
1. No title. Screen readers just say "frame".
2. Framing sites that block it. If the frame is blank, the site probably doesn't allow embedding.
3. Iframes for your own layout. Old sites built pages out of frames. Use normal HTML and CSS instead.
4. Too many iframes. Each one loads a whole page. Add loading="lazy" and keep them few.
Practice Questions
- Which attribute sets the page shown in an iframe?
- Why should every iframe have a
title? - How do you remove an iframe's default border?
- Make a link open inside an iframe named
preview. - Why does a bank's website stay blank in your iframe?
- What does
loading="lazy"do?
Practice Answers
src- It tells screen-reader users what the frame contains.
style="border: none;"<a href="page.html" target="preview">Open</a>with<iframe name="preview" …>- The bank sends a header that forbids being framed, to protect its users.
- It delays loading the frame until the visitor scrolls near it.
Key Takeaways
<iframe src="…" title="…">shows another page inside yours.- Size it with
width/heightor CSS; remove the border withborder: none. name+ a link'stargetload pages into the frame.- Some sites block framing;
sandboxrestricts what a frame can do.