Skip to content
Tutorials Center
My learning

Topics

Lesson 41 of 122 ยท Images and Media

Iframes

Embed one web page inside another with the HTML iframe element. Learn src, srcdoc, title, sandbox, allow, loading="lazy", referrerpolicy and framing rules.

An iframe (inline frame) is a window inside your page that shows another, completely separate web page. Video players from hosting platforms, maps, payment forms, code playgrounds, ads and chat widgets all reach your page this way. In fact, every live preview in this course is an iframe.

Because an iframe pulls in a whole page โ€” possibly from someone else's site โ€” it raises questions that images and videos do not: what may that page do, what does it learn about your visitors, and what if it refuses to be embedded? This lesson covers the element and the attributes that answer those questions.

The basics: src, title and size

HTML
<iframe src="https://example.com/widget" title="Weather forecast for Lisbon"
        width="400" height="300"></iframe>
  • src is the URL of the page to show.
  • title describes the frame's content. Screen readers announce it when a user reaches the iframe, so it is how people decide whether to enter the frame. Every iframe should have one.
  • width and height set the size in CSS pixels. Without them, an iframe is 300 by 150 pixels โ€” rarely what you want. You can also size it with CSS.

The embedded page is a separate document. It has its own HTML, CSS and JavaScript, and your page's styles do not reach into it.

An embedded page

Browsers draw a thin inset border around iframes by default. Old tutorials remove it with frameborder="0", an obsolete attribute; use CSS (border: none or your own border) instead.

Inline content with srcdoc

Instead of a URL, srcdoc lets you write the framed page's HTML directly in an attribute. If an iframe has both, srcdoc wins.

srcdoc

Because the HTML sits inside a double-quoted attribute, any " inside it must be written as &quot; (and & as &amp;). That gets awkward quickly, so srcdoc is best for small snippets, or for content your JavaScript generates. It is handy for previews: this course's TryIt editor works exactly that way.

Give an iframe a name, and links on your page can open inside it by using that name as their target. The Link Targets and Page Jumps lesson covers target in general.

Links that load into a frame

Do not build a site out of frames. In the 1990s, whole sites were split into frames with the now-obsolete <frameset> element. That broke bookmarks, the Back button, search engines and accessibility. Use iframes to embed separate content, not to lay out your own pages.

Sandboxing

An embedded page can run scripts, submit forms, open pop-ups and even try to navigate your whole page away. The sandbox attribute takes those powers away. With an empty sandbox, the framed page:

  • cannot run scripts,
  • cannot submit forms,
  • cannot open pop-ups or new windows,
  • cannot navigate the top-level page,
  • is treated as coming from a unique, opaque origin, so it cannot read cookies or storage of its real origin,
  • cannot autoplay media, download files or show alert() dialogs.

You then give back only what the content needs, by listing tokens separated by spaces:

Token Allows
allow-scripts Running JavaScript
allow-forms Submitting forms
allow-popups Opening new windows and tabs, for example with target="_blank"
allow-modals alert(), confirm(), prompt() and printing
allow-same-origin Keeping its real origin (cookies, storage, same-origin access)
allow-top-navigation-by-user-activation Navigating the top page, but only after a user click
allow-downloads Downloading files
The sandbox in action

Never combine allow-scripts and allow-same-origin for content from your own site. Together they let the framed page reach up into its parent, find its own <iframe> element and simply remove the sandbox attribute. That pair is only safe for content from a different origin, where same-origin access does not reach your page anyway.

Sandboxing is ideal for showing untrusted HTML, such as user-written code or third-party ads. Keep the token list as short as possible.

Granting features with allow

Some browser features are powerful or private: the camera, the microphone, location, fullscreen, autoplay with sound. Iframes from another origin do not get them unless your page delegates them with the allow attribute. It contains a permissions policy list, separated by semicolons:

HTML
<iframe src="https://video.example/embed/VIDEO_ID"
        title="Product tour video"
        allow="fullscreen; autoplay; picture-in-picture; encrypted-media"></iframe>

Each feature listed without an origin applies to the frame's src origin. You can also be explicit, for example allow="geolocation 'none'" to be sure the frame never asks for the visitor's location. Leaving a feature out keeps it off for cross-origin frames. The older allowfullscreen boolean attribute still works and means the same as allow="fullscreen".

sandbox and allow complement each other: sandbox turns off general capabilities, while allow turns on specific powerful features.

Performance and privacy

loading="lazy"

Every iframe loads a whole page, often with its own scripts, fonts and images. For iframes further down the page, loading="lazy" waits until the visitor scrolls near them โ€” exactly as it does for images:

HTML
<iframe src="/map.html" title="Map of our location" width="600" height="400" loading="lazy"></iframe>

referrerpolicy

When the framed page loads, the browser normally tells it which page the request came from, through the Referer header. By default, browsers send only your origin (such as https://example.com) to other sites, never the full path. The referrerpolicy attribute changes that; no-referrer sends nothing at all:

HTML
<iframe src="https://widgets.example/clock" title="World clock" referrerpolicy="no-referrer"></iframe>

Other values include origin, same-origin and strict-origin-when-cross-origin (the default). Some embed providers need the referrer to verify that your site is allowed to use them, so do not remove it blindly from embeds that ask for it.

Sites that refuse to be framed

Try putting a bank's login page or a major search engine in an iframe, and you will see an empty frame or an error message instead. Many sites forbid being framed, to protect their users from clickjacking โ€” an attack in which a site is shown invisibly under a fake button so that a click lands on the hidden page.

Sites refuse with an HTTP response header, which you cannot override from your HTML:

  • Content-Security-Policy: frame-ancestors 'none' (or 'self' to allow only their own pages) is the modern way.
  • X-Frame-Options: DENY or SAMEORIGIN is the older header with the same purpose.

If you want to embed a service, use the embed URL it provides, which is designed to be framed. The next lesson, Embedding Videos, Maps and Other Content, shows typical examples. And it works both ways: you should send one of these headers for your own sensitive pages.

Talking across frames. A page and an iframe from different origins cannot touch each other's DOM, but they can exchange messages safely with window.postMessage(), checking event.origin on the receiving side. The TryIt editor uses this to show console output from the preview.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should every iframe have a title attribute?

    The title gives the iframe an accessible name. It does not change the framed document or appear visually.
  2. 2. What does <iframe sandbox src="..."> (with no tokens) do?

    An empty sandbox applies all restrictions. Tokens such as allow-scripts give specific capabilities back.
  3. 3. Which sandbox combination is dangerous for a page from your own origin?

    With both, a same-origin framed page can script its parent and remove its own sandbox attribute.
  4. 4. You put a popular website in an iframe and the frame stays blank. What is the most likely reason?

    Many sites refuse to be framed to prevent clickjacking. Only the site's own headers can change that.

Summary

  • <iframe> shows a separate web page inside yours; give it src (or srcdoc), a title and a size.
  • srcdoc holds inline HTML and takes priority over src.
  • name plus a link's target loads pages into a frame.
  • sandbox removes capabilities; add back only the tokens you need, and never pair allow-scripts with allow-same-origin for same-origin content.
  • allow delegates powerful features such as fullscreen and autoplay to cross-origin frames.
  • loading="lazy" defers off-screen frames; referrerpolicy controls what the frame learns about your page.
  • Sites can refuse framing with frame-ancestors or X-Frame-Options; use their official embed URLs instead.

Next up: Embedding Videos, Maps and Other Content.