HTML Iframes: Embedding External Content
HTML Iframes: Embedding External Content
Focus keyword: HTML iframe — the go-to element for embedding YouTube videos, Google Maps, payment widgets, and third-party content into a web page.
1. What Is an Iframe?
The <iframe> (inline frame) element embeds another HTML document inside the current page, rendering it as a nested browsing context. It's how sites embed a YouTube video player, an embedded map, or a payment gateway widget without leaving the page.
2. Basic Syntax
The core attribute is src, which points to the URL of the page to embed. Common supporting attributes:
width/height— sets the frame dimensionstitle— required for accessibility, describes the embedded content to screen readersloading="lazy"— a trending performance optimization that defers loading offscreen iframesallowfullscreen— lets embedded video players go fullscreen
3. Security: The sandbox Attribute
Because iframes load third-party content, they pose a security risk (clickjacking, malicious scripts). The sandbox attribute restricts what the embedded content can do — e.g., sandbox="allow-scripts" permits scripts but blocks form submission, popups, and top-level navigation unless explicitly allowed. This is a core part of modern web security best practices.
4. Responsive Iframes
By default, iframes have a fixed pixel size. For responsive design, developers commonly wrap the iframe in a container styled with position: relative and use aspect-ratio (a trending modern CSS property) to keep video embeds proportional across screen sizes.
5. Common Use Cases
- Embedding YouTube/Vimeo videos
- Embedding Google Maps locations
- Embedding payment forms (Stripe Checkout, PayPal)
- Embedding CodePen/JSFiddle demos in tutorials
Press Run to execute.
Press Run to execute.
Press Run to execute.
The iframe below has fixed width and height in pixels, so it doesn't resize on small screens. Wrap it in a container and use CSS (aspect-ratio or padding-trick) to make it responsive.
Press Run to execute.
Show expected output
The iframe is wrapped in a responsive container that maintains a 16:9 ratio across screen sizes using CSS.This is a self-check — compare your result with the expected output above.
Was this page helpful?