HTML '<iframe>' Element
π What is an <iframe>?
The <iframe> (short for inline frame) is an HTML element that allows you to embed another HTML page or external content (like videos, maps, or websites) inside the current page. Think of it as a "window" to another webpage π.
>>βAn <iframe> lets you nest the world inside your webpage.β π
π Basic Syntax
Basic iframe usage
<iframe src="https://example.com" width="600" height="400"></iframe>This will embed https://example.com in a 600x400 box within your page.
π Key Attributes
| Attribute | Description |
|---|---|
| src | The URL of the page to embed |
| width | Specifies the width of the iframe |
| height | Specifies the height of the iframe |
| title | Describes the iframe content for accessibility |
| loading | Can be set to "lazy" to delay loading |
| allowfullscreen | Allows the iframe content to be shown in full-screen mode |
π Example: Embedding Google Maps
Google Maps iframe
<iframe
src="https://www.google.com/maps/embed?pb=!1m18..."
width="600"
height="450"
style="border:0;"
allowfullscreen=""
loading="lazy"
title="My Google Map"
></iframe>
Note
π‘ Use the title attribute to improve accessibility, especially for screen readers.
π Security & Restrictions
- Some sites block embedding with X-Frame-Options headers (e.g., Facebook, Twitter).
- Be cautious of iframe injection attacks; never embed user-controlled URLs directly.
- You can use sandbox attribute to limit iframe capabilities.
Secure iframe with sandbox
<iframe src="..." sandbox></iframe>π Use Cases
- π½οΈ Embedding YouTube videos
- πΊοΈ Displaying interactive maps
- π Embedding PDFs or other documents
- π» Showing content from another site inside your page
π Learn More
>>βWith <iframe>, your page becomes a window to the web.β πͺ