An inline frame, written <iframe>, embeds one web page inside another. Think of it as a window cut into your page through which a different document is shown. Videos, maps and payment widgets commonly appear this way.
Basic usage#
<iframe
src="https://example.com/"
title="Example website preview"
width="600"
height="400"></iframe>srcis the address of the page to embed.titledescribes the frame for assistive technology. Always provide one.widthandheightset the frame size in pixels (CSS can override them).
The closing tag is required. Anything between the tags is fallback content for ancient browsers, which you can safely leave empty.
Embedding your own pages#
Iframes can show pages from your own site as well, such as a printable menu:
<iframe src="print/menu-card.html" title="Printable menu card" width="100%" height="500"></iframe>Why some sites will not embed#
Many sites refuse to be shown inside someone else's frame for security reasons, for example to prevent clickjacking, where an attacker overlays invisible frames over buttons. If you see an empty frame or a refused-to-connect message, the owner has blocked embedding. You cannot fix this from your side. Look for an official embed code instead.
Security: the sandbox attribute#
An iframe loads content you do not fully control. The sandbox attribute applies strict restrictions, which you can then loosen one by one:
<iframe
src="https://example.com/widget"
title="Reviews widget"
sandbox="allow-scripts allow-same-origin"
referrerpolicy="no-referrer"
loading="lazy"></iframe>sandboxwith no value blocks scripts, forms, popups and more. Add only the permissions the embed truly needs.referrerpolicycontrols what address information is sent to the embedded site.loading="lazy"delays loading frames that are far down the page.
Accessibility and performance#
- The
titleattribute is the iframe's accessible name. "Map showing the cafe location" is far better than no title. - Poorly designed frames can trap keyboard focus, so test tabbing through them.
- Each iframe loads a whole extra document, which costs time. Use as few as you need.
- Make sure the frame has enough size, or the embedded content will show scroll bars.
Links and frames#
You can also make a link open inside a named frame using the target attribute, but this pattern is rarely needed today. Prefer simple links, or a real embed.
Common mistakes#
- Omitting
title. - Embedding content over insecure
http://inside a securehttps://page, which browsers block. - Using iframes to build your site layout. That is an outdated approach from the 1990s. Use real pages and semantic elements.
- Assuming any site can be embedded.
- Giving a frame fixed pixel sizes that overflow on phones.
Try it yourself#
Create a page called print-menu.html containing a short menu. Embed it in index.html using an iframe with a descriptive title, a width of 100 percent and a height that avoids scroll bars. Add loading="lazy". Then try embedding a large website and observe what happens, and note why.
Recap#
iframe embeds another document in your page. Always give it a descriptive title, size it sensibly, lazy-load it, and restrict it with sandbox where possible. Some sites block embedding, and iframes are not for page layout.
Check your understanding
Lesson quiz
OptionalA short quiz to check what you just learned.
- Questions
- 4
- Time limit
- 5 min
- Pass mark
- 70%
- Best score
- -