Iframes & Embeds
An <iframe> (inline frame) puts a whole web page inside your page, like a window onto another site. It's how people embed YouTube videos, Google or OpenStreetMap maps, payment widgets and social media posts. Because the embedded page comes from someone else, you also need to think about privacy and security.
The iframe element
<iframe src="https://www.example.com/widget.html"
title="Weather forecast for Kisumu"
width="400" height="300" loading="lazy"></iframe>
srcis the URL of the page to show.titledescribes the frame's content. Screen readers announce it when users reach the frame, so every iframe needs a meaningful title.widthandheightset the frame's size in pixels; the default is 300 × 150. CSS can make it responsive.loading="lazy"waits until the user scrolls near the frame before loading it. Embeds are often heavy, so this saves data and speeds up your page.
Browsers draw a thin border around iframes by default. Remove it with CSS (border: none), not with the obsolete frameborder attribute.
Embedding a YouTube video
On YouTube, press Share → Embed and copy the code. It looks similar to this (the video ID is a placeholder):
<iframe width="560" height="315"
src="https://www.youtube-nocookie.com/embed/VIDEO_ID"
title="How to build your first website"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
referrerpolicy="strict-origin-when-cross-origin"
allowfullscreen></iframe>
- The URL uses
/embed/, not the normalwatch?v=page address. Using a normal watch address won't work, because YouTube blocks its main pages from being framed. - The
youtube-nocookie.comdomain is YouTube's privacy-enhanced mode, which avoids storing some tracking cookies until the visitor plays the video. allowlists browser features the frame may use, andallowfullscreenlets the video go full screen.- Change the generic title YouTube gives you into a description of the actual video.
Embedding a map
Google Maps (Share → Embed a map) and OpenStreetMap (Share → HTML) both give you iframe code. An OpenStreetMap embed of central Nairobi looks like this:
<iframe src="https://www.openstreetmap.org/export/embed.html?bbox=36.80,-1.30,36.84,-1.27&layer=mapnik"
title="Map of central Nairobi"
width="600" height="400" loading="lazy"></iframe>
The bbox values are the map's west, south, east and north edges as longitude and latitude. Notice the &: an ampersand in an attribute value should be written as an entity, just like in text.
Security: who can frame whom
Some sites refuse to be framed
If you try to frame many big sites (banks, Google Search, most social networks), you'll see an empty box or an error. Those sites send an HTTP header, X-Frame-Options or the frame-ancestors rule of a Content Security Policy, telling browsers not to display them inside other sites' frames. This protects users from clickjacking, a trick where an attacker hides a real site inside an invisible frame and fools people into clicking its buttons. You can't override this, and you shouldn't try.
Limiting what an embed can do: sandbox
The sandbox attribute puts the framed page in a locked-down mode. With an empty sandbox, the frame can't run scripts, submit forms, open pop-ups or navigate your page. You then switch features back on one at a time:
<iframe src="https://widgets.example.com/poll.html" title="Class poll"
sandbox="allow-scripts allow-forms"></iframe>
Only allow what the embed really needs. In particular, combining allow-scripts and allow-same-origin for a page from your own site lets it remove its own sandbox, which defeats the purpose.
The "Try it yourself" editor on this site uses exactly this technique: your code runs in a sandboxed iframe so it can't affect the rest of the page.
Privacy
When an embed loads, the visitor's browser connects to the other company's servers, which can see their IP address and may set cookies. Embed only from sources you trust, use privacy-friendly options where they exist, and mention embedded services in your site's privacy notice.
embed and object
HTML also has <embed> and <object>, originally for browser plug-ins such as Flash, which no longer exist. Today you'll mostly see them used to show PDFs. For video, audio and images, use <video>, <audio> and <img>; for other web pages, use <iframe>. Often the simplest and most mobile-friendly option for a PDF is a plain link to download it.
Try it
The srcdoc attribute lets you write the frame's HTML directly instead of pointing at a URL, which is handy for experiments. The first frame is sandboxed with no permissions, so its script is blocked; the second allows scripts.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Iframes</title>
<style>
iframe { border: 2px solid #1f3a5f; border-radius: 6px; }
</style>
</head>
<body>
<h1>Two frames</h1>
<iframe title="Locked frame" width="280" height="120" sandbox
srcdoc="<h2>Locked</h2><p id='msg'>Scripts cannot change me.</p><script>document.getElementById('msg').textContent = 'A script ran!';</script>">
</iframe>
<iframe title="Frame with scripts allowed" width="280" height="120" sandbox="allow-scripts"
srcdoc="<h2>Scripts allowed</h2><p id='msg'>Waiting...</p><script>document.getElementById('msg').textContent = 'A script ran!';</script>">
</iframe>
</body>
</html>
Two bordered boxes appear side by side (or stacked on a narrow screen). The first still says "Scripts cannot change me." because the empty sandbox blocks its script. The second says "A script ran!" because allow-scripts lets its script run.
Key points
<iframe>displays another page inside yours; always give it a descriptivetitle, and considerloading="lazy".- Use the embed code from YouTube or map services; YouTube embeds use
/embed/URLs. - Sites can forbid framing with
X-Frame-Optionsor CSPframe-ancestorsto prevent clickjacking. sandboxrestricts what an embedded page can do; enable only the permissions it needs.
Tip: use the ← and → keys to move between pages.
