Codectionary / Developer documentation / HTML

<iframe>

The <iframe> element embeds another HTML document within the current page, creating a nested browsing context. It is commonly used for embedding maps, videos from external platforms, payment widgets, or ads. Because it loads content from potentially untrusted sources, the sandbox attribute is important for restricting what an embedded page is allowed to do.

Syntax

<iframe src="https://example.com" width="600" height="400"></iframe>

Examples

Embedding a Map

A basic embedded iframe, such as a map widget.

<iframe src="https://maps.example.com/embed?q=Manchester" width="600" height="450" style="border:0;" loading="lazy"></iframe>

Sandboxed Iframe

Restricting what an embedded page is allowed to do for security.

<iframe src="https://untrusted-widget.com" sandbox="allow-scripts" width="400" height="300"></iframe>

Responsive Iframe

Making an iframe scale with its container using CSS.

<div style="position: relative; padding-bottom: 56.25%; height: 0;">
  <iframe src="https://example.com/video" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;" frameborder="0"></iframe>
</div>

Attributes

  • src (URL): The URL of the page to embed
  • sandbox (string): Restricts capabilities of the embedded content, such as scripts or form submission
  • loading (lazy | eager): Controls when the iframe content loads relative to the viewport
  • allow (string): Specifies permissions policy features the iframe can use, like camera or fullscreen

Best practices

  • Always use the sandbox attribute when embedding content from a source you do not fully control
  • Use loading="lazy" for iframes below the fold to improve initial page load performance
  • Give the iframe a descriptive title attribute for screen reader users
  • Avoid iframes for content that could just as easily be a normal part of the page — they add overhead and complicate styling

At a glance

Purpose
Structure and meaning for web content
File extension
.html · .htm
Runs in
Web browsers
Usually used with
CSS and JavaScript

Specifications & further reading

Related HTML documentation