An iframe (inline frame) shows one web page inside another. It is how sites embed YouTube videos, Google Maps, payment forms and social media posts. Because the embedded page runs separately, iframes also raise questions of size, speed and security. This lesson covers the <iframe> element, making embeds responsive, lazy loading, and the sandbox and allow attributes that keep your page safe.
In this lesson you will learn
- The
<iframe>element and its main attributes - Embedding with
srcand withsrcdoc - Responsive embeds with
aspect-ratio - Lazy loading and accessible titles
- Security with
sandbox,allowand frame-blocking headers
You should know links and images and audio and video.
A First iframe
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>iframe basics</title>
<style>
body { font-family: sans-serif; margin: 12px; font-size: 14px; }
iframe { width: 100%; height: 110px; border: 2px solid #90caf9; border-radius: 6px; }
</style>
</head>
<body>
<p>The box below is a separate page inside this page:</p>
<iframe
title="Mini notice board"
srcdoc="<body style='font-family:sans-serif;background:#fff8e1'>
<h3 style='margin:4px 0'>Notice</h3>
<p>School closes early on Thursday.</p>
</body>">
</iframe>
<p>Real embeds use <code>src</code> with a URL instead of <code>srcdoc</code>.</p>
</body>
</html>
Result
- An iframe is a rectangle that contains a complete, separate document with its own HTML, CSS and scripts. Styles from the outer page do not reach inside it.
src="https://…"loads a page from a URL. This example usessrcdocinstead, which holds the inner page’s HTML directly in the attribute; it is handy for small previews, like the live results on this site.- Every iframe needs a
titledescribing its content. Screen readers announce it, so a user knows what the frame contains before entering it. - An iframe has a default size of 300 × 150 pixels, so you nearly always set its width and height with CSS.
Responsive and Lazy Embeds
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Responsive embed</title>
<style>
body { font-family: sans-serif; margin: 12px; font-size: 14px; }
.video {
aspect-ratio: 16 / 9; /* keeps the shape at any width */
width: 100%;
max-width: 420px;
background: #263238;
border-radius: 8px;
overflow: hidden;
}
.video iframe { width: 100%; height: 100%; border: 0; }
</style>
</head>
<body>
<div class="video">
<iframe
title="Placeholder video"
loading="lazy"
sandbox=""
srcdoc="<body style='margin:0;display:grid;place-items:center;height:100vh;background:#37474f;color:#fff;font-family:sans-serif'>16 : 9 video area</body>">
</iframe>
</div>
<p>Resize the window: the box keeps its 16:9 shape.</p>
</body>
</html>
Result
- Videos and maps have a fixed shape.
aspect-ratio: 16 / 9on the wrapper keeps that shape at any width, and the iframe fills the wrapper withwidth: 100%; height: 100%. Beforeaspect-ratio, developers used a padding trick (padding-top: 56.25%) to do the same. loading="lazy"delays loading the frame until the user scrolls near it. Embeds such as YouTube players are heavy, so lazy loading can make a page noticeably faster.sandbox=""switches on every restriction: no scripts, no forms, no pop-ups, and the content is treated as coming from a unique, unknown origin.
Security: sandbox and allow
| Attribute or header | What it does |
|---|---|
sandbox | Applies all restrictions; add back only what is needed, such as sandbox="allow-scripts allow-forms" |
allow | Grants powerful features, for example allow="fullscreen; geolocation" |
referrerpolicy | Controls how much of your page’s URL is sent to the embedded site |
X-Frame-Options / frame-ancestors | Headers a site sends to stop other sites framing it, which prevents “clickjacking” |
Never give an untrusted frame both allow-scripts and allow-same-origin when it comes from your own site: together they let the framed page remove its own sandbox. Many sites, including banks, refuse to be framed at all, so an iframe of them shows an error instead of the page.
Embedding Popular Services
Most services give you ready-made embed code: YouTube under Share → Embed, Google Maps under Share → Embed a map. Paste the code, then add a meaningful title, loading="lazy", and a responsive wrapper. For a single video file you host yourself, use the <video> element instead of an iframe.
Common Mistakes
- Leaving out
title, so screen reader users hear only “frame”. - Setting a fixed pixel width, which overflows small phone screens.
- Embedding many heavy players at once without lazy loading.
- Expecting outer CSS to style the frame’s content. The inside is a separate document.
Practice Exercises
Exercise 1. Write an iframe for https://www.example.com/map that is lazy-loaded, has a title, and is 100% wide and 300px tall.
Show answer
<iframe src="https://www.example.com/map" title="School location map"
loading="lazy" style="width:100%; height:300px; border:0"></iframe>
Exercise 2. A frame shows an online quiz that needs JavaScript and must submit a form, but nothing else. Write its sandbox attribute.
Show answer
sandbox="allow-scripts allow-forms"
Frequently Asked Questions
Why does my iframe show “refused to connect”?
The site sends a header such as X-Frame-Options or a frame-ancestors rule that forbids being shown inside other pages. You cannot override this; link to the site instead.
Can JavaScript in my page read the iframe’s content?
Only if both pages come from the same origin (same protocol, domain and port). For other sites, the browser blocks access, and the two pages can talk only through postMessage.
Summary
<iframe>embeds a separate page usingsrcorsrcdoc.- Always add a
title; useaspect-ratioandloading="lazy". sandboxrestricts the frame;allowgrants specific features.- Sites can refuse to be framed to protect against clickjacking.
