The <iframe> (inline frame) tag allows you to embed another HTML page within the current page. It’s commonly used to display external content such as videos, maps, ads, or other web pages without leaving the parent site.
How does the <iframe> tag work and what is it used for?
Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.
Overview
The iframe tag creates an inline frame that embeds another webpage inside the current document. It acts like a small browser window within your page, allowing you to load external or internal HTML content.
Attribute | Purpose | Example Value |
|---|---|---|
| Specifies the URL of the page to display |
|
| Define frame dimensions |
|
| Provides an accessible name for screen readers |
|
| Lets embedded videos enter fullscreen mode |
|
| Controls loading behavior for performance |
|
Basic Example
<iframe src="https://www.example.com" width="600" height="400" title="Example Website"></iframe>
This code embeds the page at https://www.example.com directly inside your webpage.
Example: Embedding a YouTube Video
<iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" title="YouTube video player" frameborder="0" allowfullscreen></iframe>
The YouTube video is displayed inside the frame, allowing playback without leaving your site.
Security and Best Practices
- Always specify the
titleattribute for accessibility. - Use the
sandboxattribute to restrict what embedded content can do (e.g., prevent scripts or forms). - Only embed trusted sources — malicious sites could attempt cross-site scripting (XSS) attacks.
- Use
loading="lazy"to improve performance by deferring off-screen iframes.
Sandbox Example
You can limit iframe permissions using the sandbox attribute:
<iframe src="/docs/guide.html" sandbox></iframe>
<!-- Disables scripts, forms, and same-origin access -->
Think of an iframe as a window inside your page — it shows another page, but you can decide how much control that page has through attributes like sandbox or allowfullscreen.
iframeembeds one webpage into another.- Commonly used for videos, maps, or third-party content.
- Supports attributes like
src,title,sandbox, andallowfullscreen. - Use it responsibly to avoid performance or security issues.