iframe

The iframe element loads a second web page inside a box on the current page. The page in the frame keeps its own URL, scripts, and cookies.

HTML element

Also called Inline frame

Updated

On this page

How it works

You add an <iframe> tag and give it a URL in src. The browser loads that URL as a full, separate document inside a box on your page. The page with the tag is the parent page. The page in the box is the framed page.

Each page has its own HTML, CSS, scripts, and history. CSS from the parent does not style the framed page. When the two pages have different origins, scripts on one page cannot read the other. They can only send messages with postMessage.

Attributes you use most

AttributeWhat it does
srcThe URL to load.
srcdocHTML to show in the frame, in place of a URL.
titleA short name for screen readers.
width, heightThe size in CSS pixels. The default is 300 by 150.
loadinglazy waits until the frame is near the viewport.
sandboxRemoves powers from the framed page.
allowGives the frame features such as the camera or fullscreen.

Example

HTML
<iframe
  src="https://widget.example/booking"
  title="Booking form"
  width="600"
  height="400"
></iframe>

Without width and height, the frame is 300 by 150 CSS pixels.

How to use it

Set the URL in src and a short title for screen readers. Add loading="lazy" for a frame far down the page.

<iframe
  src="https://widget.example/booking"
  title="Booking form"
  width="600"
  height="400"
  loading="lazy"
></iframe>

The framed site decides if it can load in a frame. No iframe attribute can override its frame-ancestors or X-Frame-Options header.

Learn more

Sources

Know before you embed.

Free, no sign-up, nothing stored. Paste a URL and get a verdict, a live preview, and the fix.