HTML Tutorial
HTML Iframes
iframe embeds another page inside yours: maps, videos, or a live preview.
A page inside a page
An iframe (inline frame) shows another HTML document in a rectangle on your page. The inner document has its own URL, its own CSS, and its own scripts. Visitors still see your chrome around it: headings, notes, and navigation stay outside the frame.
Typical uses are a map, a video player, a payment widget, or a live HTML preview. The frame is not a screenshot. It is a real nested browsing context.
src, width, and height
src is the URL of the page to load. width and height set the size of the rectangle in pixels (or CSS later). Without a size, the frame can look like a small empty box.
Example
<h1>Embedded page</h1>
<p>The box below loads another document.</p>
<iframe
src="https://example.com"
width="400"
height="240">
</iframe>Some sites refuse to be framed. If the inner page is blank, that host may send a header that blocks embedding. Your own pages, many map and video providers, and the StudyGrid preview still work as teaching examples.
Always give a title
Screen reader users move between frames. Without a title, the frame is just “iframe” with no hint of what is inside. Write a short description of the embedded content, the same way you writealt on an image.
Example
<iframe
src="https://example.com"
width="400"
height="240"
title="Example.org home page">
</iframe>
<iframe
src="map.html"
width="400"
height="300"
title="Map of the cafe location">
</iframe>title on an iframe is for accessibility. It is not the same as the <title>inside the embedded document’s head. You still need both: one names the frame on your page, the other names the inner tab if that document is opened on its own.
sandbox is a safety idea
An iframe can load a page you do not control. That inner page might try to run scripts, submit forms, or open pop-ups. The sandbox attribute tells the browser to restrict the framed document so it cannot do those things unless you explicitly allow them.
Example
<!-- Empty sandbox: very restricted preview -->
<iframe
src="guest.html"
width="400"
height="200"
title="Guest page preview"
sandbox>
</iframe>An empty sandbox is the strict default: treat the inner page as untrusted. Tokens you add later (in other docs) can allow specific capabilities. For this chapter, remember the idea: sandbox is how you reduce risk when you embed content you did not write. Do not copy random pages into an iframe without it when the source is untrusted.
Why StudyGrid preview uses an iframe
The HTML editor at /html/try renders your example inside an iframe. That keeps your markup, CSS, and tiny scripts inside a nested page. The tutorial chrome, sidebar, and navigation stay outside.
- Your
bodystyles do not restyle the whole StudyGrid site. - A heading in the example stays in the preview pane, not in the lesson article.
- Reload and “Try it in HTML” swap only the inner document.
That is the same pattern as a map or a video embed: one rectangle, one inner URL, one job.
Maps, videos, and live previews
Hosts that allow embedding give you an iframe snippet. You paste it, set a sensible size, and keep the title. A later chapter covers YouTube in more detail. For a local preview, pointsrc at another file in your project.
| Attribute | Role |
|---|---|
src | URL of the inner page |
width / height | Size of the rectangle |
title | Accessible name of the frame |
sandbox | Restrict what the inner page may do |
Limits to keep in mind
- Not every site allows framing. A blank box often means the other host said no.
- Inner and outer pages do not freely share data. Treat them as separate documents.
- Prefer a native
videoorimgwhen you host the file yourself. - Too many iframes slow the page. One preview or one map is enough for a beginner layout.
Next: script tags add behavior inside your page. HTML remains the structure. The live preview still runs that tiny JavaScript inside its iframe at /html/try.
Worked examples
The short listings above show the tag in isolation. These pages use the same markup on documents you would actually publish: a lab report, a clinic form, a timetable, a weather card.
HTML does not calculate. It names the pieces so a browser, a screen reader, and a search engine can tell a heading from a paragraph. The numbers below are classroom values — the same Ohm, pH, and pulse figures as the C track — now sitting in real page structure.
Preview them in the HTML editor at /html/try. Change a heading or a number and watch the page, not a print log.
Engineering
A nested note with a title
iframe embeds another document. title is required in practice so assistive tech can name the frame. srcdoc is a tiny inner page for demos.
Maps and videos use iframe. sandbox can lock the inner page down. Do not embed untrusted pages without it.
Example
<iframe
title="Inner lab note"
width="280"
height="120"
srcdoc="<p>Inner page: I = 0.45 A</p>">
</iframe>Astronomy
A fact sheet inside a frame
The outer page stays yours. The inner document has its own HTML. That isolation is the point — and why a missing title is a problem.
Example
<iframe title="Mars year" width="280" height="100" srcdoc="<p>Mars year ~ 687 Earth days</p>"></iframe>