The iframe element embeds external content with responsive containers and security best practices for third-party integrations.
Description
The <iframe> element embeds another HTML document within the current page. VB removes the default border, makes the frame block-level and never wider than its container, and offers variant classes, a data-ratio attribute for proportional sizing, and ratio wrappers for common embeds.
When to Use
- Video embeds: YouTube, Vimeo, and other video platforms
- Maps: OpenStreetMap and other map embeds (or
<geo-map> for a tile map you control)
- Social widgets: Timelines and posts
- Payment forms: Hosted checkout fields
- Third-party tools: Calendars, forms, surveys
- Untrusted HTML: User-supplied markup rendered with
sandbox and srcdoc
When to Use Other Elements
- Use
<video> for self-hosted video files
- Use
<audio> for self-hosted audio
- Use
<dialog> for modal content on your own page
- Use
<object> for PDFs that need a download-link fallback
Variants
VB removes the default iframe border and provides variant classes for visual style and width.
1
<iframe src="page.html" title="Descriptive title" width="600" height="400"></iframe>
1
<iframe class="full bordered" src="page.html" title="..." height="300"></iframe>
| Class | Effect |
| (default) | No border, block display, max-inline-size: 100%. Size comes from the width/height attributes (UA default 300×150). |
.bordered | Subtle border with rounded corners |
.rounded | Medium border radius, overflow hidden |
.full | 100% container width; the height attribute is kept |
.fixed | 100% width, fixed 400px height (overrides height) |
Responsive Sizing with data-ratio
An iframe has no natural aspect ratio, so its width and height attributes don't scale together: on a narrow screen max-inline-size clamps the width and leaves the full height, giving a tall letterbox. Put data-ratio on the iframe itself and the block size follows the used width instead.
1234567891011
<iframe src="https://www.youtube-nocookie.com/embed/VIDEO_ID" title="Video title" width="560" height="315" data-ratio="16:9" allow="fullscreen; picture-in-picture" loading="lazy"></iframe> <iframe src="/report.html" title="Quarterly report" data-ratio="page"></iframe>
- A
width attribute stays the preferred (maximum) width; without one the iframe fills its container.
- Keep integer
width/height as the no-CSS fallback.
- Values:
1:1, 4:3, 3:2, 16:9 (default), 21:9, 2:3, 3:4, 9:16, and page (1 : 1.414, a portrait document page). The same vocabulary works on <embed> and <object>.
When an embed needs a caption, rounded corners, or a wrapper you can lay out, use a [data-media] ratio box. Its child iframe fills the box. On a <figure> with a <figcaption>, the ratio moves to the iframe and the caption flows below it (see figure).
123456789
<figure data-media data-ratio="16:9" data-radius="m"> <iframe src="https://www.youtube-nocookie.com/embed/VIDEO_ID" title="Product walkthrough video" allow="fullscreen; picture-in-picture" loading="lazy"> </iframe> <figcaption>Product walkthrough (4 minutes)</figcaption></figure>
Legacy .embed-responsive wrapper
The .embed-responsive class works the same way as [data-media] and accepts ratio modifier classes. Prefer data-ratio for new markup.
123
<figure class="embed-responsive ratio-4x3"> <iframe src="..." title="..."></iframe></figure>
| Wrapper Class | Aspect Ratio | Use Case |
.embed-responsive | 16:9 | YouTube, Vimeo, most video |
.ratio-4x3 | 4:3 | Classic presentations, some maps |
.ratio-1x1 | 1:1 | Square widgets |
.ratio-21x9 | 21:9 | Cinematic video, ultrawide content |
Live Embeds
Real-world embed examples showing YouTube video and OpenStreetMap.
12345678
<iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" title="YouTube video player" width="560" height="315" data-ratio="16:9" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; fullscreen" referrerpolicy="strict-origin-when-cross-origin" loading="lazy"></iframe>
123456
<iframe src="https://www.openstreetmap.org/export/embed.html?bbox=..." title="Map showing office location" width="500" height="375" data-ratio="4:3" loading="lazy"></iframe>
Privacy note: Use youtube-nocookie.com instead of youtube.com for privacy-enhanced mode that doesn't set tracking cookies until the user plays the video.
Attributes
| Attribute | Purpose |
title | Accessible name. Required (see Accessibility). |
src | URL of the embedded document. |
srcdoc | Inline HTML to render instead of src (wins when both are set). Pair with sandbox for untrusted markup. |
width, height | Integer CSS pixels. The fallback size, and the preferred width with data-ratio. |
allow | Permissions Policy for the frame (see below). |
sandbox | Restricts what the framed document can do (see Security). |
loading | lazy defers loading until the frame nears the viewport (see Performance). |
referrerpolicy | How much of your URL is sent to the embedded site, e.g. no-referrer or strict-origin-when-cross-origin. |
credentialless | Loads the frame in a fresh, ephemeral context without your cookies or storage, so third-party embeds work on pages with cross-origin isolation (COEP). Chromium only at present; other browsers ignore it. |
name | Browsing-context name, a target for <a target> and <form target>. |
data-ratio | VB: proportional sizing (see above). |
data-mock | VB prototyping: an iframe with no src gets a labelled placeholder via srcdoc. See data-mock. |
The allow attribute (Permissions Policy)
allow takes a semicolon-separated list of features the framed document may use. Each feature defaults to the frame's own origin (src); add an origin list or * to widen it.
12345
<iframe src="https://player.example.com/embed/42" title="Conference keynote video" allow="fullscreen; picture-in-picture; autoplay 'src'; geolocation 'none'"></iframe>
Use allow="fullscreen" rather than the legacy allowfullscreen attribute. Both work today; allowfullscreen is the older boolean form (equivalent to allow="fullscreen *"), and when allow also names fullscreen the allow value takes precedence. Keep to one vocabulary.
Security
The sandbox Attribute
Restricts iframe capabilities. Start with an empty value (most restrictive) and add tokens only as needed.
Never combine allow-scripts and allow-same-origin for content from your own origin. A same-origin document that may run scripts can reach into its parent, remove the sandbox attribute from its own iframe and reload itself unsandboxed: the sandbox is no protection at all. The pair is only meaningful for a cross-origin src, where the same-origin policy still separates the frame from your page. For untrusted HTML, use srcdoc or a separate origin with sandbox="allow-scripts" alone.
12345678910111213141516171819
<iframe sandbox src="..." title="..."></iframe> <iframe sandbox="allow-scripts" srcdoc="..." title="..."></iframe> <iframe sandbox="allow-scripts allow-same-origin allow-popups" src="https://widgets.example.com/embed" title="..."></iframe> <iframe sandbox="allow-forms allow-scripts allow-same-origin" src="https://forms.example.com/survey" title="..."></iframe>
| Sandbox Token | Allows |
allow-scripts | JavaScript execution |
allow-same-origin | Keep the document's real origin (cookies, storage, same-origin DOM access). Without it the frame gets an opaque origin. |
allow-forms | Form submission |
allow-popups | Open new windows/tabs (window.open, target="_blank") |
allow-popups-to-escape-sandbox | Popups open without inheriting the sandbox (needed for links out to normal sites) |
allow-modals | alert(), confirm(), prompt(), print() |
allow-downloads | Downloading files |
allow-top-navigation | Navigate the top-level page at any time |
allow-top-navigation-by-user-activation | Navigate the top-level page only after a user click (prefer this to allow-top-navigation) |
allow-top-navigation-to-custom-protocols | Open mailto:, tel: and other non-HTTP links in the top-level page |
allow-presentation | Start a Presentation API session (casting) |
allow-orientation-lock | Lock screen orientation |
allow-pointer-lock | Pointer Lock API (games, 3D viewers) |
allow-storage-access-by-user-activation | Request unpartitioned storage via the Storage Access API |
Content Security Policy
List exactly the origins you embed. The privacy-enhanced YouTube host is www.youtube-nocookie.com, not www.youtube.com.
123456
Content-Security-Policy: frame-src 'self' https://www.youtube-nocookie.com https://player.vimeo.com; <meta http-equiv="Content-Security-Policy" content="frame-src 'self' https://www.youtube-nocookie.com;">
To control who may frame your pages, send Content-Security-Policy: frame-ancestors from the embedded site; it can't be set from a meta tag.
Referrer Policy
12345
<iframe referrerpolicy="no-referrer" src="..." title="..."></iframe>
Performance
Lazy Loading
Defer loading of below-the-fold iframes. Third-party embeds often pull in megabytes of script, so this is one of the cheapest wins on a page. Don't lazy-load an embed that is visible on first paint.
123456
<iframe src="..." title="..." width="560" height="315" data-ratio="16:9" loading="lazy"></iframe>
Give lazy iframes a size (width/height or data-ratio) so the space is reserved and nothing shifts when they load.
Printing
Browsers print only the visible part of an iframe, cropped to its box; scrolled-away content and video are lost. If the embedded content matters on paper, follow the frame with a link or summary that prints instead.
1234567
<figure data-media data-ratio="16:9"> <iframe src="https://www.youtube-nocookie.com/embed/VIDEO_ID" title="Setup walkthrough video" loading="lazy"></iframe> <figcaption> Setup walkthrough — <a href="https://www.youtube.com/watch?v=VIDEO_ID">watch on YouTube</a> </figcaption></figure>
See Printing for how VB prints embedded media.
Accessibility
Title Attribute
Every iframe must have a title attribute that describes its content for screen readers.
123456789
<iframe src="https://www.youtube-nocookie.com/embed/..." title="Introduction to Web Accessibility - Tutorial Video"></iframe> <iframe src="..."></iframe><iframe src="..." title="iframe"></iframe>
Best Practices
- Always provide a descriptive
title attribute
- Ensure embedded content is itself accessible; you can't fix it from the parent page
- Provide alternative content (a link, transcript or summary) for critical information
- Consider that some users block third-party content
- Don't put
tabindex="-1" on an interactive embed; keyboard users need to reach its controls
CSS Reference
Styles defined in /src/native-elements/iframe/styles.css and /src/utils/media.css.
| Selector | Properties |
iframe | display: block; border: none; max-inline-size: 100%; |
iframe.full | inline-size: 100%; (block size from height) |
iframe.fixed | inline-size: 100%; block-size: 400px; |
iframe.rounded | border-radius: var(--radius-m); overflow: hidden; |
iframe.bordered | border; border-radius; |
:is(iframe, embed, object)[data-ratio] | block-size: auto; aspect-ratio: var(--_ratio, 16 / 9); plus inline-size: 100% when there is no width attribute |
[data-media] > iframe | position: absolute; inset: 0; 100% width/height |
.embed-responsive | position: relative; inline-size: 100%; aspect-ratio: 16 / 9; |
.embed-responsive > :is(iframe, embed, object) | position: absolute; inset: 0; 100% width/height |
Related
<video> — For self-hosted video content
<audio> — For self-hosted audio content
<dialog> — For modal content on your own page
<object> — Legacy embed mechanism, PDF fallback
<embed> — Same data-ratio sizing
<figure> — Wrap embeds that need captions
data-mock — Placeholder iframes for prototypes
sandbox — Lock down what an iframe may do
srcdoc — Inline HTML documents for iframes
allow — Permissions Policy for iframes