The iframe
tag is used to embed another HTML page or web resource inside the current page. Think of it as a “window” that displays content from another website or file.
✅ Basic Syntax
<iframe src="https://example.com" width="600" height="400"></iframe>
📍 Result: This will display the webpage from example.com
inside a 600×400 pixel frame.
🎯 Common Use Cases
Use Case | Example |
---|---|
Embedding videos | YouTube videos with <iframe> |
Displaying maps | Google Maps location iframe |
Integrating other websites | Showing another site or widget |
Embedding documents | PDFs or other files inside a web page |
📌 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>
🛡️ Attributes of <iframe>
Attribute | Description |
---|---|
src | URL of the content to embed |
width , height | Size of the iframe |
title | Describes the iframe for accessibility |
frameborder | Deprecated (use CSS border instead) |
allowfullscreen | Allows fullscreen mode for embedded media |
loading="lazy" | Defers loading until iframe is in view |
sandbox | Adds security restrictions to iframe |
⚠️ Security Tips
- Only embed trusted sources to prevent clickjacking and other attacks.
- Use the
sandbox
attribute for stricter control:
<iframe src="https://example.com" sandbox></iframe>
✅ Summary
Feature | <iframe> Tag |
---|---|
Purpose | Embeds external content into a page |
Common Uses | Videos, maps, other sites |
Displayed As | A resizable window on the page |
Security Note | Use sandbox and trusted URLs |