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>
<iframe ...>is the opening tag. You can put each attribute on its own line to make it easier to read. The result is the same as on one line.srcis the address of the page to show, written in full fromhttps://.widthandheightare the window's width and height in pixels. Write only the number, withoutpx. The browser still readswidth="600px"as 600, but an HTML validator (a tool that checks HTML code) marks it as wrong.titleis a short description of what is in the window. It doesn't show on screen, but screen readers (programs that read pages aloud for blind users) read it aloud. That way their users know what is in the window.</iframe>is the closing tag. You still write it, even though nothing goes between the tags.
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>
- The address uses
/embed/followed by the video's code. The example writes that code as VIDEO_ID. YouTube refuses the address you normally open to watch a video (youtube.com/watch?v=...) inside an iframe. allowfullscreenis an attribute with no value: you only write its name. If the attribute is there, it means "allowed". With it, the video's full screen button works.
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>
loading="lazy"makes the iframe load only when it is almost visible on screen. Your page opens faster.sandboxlimits what the page inside the iframe may do. Use it for content you don't trust.sandboxwith no value is the strictest mode: scripts (JavaScript code), forms and popups are all turned off.- Through the value of
sandbox, you allow them again one at a time.sandbox="allow-scripts"allows only scripts. Everything else stays off.
⚠️ Before you decide the iframe is broken
- Error 153 on a YouTube video. The cause: you opened your page straight from the file (with a double-click, so the address starts with
file://). YouTube needs to know which site shows its video, and a file opened this way does not send that information. Open the page through a local server (a small server on your own computer), such as the Live Server extension in VS Code, or try it in the exercise editor. - A missing
</iframe>. Without the closing tag, the browser can treat the rest of the page as part of the iframe, so that part of the page doesn't show. - A missing
title. A screen reader only says "frame", and its user can't tell what is inside. - An empty box that looks like broken code. The site may refuse through an
X-Frame-Optionsheader or a CSPframe-ancestorsrule (CSP is covered in the Content Security Policy lesson). Both are messages from the server (the site's computer) that forbid showing its page in an iframe. Try another address, such ashttps://example.com.
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.