Learn Iframes in HTML

<iframe src="..." width="..." height="..." title="..."></iframe> shows another page inside yours. Width and height are numbers without px, and title describes…

A shop's contact page often has a small Google Maps map that you can drag. A recipe blog often puts a YouTube video in the middle of an article. Visitors can see the map or watch the video without leaving the page.

Both use an <iframe>. This tag shows another web page inside your page, like a small window that looks out at another site.

Show another page with <iframe>

<iframe
  src="https://example.com"
  width="300"
  height="200"
  title="Example page"
></iframe>

The result is a 300 by 200 pixel box showing the example.com page, with the browser's default border around it.

Not every site allows its pages to be shown in an iframe. Google, banks and login pages usually refuse, so the box stays empty or shows an error. YouTube videos and Google Maps are made to be shown in iframes.

YouTube videos and full screen

<iframe
  src="https://www.youtube.com/embed/VIDEO_ID"
  width="560"
  height="315"
  title="HTML tutorial video"
  allowfullscreen
></iframe>

A width of 560 pixels is more than a phone screen, which is about 360 pixels wide, so the page becomes wider than the screen and must be scrolled sideways. In the CSS lessons, the rule iframe { width: 100%; height: auto; aspect-ratio: 16 / 9; border: 0; } makes the video as wide as the screen, in a 16:9 shape (16 wide for every 9 high). border: 0 removes the default border, so the iframe is not wider than the screen.

Faster and safer: loading and sandbox

This iframe loads a weather widget page, with two extra attributes:

<iframe src="widget.html" title="Weather widget" loading="lazy" sandbox="allow-scripts"></iframe>

⚠️ Before you decide the iframe is broken

TL;DR: <iframe src="..." width="..." height="..." title="..."></iframe> shows another page inside yours. Width and height are numbers without px, and title describes the content for screen readers. A YouTube video uses the /embed/ address, plus allowfullscreen (an attribute with no value) to allow full screen. loading="lazy" makes the iframe load only when it is almost visible. sandbox limits what the page inside may do: with no value, it is the strictest mode.

⚡ A window onto another page

Write:

<iframe
  src="https://example.com"
  width="300"
  height="200"
  title="Example page"
></iframe>

Meaning: <iframe> shows another page inside yours. src is that page's address, in full from https://. width and height are numbers without px. title doesn't show, but screen readers read it aloud. You still write </iframe>, even when the iframe is empty.

⚡ A YouTube video

Write:

<iframe
  src="https://www.youtube.com/embed/VIDEO_ID"
  width="560"
  height="315"
  title="HTML tutorial video"
  allowfullscreen
></iframe>

Meaning: Use the /embed/ address, because YouTube refuses the normal watch address (watch?v=...). For allowfullscreen you only write its name, and the full screen button works.

⚡ Limit the content with sandbox

Write:

<iframe src="widget.html" title="Weather widget" loading="lazy" sandbox="allow-scripts"></iframe>
Guess first: what does sandbox with no value mean?

The strictest mode. sandbox limits the page inside the iframe. With no value, scripts (JavaScript code), forms and popups are all turned off.

Meaning: Through the value of sandbox, you allow them again one at a time: sandbox="allow-scripts" allows only scripts. loading="lazy" makes the iframe load only when it is almost visible.

What you will learn