Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 4 of 7

Embedding & iframes

Learn to embed external content in your HTML pages using iframes - YouTube videos, Google Maps, CodePen demos, and third-party widgets. Understand security considerations like the sandbox attribute and CSP headers.

25 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

The iframe Element

An <iframe> (inline frame) embeds another HTML page within your page. It creates a separate browsing context with its own document. Common uses: YouTube/Vimeo video embeds, Google Maps, payment forms (Stripe), social media widgets, and code playground demos. Always include a title attribute for accessibility - screen readers announce it as the frame's purpose.

Diagram
Loading diagram…

The sandbox attribute restricts untrusted embedded third-party content

iframe Security

  • sandbox attribute - Restricts iframe capabilities. sandbox='allow-scripts allow-same-origin' limits what the embedded page can do
  • Never embed untrusted content without sandbox - malicious iframes can attempt clickjacking attacks
  • Content Security Policy (CSP) - Set frame-src directive to whitelist allowed iframe sources
  • X-Frame-Options header - Controls whether YOUR page can be embedded in someone else's iframe
  • loading='lazy' - Defer iframe loading until user scrolls near it. Critical for pages with YouTube embeds
  • referrerpolicy='no-referrer' - Prevents sending your page URL to the embedded site for privacy

Embedding Code Examples

Embedding Code Examples
<!-- Video player embed -->
<iframe
  width="560" height="315"
  src="https://player.example.com/embed/sample-video"
  title="Sample educational course video"
  frameborder="0"
  allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
  allowfullscreen
  loading="lazy"
></iframe>

<!-- Google Maps embed -->
<iframe
  src="https://www.google.com/maps/embed?pb=!1m18!..."
  width="600" height="450"
  style="border:0;"
  allowfullscreen=""
  loading="lazy"
  referrerpolicy="no-referrer-when-downgrade"
  title="Google Maps location"
></iframe>

<!-- Sandboxed iframe for security -->
<iframe
  src="https://untrusted-site.com/widget"
  sandbox="allow-scripts allow-popups"
  width="400" height="300"
  title="Third-party widget"
></iframe>

Quick Quiz: Embedding & iframes

Key Takeaways

  • Learn to embed external content in your HTML pages using iframes - YouTube videos, Google Maps, CodePen demos, and third-party widgets.
  • sandbox attribute - Restricts iframe capabilities. sandbox='allow-scripts allow-same-origin' limits what the embedded page can do
  • Never embed untrusted content without sandbox - malicious iframes can attempt clickjacking attacks
  • Content Security Policy (CSP) - Set frame-src directive to whitelist allowed iframe sources

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/7 done