mc0de
Advanced HTML · Lesson 03 of 13

HTML Iframes

What if your page could display another page inside it? Discover how iframes work, how to embed useful content, and how to keep those embedded experiences safe and accessible.

HTMLEmbedded Content AccessibilityHands-On Practice
● ● ●   iframes.html
<iframe
  title="Welcome"
  srcdoc="<h2>Hello!</h2>"
></iframe>
Embedded Preview Hello from another document! 👋
01 / Getting Started

A Web Page Inside Another Web Page?

Imagine you’re creating a travel guide and want to include a map, or you’re building a portfolio and want to display a video. You don’t have to recreate that content yourself.

An iframe (inline frame) embeds a separate HTML document inside your page. The embedded document has its own content, styles, and browsing context.

Because the embedded HTML is stored inside an attribute, characters such as quotation marks and angle brackets need careful escaping. For larger documents, using a separate HTML file is usually easier to maintain.

04 / Responsive Embeds

Make Your Iframe Fit the Screen

A fixed-width iframe can overflow on mobile devices. CSS helps your embed adapt to the available space.

Responsive iframe
<style>
  .my-frame {
    display: block;
    width: 100%;
    max-width: 700px;
    height: 300px;
    border: 1px solid #cbd5e1;
    border-radius: 12px;
  }
</style>

<iframe
  class="my-frame"
  title="Embedded content"
  src="https://example.com/"
></iframe>

You can also use aspect-ratio: 16 / 9 and height: auto for video-style embeds. Choose dimensions that suit the content you’re showing.

05 / Security Matters

What Does sandbox Do?

Embedding another document comes with security considerations. The sandbox attribute applies restrictions to the iframe’s content.

A restricted iframe
<iframe
  title="Restricted preview"
  srcdoc="<h2>Safe preview</h2>"
  sandbox
></iframe>

An empty sandbox attribute applies a restrictive set of rules, including blocking scripts and forms by default. Specific permissions can be granted when genuinely necessary.

Allowing scripts selectively
<iframe
  title="Interactive demo"
  src="/demos/interactive.html"
  sandbox="allow-scripts"
></iframe>
Security tip: Avoid combining allow-scripts and allow-same-origin for same-origin embedded content you don’t fully trust. That combination can undermine the protection provided by sandboxing.
06 / Performance & Accessibility

Help Everyone Enjoy Your Embed

Every iframe loads another document, which can require additional resources. Make your embeds purposeful, accessible, and efficient.

A thoughtfully configured iframe
<iframe
  src="https://example.com/"
  title="Interactive learning resource"
  loading="lazy"
  width="640"
  height="360"
  referrerpolicy="no-referrer"
></iframe>
  • Give each iframe a meaningful title.
  • Use loading="lazy" for suitable offscreen embeds.
  • Set useful dimensions to reduce layout shifting.
  • Use trusted embed sources and appropriate security restrictions.
  • Provide an alternative link when embedded content might be unavailable.

The referrerpolicy attribute controls how much referrer information the browser sends when requesting the embedded resource.

07 / Real-World Uses

Where Will You Use Iframes?

Iframes are especially useful when another service provides an approved embed for its content.

  • Videos: Embed a video using its provider’s official embed URL.
  • Maps: Display a location with an embeddable map.
  • Documents: Present supported external documents or previews.
  • Interactive demos: Show a separate learning experience inside a page.
Pro tip: Always use the service’s official embed code when available. A normal page URL isn’t necessarily an embeddable URL.
🚀 mC0de Challenge

Create an Embedded Content Gallery

Build a mini gallery with two different embedded cards using srcdoc. You don’t need an external website — everything can work directly in the sandbox!

Your Mission

  1. Add a heading for your gallery.
  2. Create two iframe elements.
  3. Give both iframes descriptive title attributes.
  4. Use srcdoc to display different content in each iframe.
  5. Add sandbox to both iframes.
  6. Make both embeds responsive with CSS.
Extra challenge: Give each embedded card its own background color and heading. Can you make your gallery feel like a real project showcase?
💻 Interactive HTML Sandbox

Build Your Embedded Gallery!

Edit the HTML, run your code, and see the embedded documents appear in the live result. Start with one iframe, then create another!

Live Result
Lesson 03 Recap

What You’ve Learned

You’ve Opened a Window to More Content!

You now know how to embed separate documents inside your pages and make those experiences more thoughtful and secure.

  • How the <iframe> element works.
  • How to use src and srcdoc.
  • How to size iframes for different screens.
  • Why sandbox, titles, and embed permissions matter.
  • How to build your own embedded gallery.

Next up: HTML Forms — let’s make web pages that can collect information and respond to user input!

Your Learning Journey

0 / 13 Completed

Keep going! Every completed lesson brings you closer to mastering Advanced HTML.

🎉

Nice Work!

You've learned how to embed separate documents, build your own content gallery, and think about security, accessibility, and responsive design. Another awesome skill unlocked!