Iframes and Embedded Content

Intermediate · 8 min

Embed another page with iframe: title, loading, allow and sandbox, what a sandbox really blocks, and what happens when the other site forbids framing.

Before thisCaptions, Subtitles and Text Tracks

Miljan Peric · 8 min

Iframes and Embedded Content

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Miljan PericReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `<iframe src="..." title="...">` embeds another page inside yours. Always give it a `title`, which labels the frame for people using assistive technology.
  • `sandbox` with no value blocks scripts, forms, popups and more. In a test a sandboxed frame loaded its HTML but its script did not run.
  • `sandbox="allow-scripts"` alone gives the frame an opaque origin. In a test its `localStorage` threw `SecurityError` and the parent could not read its document. Adding `allow-same-origin` as well removes that protection.
  • A site can refuse to be framed. In a test `X-Frame-Options: DENY` and `Content-Security-Policy: frame-ancestors 'none'` each left the frame on a browser error page.
  • `srcdoc` embeds HTML inline, with the frame's URL `about:srcdoc`. `loading="lazy"` defers a frame far down the page, and in a test a frame 5000px down was not requested.

A window onto someone else’s page

Sometimes the thing you want on your page already exists as a page somewhere else: a map, a video player, a payment form, a slide deck. Copying it isn’t possible, and you don’t want to. The <iframe> element embeds one document inside another. It’s the browser’s picture-in-picture for web pages, a rectangle of your page that shows a separate page with its own scripts, styles and history.

<iframe
  src="https://example.com/map"
  title="Route map to the venue"
  width="600" height="400"
  loading="lazy"
  allow="fullscreen"></iframe>

title names the frame for assistive technology. width and height reserve the space, as with images. loading="lazy" defers the load, which I tested: a lazy frame placed 5000px down a page wasn’t requested at load in headless Chromium 159. allow grants specific browser features, and allowfullscreen or allow="fullscreen" lets a player go fullscreen. The lazy loading lesson covers the attribute, and MDN’s data lists iframe loading in Chrome 77, Firefox 121 and Safari 16.4.

Why embedding is a risk

An embedded page runs code on a page you do not control. By default, a frame from another origin can’t read your page and you can’t read it, thanks to the same-origin policy. But the frame can still run scripts, show popups and redirect itself. If you embed untrusted content, or if you’re the one who gets embedded, you need controls.

sandbox: remove powers, then add back

The sandbox attribute starts from zero. With no value it blocks scripts, forms, popups, and treats the frame as a unique origin. You then add back what you need with tokens. I tested a frame whose page ran a script that set its own title:

Markup Result
plain <iframe>, same origin script ran, parent could read the frame’s document
sandbox the page loaded but its script did not run
sandbox="allow-scripts" script ran, origin was null, localStorage threw SecurityError, parent could not read the document
sandbox="allow-scripts allow-same-origin" real origin, storage worked, parent could read the document

The last row is the trap. Scripts plus the same origin let the framed page behave like part of your site, and the HTML spec warns that a same-origin framed page with both tokens can remove its own sandbox attribute (I did not test that). Never give both to content you don’t fully trust. The HTML security lesson works through this and the frontend security lesson covers the general threat model.

When a site says no

Embedding works only if the other site allows it. Servers can send X-Frame-Options: DENY (or SAMEORIGIN), or a Content-Security-Policy header with frame-ancestors. I served a page with each and framed it: in both cases the frame’s URL became chrome-error://chromewebdata/, a browser error page. Your page can’t tell the frame to show anything else. If an embed turns grey, look at the response headers of the framed URL first. Many big sites forbid framing, which is why “embed a website in an iframe” tutorials fail on them.

Two notes on the other direction. If you want to prevent your own pages from being framed (clickjacking), send the same headers. And srcdoc fills a frame with inline HTML instead of a URL: in a test the frame’s URL was about:srcdoc and its body held inline. It’s the usual partner of sandbox for previewing markup safely.

Iframes versus the alternatives

For video, the provider’s embed code is an <iframe>, and the lesson on audio and video elements covers hosting a file yourself. For your own components, an iframe is a heavy tool: each frame is a full document with its own load cost and no shared CSS. Web components and templates are lighter, and the web components lesson in the frontend roadmap covers them. Responsive sizing of embeds is a layout concern, discussed in aspect ratio and responsive media.

What the video covers

The embed is Miljan Peric’s “HTML5 Tutorial #27: HTML Iframe Explained” (November 2026, about 8 minutes). The description lists the src, width and height, title, loading, allowfullscreen, allow, name and sandbox attributes, embedding YouTube and Google Maps, and making iframes responsive, with a note that not every site permits embedding. That’s a good match for the lesson’s scope. It’s a brand-new video from a small channel, I could not confirm its audio language, and I could not read a transcript, so I describe only the listed topics. How sandbox tokens combine, the header tests and the srcdoc result are this lesson’s own, from one Chromium build. The deeper link is a four-minute iframe basics video from Bro Code (2021).

Where this fits

This follows captions and text tracks. Next, inline SVG and canvas closes the group with graphics drawn in the page instead of embedded from elsewhere.

Check yourself

  1. 01Why does an iframe need a `title` attribute?

    A frame is a box with another document inside, and without a title assistive technology can only announce 'frame'. A title such as 'Route map' tells people what it is before they enter. WCAG expects frames to have an accessible name.

  2. 02You embed a widget with `sandbox="allow-scripts allow-same-origin"` and the widget is served from your own domain. What did you give up?

    The sandbox's main protection. With both tokens the frame keeps your origin and can run scripts, so it can reach your storage, and the spec warns it can even remove its own `sandbox`. In a test the origin was the real one and `localStorage` worked. Do not combine them for content you do not trust.

  3. 03An embed shows a grey browser error page. What are two likely causes on the other site's side?

    It sends `X-Frame-Options: DENY` or `SAMEORIGIN`, or a `Content-Security-Policy` with `frame-ancestors` that excludes your site. In a test each header left the frame on an error page, with no way to read the content. Check the response headers in DevTools.

  4. 04When do you want `srcdoc`?

    For a small piece of generated HTML shown in isolation, such as a preview of user-written markup, usually combined with `sandbox`. The frame's URL is `about:srcdoc`, and in a test its body held exactly the inline HTML.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark