TExcelTAD
Home › HTML › HTML Iframes

HTML Iframes

An iframe (inline frame) is a window inside your page that shows another HTML document. Video players, maps, payment widgets and code playgrounds — including the ones on this site — are very often iframes.

html
<iframe src="page.html" title="Description of the content"></iframe>

Basic syntax

Attribute Purpose
src URL of the page to load inside the frame
srcdoc HTML written directly in the attribute (overrides src)
title Describes the frame for screen readers — always include it
width, height Size in pixels (or use CSS)
name Lets links target the frame
loading="lazy" Delays loading until the frame is near the viewport
sandbox Restricts what the embedded page may do
allow Grants features such as fullscreen or camera

In these examples we use srcdoc, which lets us put the inner page's HTML right inside the attribute, so everything works offline. With a real site you would usually use src="https://…".

html · try it yourself
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
  <p>This is the outer page.</p>
  <iframe
    title="Greeting card"
    width="300" height="120"
    srcdoc="<body style='background:#fff3e0;font-family:sans-serif'><h3>Hello from inside!</h3><p>I am a separate document.</p></body>">
  </iframe>
  <p>The outer page continues below the frame.</p>
</body>
</html>

Note: Inside srcdoc, the HTML lives in an attribute value. Use single quotes inside it (or &quot;) so the outer double quotes are not closed early.

Sizing and borders

Browsers draw a thin inset border around iframes by default. You can change size and border with CSS, just like any other element.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  iframe {
    width: 100%;
    height: 140px;
    border: 3px solid #e34c26;
    border-radius: 10px;
  }
  .plain { border: none; background: #eef; }
</style>
</head>
<body style="font-family: sans-serif">
  <iframe title="Styled frame" srcdoc="<p style='font-family:sans-serif'>A frame with a custom orange border.</p>"></iframe>
  <iframe class="plain" title="Borderless frame" srcdoc="<p style='font-family:sans-serif'>A frame with no border at all.</p>"></iframe>
</body>
</html>

Opening links inside a frame

Give the iframe a name, then use that name as the target of a link. Clicking the link loads the new page inside the frame instead of replacing the whole window.

html · try it yourself
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
  <iframe name="viewer" title="Viewer" width="320" height="100"
    srcdoc="<p style='font-family:sans-serif'>Click a link above to load a page here.</p>"></iframe>
  <p>
    <a target="viewer" href="data:text/html,<h2 style='color:green'>Page A</h2>">Load page A</a> |
    <a target="viewer" href="data:text/html,<h2 style='color:purple'>Page B</h2>">Load page B</a>
  </p>
</body>
</html>

The links above use data: URLs (a tiny document encoded in the URL itself) so they work offline. Some browsers block navigating to data: URLs; if nothing happens, the idea is the same with ordinary links like href="about.html".

Security: the sandbox attribute

An iframe can show content you do not control. The sandbox attribute switches off risky features; you then switch back on only what you need.

Value Re-enables
(empty) sandbox Nothing: no scripts, no forms, no popups
allow-scripts JavaScript
allow-forms Submitting forms
allow-popups window.open and target="_blank"
allow-same-origin Treating the content as its own origin (cookies, storage)
allow-modals alert(), confirm(), prompt()

ExcelTAD runs your examples in sandbox="allow-scripts allow-modals allow-forms". That is why storage and some APIs may be blocked in later lessons.

html · try it yourself
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
  <p>Scripts allowed:</p>
  <iframe sandbox="allow-scripts" title="Scripts on" height="60"
    srcdoc="<p id='m'>waiting</p><script>document.getElementById('m').textContent='Script ran!'</script>"></iframe>
  <p>Scripts blocked (empty sandbox):</p>
  <iframe sandbox title="Scripts off" height="60"
    srcdoc="<p id='m'>waiting</p><script>document.getElementById('m').textContent='Script ran!'</script>"></iframe>
</body>
</html>

The second frame stays on "waiting" because its script was never allowed to run.

Tip: Many sites refuse to be framed (they send an X-Frame-Options or Content-Security-Policy header). If an embedded site shows a blank box or an error, it is usually the site saying "no", not a bug in your HTML.

When to use iframes

  • Good uses: embedding videos and maps from their providers, isolating third-party widgets, previews of user-written HTML.
  • Avoid: building your own site's layout out of frames. Use normal elements and CSS instead; frames hurt accessibility, bookmarking and search engines.

Test yourself

Which attribute lets you write the frame's HTML directly instead of loading a URL?

  1. src
  2. srcdoc
  3. html
  4. content

What does an empty sandbox attribute do?

  1. Allows everything
  2. Applies all restrictions, including blocking scripts
  3. Only removes the border
  4. Makes the frame invisible

How do you make a link open its page inside <iframe name="view">?

  1. <a href="x.html" frame="view">
  2. <a href="x.html" target="view">
  3. <a href="view#x.html">
  4. <a src="x.html" name="view">