iframe

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 default borders, provides variant classes for visual style, and offers responsive wrapper classes for common aspect ratios.

When to Use

  • Video embeds: YouTube, Vimeo, and other video platforms
  • Maps: Google Maps, OpenStreetMap embeds
  • Social widgets: Twitter timelines, social posts
  • Payment forms: Stripe, PayPal checkout
  • Third-party tools: Calendars, forms, surveys

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

Variants

VB removes the default iframe border and provides variant classes for visual style.

ClassEffect
(default)No border, block display, max-width 100%
.borderedSubtle border with rounded corners
.roundedMedium border radius, overflow hidden
.full100% container width
.fixed100% width, fixed 400px height

Responsive Embeds

Wrap an iframe in an .embed-responsive container to maintain aspect ratio while filling available width. The iframe stretches to fill the container via absolute positioning.

Wrapper ClassAspect RatioUse Case
.embed-responsive16:9YouTube, Vimeo, most video
.ratio-4x34:3Classic presentations, some maps
.ratio-1x11:1Instagram embeds, square widgets
.ratio-21x921:9Cinematic video, ultrawide content

Live Embeds

Real-world embed examples showing YouTube video and OpenStreetMap.

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.

Security

The sandbox Attribute

Restricts iframe capabilities. Start with an empty value (most restrictive) and add permissions as needed.

Sandbox ValueAllows
allow-scriptsJavaScript execution
allow-same-originTreat content as same origin
allow-formsForm submission
allow-popupsOpen new windows/tabs
allow-modalsModal dialogs (alert, confirm)
allow-downloadsDownloading files

Content Security Policy

Referrer Policy

Accessibility

Title Attribute

Every iframe must have a title attribute that describes its content for screen readers.

Lazy Loading

Defer loading of below-the-fold iframes for better performance.

Best Practices

  • Always provide a descriptive title attribute
  • Use loading="lazy" for below-the-fold embeds
  • Ensure embedded content is itself accessible
  • Provide alternative content for critical information
  • Consider that some users block third-party content

CSS Reference

SelectorProperties
iframedisplay: block; border: none; max-inline-size: 100%;
iframe.fullinline-size: 100%;
iframe.fixedinline-size: 100%; block-size: 400px;
iframe.roundedborder-radius: var(--radius-m); overflow: hidden;
iframe.borderedborder; border-radius;
.embed-responsiveposition: relative; aspect-ratio: 16 / 9;
.embed-responsive > iframeposition: 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
  • <figure> — Wrap embeds that need captions