How to Embed Videos and Pages With Responsive, Secure Iframes

Learn how iframes work, how to keep embeds responsive at any aspect ratio, and which title, loading, allow and referrer settings make them fast and safe.

Updated September 29, 2026 6 min read

Use the free Iframe GeneratorCreate responsive iframe embed code with a live preview frame

Embedding something from another site is one of the most common tasks on the web: a product video, a map, a booking widget, a form or a slide deck. The platform gives you a snippet, you paste it in, and on a phone the video overflows the screen or leaves an awkward black bar. Or the frame shows "refused to connect" and you have no idea why.

This guide explains how iframes work, the standard technique for keeping them responsive at any aspect ratio, what each important attribute does for performance, privacy and accessibility, and how to generate a clean embed code.

What an iframe is

An <iframe> (inline frame) displays one web page inside another. The embedded page is loaded from its own server and runs in its own browsing context. Your page controls where the frame sits and how big it is; the embedded site controls what appears inside it.

A basic embed looks like this:

<iframe src="https://www.example.com/widget" title="Booking calendar" width="600" height="400"></iframe>

Two consequences follow from how iframes work:

  • The embedded site decides whether it can be framed. Sites can send an X-Frame-Options header or a Content-Security-Policy with frame-ancestors to refuse. If they do, no code on your side can override it.
  • You decide what the frame is allowed to do. Attributes like allow, sandbox and referrerpolicy limit what the embedded page can access.

Making iframes responsive

An iframe with fixed width and height does not shrink with the screen. Setting width: 100% alone is not enough, because the height stays fixed and the aspect ratio breaks. The classic solution is a wrapper that reserves space using percentage padding, which is calculated from the element's width:

<div style="position: relative; width: 100%; padding-bottom: 56.25%; height: 0; overflow: hidden;">
  <iframe
    src="https://www.youtube.com/embed/VIDEO_ID"
    title="Product overview video"
    style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: 0;"
    loading="lazy"
    allowfullscreen
  ></iframe>
</div>

The padding percentage is the height divided by the width:

Aspect ratio Padding-bottom
16:9 (widescreen video) 56.25%
4:3 75%
1:1 (square) 100%
9:16 (vertical video) 177.78%
21:9 (cinema) 42.86%

Modern browsers also support the CSS aspect-ratio property, which does the same thing more directly:

.video-embed {
  width: 100%;
  aspect-ratio: 16 / 9;
  border: 0;
}

The padding wrapper remains useful where you cannot add a stylesheet, such as some CMS fields, because it works entirely with inline styles.

Important iframe attributes

  • title: describes the frame's content for screen reader users, who otherwise hear only "frame". Accessibility guidelines expect it on every iframe.
  • loading="lazy": delays loading until the frame is near the viewport. Embeds such as video players can be heavy, so this can noticeably speed up pages where the embed is below the fold. Do not lazy-load an embed that is the main content at the top of the page.
  • allow: a permissions policy listing features the frame may use, such as autoplay, encrypted-media, picture-in-picture, clipboard-write, fullscreen, camera or geolocation. Grant only what the embed needs.
  • allowfullscreen: lets videos and presentations go full screen.
  • referrerpolicy: controls how much of your page's URL is sent to the embedded site. strict-origin-when-cross-origin (the browser default) sends only your domain to other sites over HTTPS; no-referrer sends nothing, although some embeds need a referrer to work.
  • sandbox: applies strict restrictions to untrusted content, with exceptions you list. Useful for user-provided embeds, but it breaks many third-party widgets, so test carefully.

Generate embed code with the Iframe Generator

Our Iframe Generator builds the embed code and shows a live preview.

  1. Open the Iframe Generator.
  2. Paste a Page or video URL. YouTube watch links, youtu.be short links and Shorts links are converted to embed links automatically (a t= start time on a watch link is kept), and Vimeo links become player links. A note confirms the conversion.
  3. Fill in Title (required for accessibility) with a short description of the content.
  4. Under Size, keep Responsive (fills container width) on and choose an Aspect ratio: 16:9 (video), 4:3, 1:1 (square), 9:16 (vertical) or 21:9 (cinema). Turn it off to set a fixed Width and Height in pixels instead.
  5. Under Style, set a Border width and Corner radius if you want a frame around the embed.
  6. Under Behaviour, choose allowfullscreen, loading="lazy", a Referrer policy, and the permissions to include in the allow attribute.
  7. Check the Live preview, then copy the Embed code or download it.

Other ways to embed content

The platform's own share button

YouTube, Vimeo, Google Maps and most other services have a "Share" then "Embed" option that gives you official code. It is always a good starting point. Our generator helps when you need it responsive, lazy-loaded or with tighter permissions. For YouTube-specific options such as start time, controls and privacy-enhanced mode, the YouTube Embed Code Generator goes further.

CMS embed blocks

Most content management systems turn a pasted URL into an embed automatically. They are easy for editors, though you have less control over the attributes.

Linking instead of embedding

A thumbnail image that links to the video loads nothing from the third party until someone clicks. It is the fastest and most private option, at the cost of one extra click.

Troubleshooting

"Refused to connect" or a blank frame

The embedded site forbids framing. Many large sites do this for security. Use the site's official embed URL if it offers one (for example youtube.com/embed/… rather than youtube.com/watch?…), or link to the page instead.

Mixed content blocked

An http:// iframe on an https:// page is blocked by browsers. Use the https:// version of the URL.

Video does not autoplay

Browsers generally allow autoplay only when the video is muted, and the frame needs autoplay in its allow attribute. Check the platform's own documentation for the parameter that starts playback.

Special characters in the title break the code

Quotes or ampersands in attribute values must be escaped. The generator escapes them for you; for hand-written code, use the HTML Entity Encoder. To test the final page on several screen sizes, load it in the Responsive Design Tester.

Frequently asked questions

How do I make a YouTube embed responsive?

Wrap the iframe in a container with padding-bottom: 56.25% and position the iframe absolutely at 100% width and height, or use CSS aspect-ratio: 16 / 9. The generator produces the wrapper version for you.

Why is the title attribute important on an iframe?

Screen readers announce the title so users know what the frame contains before entering it. Without it, they hear only a generic "frame".

Should I lazy-load iframes?

Yes, for embeds below the fold, because it avoids loading heavy players until needed. Keep embeds that are visible when the page first loads eagerly loaded.

Can I embed any website in an iframe?

No. Sites can refuse to be framed using security headers, and browsers enforce that. Only pages that allow framing, or official embed URLs, will display.

What does the allow attribute do?

It lists browser features the embedded page may use, such as autoplay, fullscreen or the camera. Anything not listed is blocked, which protects your visitors.

Try it now: Iframe Generator

Loading tool…