HTML Iframes

Beginner
10 min read
Updated: Nov 2026
6 examples
<iframe>

Definition and Usage

An HTML iframe (inline frame) embeds another document inside the current page. Use the <iframe> element with a src URL (or srcdoc markup) so visitors can view maps, docs, hosted players, or other pages without leaving your site.

What are iframes for in an HTML tutorial?

Show a nested page, a map widget, or a video player from another host. The frame has its own browsing context—separate scroll, history, and (often) origin from the parent page.

Where can you place iframes?

Inside the <body>, typically where you want the embedded surface to appear. Pair every iframe with a meaningful title. Prefer CSS for borders and responsive width; avoid obsolete attributes like frameborder.

When should you use them?

When a third party already hosts the experience (maps, video, forms) or you need a sandboxed preview. Prefer native <video>/<audio> for your own media files.

Always set title.

Assistive tech uses title to describe the frame. A missing or vague title makes embeds hard to navigate.

Trust the src.

Third-party pages can run scripts inside the frame. Use sandbox (and allow) when you embed untrusted or third-party content.

Default size.

Without sizing, iframes are often 300×150 CSS pixels. Set width/height or CSS so layout stays stable.

HTML Iframe Syntax

The basic form:

iframe.html
<iframe src="URL" title="Description" width="500" height="300"></iframe>
HTML
<iframe
  src="https://example.com"
  title="Example website"
  width="500"
  height="300">
</iframe>

<!-- Lazy-load below-the-fold embeds -->
<iframe
  src="https://example.com"
  title="Example website"
  width="500"
  height="300"
  loading="lazy">
</iframe>

<!-- Restrict untrusted content -->
<iframe
  src="preview.html"
  title="Sandboxed preview"
  width="400"
  height="250"
  sandbox>
</iframe>

src is the page to load. title is required for accessibility. Optional: name (link target), sandbox, loading, referrerpolicy, allow, and srcdoc for inline HTML.

Core HTML Concepts

Attributes and ideas you will use most often:

ConceptMeaning
srcURL of the document to embed
titleAccessible name announced by screen readers
width / heightFrame size in CSS pixels (or use CSS)
sandboxExtra restrictions; tokens re-enable specific features
loading="lazy"Defer fetch until near the viewport
nameTarget name for links (target="framename")
srcdocInline HTML to show instead of (or as fallback for) src
BorderStyle with CSS (border: none), not frameborder

sandbox tokens (common)

TokenAllows
(empty sandbox)Maximum restrictions
allow-scriptsRun scripts (not popups by itself)
allow-formsForm submission
allow-same-originTreat content as same-origin (use carefully with scripts)
allow-popupsOpen new windows/tabs

How to Use Iframes in HTML

Practical habits for safe, accessible embeds.

  1. Set src to the embed URL from the provider (or your own page).
  2. Add a clear title that describes the framed content.
  3. Give explicit width and height (or CSS aspect-ratio wrappers).
  4. Remove or restyle the default border with CSS.
  5. Add sandbox for untrusted content; allow only needed tokens.
  6. Use loading="lazy" for embeds that start off-screen.
GoalPattern
Basic embed<iframe src="…" title="…">
Sized framewidth="500" height="300" or CSS
No borderstyle="border:none"
Safer third partysandbox="allow-scripts allow-forms"
Performanceloading="lazy"
Link into framename on iframe + target on <a>

Live Preview

Switch modes to compare a basic embed, a sized borderless frame, and a sandboxed preview.

Uses srcdoc so the demo works without an external network page.

Rendered page

HTML Examples

Six patterns: basic src/title, size, borderless CSS, sandbox, lazy loading, and a map embed.

Example 1 — Basic iframe with src and title

HTML
<h1>Iframe demo</h1>
<iframe
  src="https://example.com"
  title="Example website">
</iframe>
Try It Yourself

How It Works

  1. src loads the nested document.
  2. title names the frame for assistive technology.
  3. Default size applies if you omit width/height.
  4. Use for simple page-in-page embeds.

Example 2 — Set Width and Height

HTML
<iframe
  src="https://example.com"
  title="Example website"
  width="500"
  height="300">
</iframe>
Try It Yourself

How It Works

  1. width / height reserve space and reduce layout shift.
  2. CSS can override attributes when you need fluid layouts.
  3. max-width: 100% keeps the frame usable on small screens.
  4. Defaults without attributes are roughly 300×150.

Example 3 — Remove the Border with CSS

HTML
<iframe
  src="https://example.com"
  title="Example website"
  width="500"
  height="300"
  style="border:none">
</iframe>
Try It Yourself

How It Works

  1. Default border appears around most iframes.
  2. border: none (or a custom border) styles the frame.
  3. Do not use deprecated frameborder.
  4. Also try border color, radius, and shadow in CSS.

Example 4 — Sandbox Restrictions

HTML
<iframe
  srcdoc="<p>Sandboxed content cannot run scripts by default.</p>"
  title="Sandboxed preview"
  width="400"
  height="160"
  sandbox>
</iframe>
Try It Yourself

How It Works

  1. sandbox with no value applies all restrictions.
  2. srcdoc embeds HTML without a separate file.
  3. Tokens re-enable features like scripts or forms.
  4. Avoid combining allow-scripts and allow-same-origin casually.

Example 5 — Lazy Loading

HTML
<p>Scroll content above the fold, then the embed:</p>
<iframe
  src="https://example.com"
  title="Example website"
  width="500"
  height="300"
  loading="lazy">
</iframe>
Try It Yourself

How It Works

  1. loading="lazy" waits until the iframe is near the viewport.
  2. eager (default) loads immediately.
  3. Helps first-load performance for below-the-fold embeds.
  4. Keep critical hero embeds eager.

Example 6 — Embed a Map

HTML
<h1>Office location</h1>
<iframe
  src="https://www.openstreetmap.org/export/embed.html?bbox=-0.004%2C51.476%2C0.000%2C51.479&layer=mapnik"
  title="Map of office area near London"
  width="600"
  height="350"
  style="border:0;max-width:100%"
  loading="lazy"
  allowfullscreen>
</iframe>
Try It Yourself

How It Works

  1. Provider embed URL goes in src.
  2. title describes the map region, not just “map”.
  3. loading="lazy" suits maps that start lower on the page.
  4. Same pattern works for docs viewers and many hosted widgets.

Edge Cases & Pitfalls

Broken embeds usually come from missing titles, oversized unrestricted frames, or X-Frame-Options / CSP blocks.

a11y

Missing or vague title

Screen reader users hear “iframe” with no context. Name the content specifically.

block

Site refuses to be framed

Many sites send X-Frame-Options or CSP frame-ancestors. You cannot force them into an iframe.

sandbox

Over-permissive sandbox

allow-scripts + allow-same-origin can let framed content remove sandboxing. Prefer the least privilege.

layout

No reserved size

Unset dimensions cause jank when the frame loads. Set width/height or a CSS aspect-ratio box.

legacy

Deprecated attributes

frameborder, scrolling, align, and margin attributes are obsolete—use CSS instead.

perf

Eager third-party weight

Each iframe can pull heavy scripts. Lazy-load off-screen embeds and avoid stacking many third-party frames above the fold.

Browser Support

The iframe element is part of HTML and works in all modern browsers. Prefer CSS over deprecated frame attributes.

✓ Baseline · Widely available

HTML iframes

Every current browser embeds nested documents with iframe. sandbox and loading="lazy" are widely supported in modern engines.

100% iframe element
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
HTML iframe Universal

Bottom line: Use iframe with src, title, and sensible sizing—add sandbox and lazy loading when the embed warrants it.

Key Takeaways

  • <iframe> embeds another document via src (or srcdoc).
  • title is required for accessibility; size the frame explicitly.
  • sandbox hardens third-party or untrusted content.
  • loading="lazy" helps performance for off-screen embeds.

One line: Embed with <iframe src="…" title="…">, size it, style the border in CSS, and tighten third-party content with sandbox and lazy loading when needed.

Frequently Asked Questions

An iframe (inline frame) embeds another HTML document inside the current page. The nested page loads in its own browsing context, controlled mainly by the src attribute.
Screen readers announce the title so users know what the frame contains before entering it. Always set a short, specific title—never leave it blank or generic like "iframe".
Without size attributes or CSS, browsers typically render an iframe about 300px wide and 150px high. Set width and height (or CSS) so layout does not jump when content loads.
sandbox applies extra restrictions: unique origin, no scripts, no forms, no top navigation, and more. Add space-separated tokens such as allow-scripts only for capabilities you intentionally need.
Use loading="lazy" for iframes below the fold so the browser can defer fetching them until they are near the viewport. Keep above-the-fold embeds eager (the default).
Yes. Many services provide an embed URL for iframe src. Always add title, size the frame, and review sandbox/allow permissions the provider documents.

Practice in the editor

Open a Try It demo and adjust src, title, size, and sandbox.

Open Try It editor →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful