On this page11 sections
  1. Learning Objectives
  2. 1. A First Iframe
  3. 2. Size and Border With CSS
  4. 3. Embedding a Map or Video
  5. 4. Links That Open Inside an Iframe
  6. 5. When Sites Refuse
  7. 6. sandbox: Locking an Iframe Down
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 019: Iframes

Category4: Page Structure
Lecture019 of 048
Estimated video length10 min
TargetHTML5 · any modern browser
LevelBeginner

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 name and target
  • Understand why some sites refuse to be framed, and what sandbox does

1. A First Iframe

Example

html
<iframe src="https://example.com" width="400" height="200" title="The example.com home page"></iframe>
  • src: the page to show
  • width / 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

html
<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

html
<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.


Give the iframe a name, then point a link's target at that name.

Example

html
<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:

html
<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

  1. Which attribute sets the page shown in an iframe?
  2. Why should every iframe have a title?
  3. How do you remove an iframe's default border?
  4. Make a link open inside an iframe named preview.
  5. Why does a bank's website stay blank in your iframe?
  6. What does loading="lazy" do?

Practice Answers

  1. src
  2. It tells screen-reader users what the frame contains.
  3. style="border: none;"
  4. <a href="page.html" target="preview">Open</a> with <iframe name="preview" …>
  5. The bank sends a header that forbids being framed, to protect its users.
  6. 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/height or CSS; remove the border with border: none.
  • name + a link's target load pages into the frame.
  • Some sites block framing; sandbox restricts what a frame can do.