What is an iframe?
An <iframe> (inline frame) embeds another HTML page inside your current page. Think of it as a window into another web page. It's the same element YouTube, Google Maps, and payment systems use to keep external content sandboxed and secure.
Iframes are incredibly useful for:
- Embedding YouTube videos — The most common use case.
- Embedding maps — Google Maps, Mapbox, and similar services.
- External widgets — Calendars, payment forms, chat widgets.
- Embedding other web pages — Display content from another site without leaving yours.
Iframe basics and attributes
The basic syntax is simple:
Key iframe attributes:
src— The URL of the page to embed. Must be a complete URL.width, height— Size in pixels or percentages. YouTube recommends 560x315 for responsive design.title— Accessibility requirement. Describe what's inside for screen readers.loading="lazy"— Defers loading until the iframe is near the viewport. Improves performance.allowfullscreen— Allows the embedded content to go fullscreen (video players, maps).sandbox— Restricts what the iframe can do (more below).allow— Grants specific permissions (camera, microphone, geolocation).
title attribute. Screen readers announce this to users with visual impairments.
Try it yourself: Embed YouTube
Edit the code below to embed a real YouTube video. Every YouTube video has a unique embed URL. For example, the URL https://www.youtube.com/embed/VIDEO_ID embeds the video with that ID.
HTML5 video and audio elements
Rather than embedding external players via iframe, HTML5 provides native <video> and <audio> elements with built-in controls. These don't require plugins and work across all modern browsers.
Video attributes:
controls— Shows play/pause buttons, timeline, volume.width, height— Video dimensions (aspect ratio matters!).poster— Thumbnail image shown before the video starts.autoplay— Plays automatically (be careful; can annoy users).loop— Restarts from the beginning when it ends.muted— Video starts without sound (required for autoplay on some browsers).
The <source> element:
Always provide multiple formats. Different browsers support different codecs:
type="video/mp4"— .mp4 files (most compatible).type="video/webm"— .webm files (smaller, modern).type="audio/mpeg"— .mp3 audio files.type="audio/ogg"— .ogg audio files (open-source alternative).
Try it yourself: HTML5 video and audio
Even though the media files won't actually load (they don't exist), the player UI will render perfectly. In a real project, you'd replace the src URLs with real video/audio files.
The <embed> and <object> elements
<embed> and <object> are older elements for embedding external content like PDFs, plugins, and Flash files. They're less common now, but still useful in specific scenarios.
When to use each:
- <embed> — Simple, self-closing. Good for plugins and PDFs. No fallback content possible.
- <object> — More complex, allows fallback content inside the tag. Better for accessibility.
- <video> and <audio> — Preferred for media. Built-in controls, better browser support.
<video> for video content instead of <embed>.
Security: The sandbox attribute
Iframes are powerful but risky. An untrusted iframe can:
- Access your page's cookies and local storage.
- Run JavaScript that steals data.
- Break out of the iframe and attack your page.
The sandbox attribute restricts what an iframe can do. It's essential when embedding untrusted content:
Common sandbox restrictions:
sandboxalone — Blocks everything (scripts, forms, plugins, top-level navigation).allow-same-origin— Allows access to same-origin resources (cookies, etc.).allow-scripts— Allows JavaScript execution.allow-forms— Allows form submission.allow-popups— Allows opening new windows.allow-top-navigation— Allows the iframe to navigate the top-level page.
sandbox for untrusted content. Start restrictive and only add permissions you need. YouTube, Google Maps, and payment processors are trusted services — sandboxing isn't necessary for them.
Task: Create an HTML page that combines all three embedding methods:
- Embed a YouTube video using an iframe with proper attributes (src, title, allowfullscreen)
- Add an HTML5 video element with controls and a poster image
- Add an HTML5 audio player with controls
- Include descriptive headings and text for each media section
- Add proper accessibility attributes (title on iframe)
Focus on:
- Using semantic HTML structure (sections, headings)
- Including fallback text or messages
- Proper video dimensions (aspect ratio 16:9 is standard)
Hint: Use the editors above as templates. Combine both the YouTube iframe editor and the HTML5 video/audio editor into one page. Add a page title and introduction, then sections for each media type. Remember: the actual media files won't load in our preview, but the players will render!
Key takeaways
<iframe>embeds another HTML page. Always includetitlefor accessibility andloading="lazy"for performance.<video>and<audio>are native HTML5 elements with built-in controls. Provide multiple<source>formats for browser compatibility.- Use
posteron videos to show a thumbnail before playback. <embed>and<object>are older, less common alternatives for PDFs and plugins. Prefer HTML5 video/audio.- Always use the
sandboxattribute for untrusted iframes. It prevents scripts and attacks from affecting your page. - Trusted services like YouTube and Google Maps don't need sandboxing, but always include
allowfullscreenso users can expand the embed.
Test your knowledge
You're mastering HTML.
Now embed any content you want.
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →