An iframe (inline frame) is a window into another web page, placed inside your page. It's how sites embed YouTube videos, Google Maps, music players and live code demos. The result panels in the examples on Codevra are iframes too.
The content inside an iframe is a completely separate page with its own HTML, CSS and JavaScript. Your page decides where the window goes and how big it is, but it doesn't control what happens inside. That separation is what makes embedding safe and simple, and it's also why iframes need a little care.
Basic iframe syntax
<iframe src="https://example.com" width="600" height="400" title="Example website"></iframe>srcis the page to show inside the frame.widthandheightset the size of the window.titledescribes the embedded content for screen-reader users. Always add it.
Unlike <img>, <iframe> needs a closing tag.
How to get embed code
You rarely write iframes from scratch. Most services give you ready-made code:
- YouTube: Share → Embed → copy the
<iframe>code. - Google Maps: Share → Embed a map → copy the HTML.
- Music players, code playgrounds and design tools have similar "Embed" buttons.
Paste the code into your page, then add a meaningful title and loading="lazy". Embed codes often include a fixed width, so you may also want to make the frame responsive with CSS, for example with width: 100% and aspect-ratio: 16 / 9.
Iframe safety and performance
loading="lazy"waits to load the frame until the visitor scrolls near it. Embeds are heavy, so this can make a page much faster.sandboxrestricts what the embedded page can do. An emptysandboxblocks scripts, forms and pop-ups. You can add permissions back one by one, such assandbox="allow-scripts".allowgrants features such asfullscreenorautoplay.- Some sites, including banks and search engines, refuse to be shown inside a frame for security reasons. You'll see a blank box or an error message, and that's intentional.
When not to use an iframe
Iframes are for content that lives on another site or needs to be kept separate. They are not a good way to reuse your own header or footer across pages. Each iframe is a full page load, search engines treat its content as belonging to the other page, and the frame can't easily resize to fit its content. For shared parts of your own site, a template system or build tool is the usual answer.
HTML Iframe Syntax
Iframe syntax
<iframe src="URL" title="Description" width="560" height="315" loading="lazy"></iframe>HTML Iframe Summary
- An
<iframe>displays another web page inside yours. - Always set
src, a size and a descriptivetitle. - Copy embed code from services like YouTube and Google Maps, then add
loading="lazy". - Use
sandboxandallowto control what embedded content can do. - Only embed trusted sources, and don't use iframes to share parts of your own site.
HTML Iframe Examples
Each example runs in the page. Edit the code and press Run to see what changes.
An embedded map
This uses OpenStreetMap, which allows embedding.
<h3>Find our shop</h3>
<iframe
src="https://www.openstreetmap.org/export/embed.html?bbox=-0.1300%2C51.5050%2C-0.1200%2C51.5100&layer=mapnik"
width="400" height="250"
title="Map showing our shop near Trafalgar Square, London"
loading="lazy">
</iframe>Line-by-line explanation
- Lines 2 to 7The
srcis the embed URL given by the map site. - Line 4The size of the frame in pixels.
- Line 5A specific title tells screen-reader users what's inside the frame.
- Line 6Lazy-loading delays this heavy embed until it's needed.
Embedding your own mini page with srcdoc
srcdoc puts HTML straight into the frame instead of loading a URL. This is how the result panels on Codevra work.
<p>Here's a page inside a page:</p>
<iframe
title="Mini greeting card"
width="300" height="120"
srcdoc="<body style='font-family:sans-serif;background:#fff1c2;text-align:center'><h2>Happy birthday!</h2></body>">
</iframe>Line-by-line explanation
- Line 5
srcdocholds a complete little HTML document. Single quotes inside, double quotes outside.
Common HTML Iframe Mistakes
No title
<iframe src="https://www.youtube.com/embed/abc123"></iframe><iframe src="https://www.youtube.com/embed/abc123" title="Video: How to bake sourdough bread"></iframe>Without a title, screen readers just say "frame", leaving users guessing.
Using the normal page URL instead of the embed URL
<iframe src="https://www.youtube.com/watch?v=abc123" title="Video"></iframe><iframe src="https://www.youtube.com/embed/abc123" title="Video"></iframe>Many sites block their normal pages from being framed. Always use the special embed URL from the site's Share → Embed option.
HTML Iframe in Real Projects
Where you see iframes
YouTube players on news articles, maps on "Contact us" pages, payment forms from Stripe or PayPal (isolated in an iframe for security), social media posts and online code editors like the ones on this site. For video files you host yourself, use <video> instead.
HTML Iframe Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Complete the iframe so it's accessible and lazy-loaded.
<iframe src="https://example.com/widget" ="Weather widget" loading=""></iframe>
title describes the frame; loading="lazy" defers loading.Does <iframe> need a closing tag?
</iframe>.What does an empty sandbox attribute do?
allow-scripts.Fix this iframe: it's missing a title and has no closing tag.
<iframe srcdoc="<h1>Hello from inside!</h1>" width="300" height="100">
<p>After the frame</p>- The iframe has a title
- The paragraph is outside the iframe
<iframe> is swallowed as fallback content.HTML Iframe Quiz
3 questions to check what you learned.
What is an iframe used for?
- Embedding another web page inside yours
- Drawing frames around images
- Making forms
- Adding borders
Why should you add
titleto an iframe?- It changes the browser tab title
- It tells screen-reader users what the frame contains
- It's required for it to load
- It shows a tooltip only
Some websites appear blank when put in an iframe. Why?
- Iframes only show images
- The width is too small
- Iframes don't support HTTPS
- Those sites send security headers that forbid being framed
HTML Iframe Challenge
A tiny embedded postcard
Use srcdoc to embed a mini page:
- An iframe with
title="Postcard",width="320"andheight="160" - Its
srcdoccontains an<h2>and a<p>
- Iframe titled "Postcard"
- Size is 320×160
- srcdoc has an h2 and a p
srcdoc if you need attributes.