Iframe Examples
Complete collection of iframe examples with code samples, syntax, attributes, and practical usage scenarios.
Basic Iframe Examples: Iframe Tag & Syntax
Simple Iframe Example
This is the most basic iframe example showing the essential iframe tag structure:
<iframe src="https://www.example.com" width="600" height="400"></iframe>
Result: Creates a 600x400 pixel iframe displaying example.com
Iframe with Title Attribute
Adding a title attribute improves accessibility for screen readers:
<iframe
src="https://www.youtube.com/embed/VIDEO_ID"
width="560"
height="315"
title="YouTube video player">
</iframe>
Result: YouTube video iframe with proper accessibility
Responsive Iframe Example
Using percentage-based dimensions for responsive design:
<iframe
src="https://maps.google.com/maps?q=New+York"
width="100%"
height="400"
style="border: none;"
title="Google Maps - New York">
</iframe>
Result: Full-width responsive map iframe
Iframe Attributes Examples: Complete Property Reference
Iframe Src Examples
<!-- External website -->
<iframe src="https://www.wikipedia.org"></iframe>
<!-- Local HTML file -->
<iframe src="./local-page.html"></iframe>
<!-- PDF document -->
<iframe src="document.pdf"></iframe>
Iframe Allow Examples
<!-- Allow fullscreen and camera access -->
<iframe
src="https://example.com/camera-app"
allow="fullscreen; camera; microphone">
</iframe>
<!-- Allow specific permissions -->
<iframe
src="https://example.com/payment"
allow="payment; fullscreen">
</iframe>
Iframe Properties Examples
<!-- With scrolling control -->
<iframe
src="https://example.com"
scrolling="auto"
frameborder="0"
marginwidth="10"
marginheight="10">
</iframe>
<!-- With sandbox restrictions -->
<iframe
src="https://example.com"
sandbox="allow-scripts allow-same-origin">
</iframe>
Advanced Iframe Attributes
<!-- With loading and referrer policy -->
<iframe
src="https://example.com"
loading="lazy"
referrerpolicy="no-referrer">
</iframe>
<!-- With name for JavaScript targeting -->
<iframe
src="https://example.com"
name="myIframe"
id="iframe1">
</iframe>
Iframe CSS Examples: Styling & Layout
Basic Iframe CSS Styling
Apply CSS styles to customize iframe appearance:
<iframe
src="https://example.com"
style="
border: 2px solid #3498db;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
width: 100%;
height: 400px;
">
</iframe>
Iframe Stylesheet Integration
Using external CSS classes for iframe styling:
<!-- HTML -->
<iframe
src="https://example.com"
class="styled-iframe responsive-iframe"
title="Styled iframe">
</iframe>
<!-- CSS -->
<style>
.styled-iframe {
border: 3px solid #e74c3c;
border-radius: 15px;
box-shadow: 0 6px 12px rgba(0,0,0,0.15);
transition: all 0.3s ease;
}
.styled-iframe:hover {
transform: scale(1.02);
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
}
.responsive-iframe {
width: 100%;
max-width: 800px;
height: 500px;
}
</style>
Responsive Iframe CSS
CSS techniques for responsive iframe design:
<!-- HTML -->
<div class="iframe-container">
<iframe
src="https://example.com"
title="Responsive iframe">
</iframe>
</div>
<!-- CSS -->
<style>
.iframe-container {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
overflow: hidden;
}
.iframe-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: none;
}
</style>
Iframe and JavaScript Examples: Dynamic Integration
Iframe in JavaScript: Dynamic Creation
Create iframes dynamically using JavaScript:
// Create iframe element
function createIframe(src, width, height) {
const iframe = document.createElement('iframe');
iframe.src = src;
iframe.width = width;
iframe.height = height;
iframe.title = 'Dynamic iframe';
iframe.style.border = 'none';
return iframe;
}
// Usage example
const newIframe = createIframe('https://example.com', '600', '400');
document.getElementById('container').appendChild(newIframe);
// Alternative method
const iframeHTML = `<iframe src="${src}" width="${width}" height="${height}"></iframe>`;
document.getElementById('container').innerHTML = iframeHTML;
Iframe Events and Communication
Handle iframe events and communicate between parent and iframe:
// Parent page JavaScript
const iframe = document.getElementById('myIframe');
// Listen for iframe load event
iframe.addEventListener('load', function() {
console.log('Iframe loaded successfully');
});
// Listen for iframe messages
window.addEventListener('message', function(event) {
if (event.origin !== 'https://trusted-site.com') return;
console.log('Message from iframe:', event.data);
});
// Send message to iframe
iframe.contentWindow.postMessage('Hello from parent!', 'https://trusted-site.com');
// Iframe page JavaScript (inside iframe)
window.addEventListener('message', function(event) {
if (event.origin !== 'https://parent-site.com') return;
console.log('Message from parent:', event.data);
// Send response back
event.source.postMessage('Hello from iframe!', event.origin);
});
Learn More: For comprehensive coverage of iframe loading events and state management, explore our detailed guide on iframe onload events.
Iframe Properties Manipulation
Modify iframe properties and attributes dynamically:
// Get iframe element
const iframe = document.getElementById('myIframe');
// Change iframe source
iframe.src = 'https://new-example.com';
// Modify dimensions
iframe.width = '800';
iframe.height = '600';
// Update attributes
iframe.setAttribute('title', 'Updated iframe title');
iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin');
// Remove attributes
iframe.removeAttribute('frameborder');
// Check iframe properties
console.log('Iframe src:', iframe.src);
console.log('Iframe dimensions:', iframe.width + 'x' + iframe.height);
console.log('Iframe title:', iframe.title);
// Access iframe content (same-origin only)
try {
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
console.log('Iframe content:', iframeDoc.title);
} catch (e) {
console.log('Cannot access iframe content (cross-origin)');
}
Related Iframe Topics & Applications
Webpage Frame Implementation
Learn how to create modern frame layouts using iframes instead of deprecated HTML frames.
WordPress in an Iframe
Master WordPress iframe embedding with security best practices and responsive design.
Iframe Onload Events
Handle iframe loading events, detect loaded states, and implement professional loading indicators.
Iframe Embed Code Examples: Real-World Applications
YouTube Video Embed
<iframe
width="560"
height="315"
src="https://www.youtube.com/embed/VIDEO_ID"
title="YouTube video player"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
Google Maps Embed
<iframe
src="https://www.google.com/maps/embed?pb=MAP_EMBED_CODE"
width="600"
height="450"
style="border:0;"
allowfullscreen=""
loading="lazy"
referrerpolicy="no-referrer-when-downgrade">
</iframe>
Social Media Embed
<!-- Twitter Tweet -->
<iframe
src="https://platform.twitter.com/embed/Tweet.html?id=TWEET_ID"
width="550"
height="420"
style="border: 0;">
</iframe>
<!-- Facebook Post -->
<iframe
src="https://www.facebook.com/plugins/post.php?href=POST_URL"
width="500"
height="600"
style="border:none;overflow:hidden"
scrolling="no"
frameborder="0">
</iframe>
PDF Document Embed
<iframe
src="document.pdf#toolbar=1&navpanes=1&scrollbar=1"
width="100%"
height="600px"
style="border: 1px solid #ccc;">
</iframe>
Iframe Support in Browsers: Compatibility Guide
Browser Compatibility Table
Browser | Iframe Support | Version | Notes |
---|---|---|---|
Chrome | ✅ Full Support | 1.0+ | All iframe features supported |
Firefox | ✅ Full Support | 1.0+ | Complete iframe implementation |
Safari | ✅ Full Support | 1.0+ | All iframe attributes supported |
Edge | ✅ Full Support | 12.0+ | Modern iframe features |
Internet Explorer | ⚠️ Partial Support | 3.0+ | Basic iframe support, limited attributes |
Sample Iframe Collection: Ready-to-Use Examples
Inline Frame Example
<!-- Basic inline frame -->
<iframe
src="https://example.com"
width="300"
height="200"
title="Inline frame example">
</iframe>
<!-- With custom styling -->
<iframe
src="https://example.com"
style="
width: 100%;
height: 300px;
border: 2px dashed #ccc;
border-radius: 8px;
"
title="Styled inline frame">
</iframe>
Iframe Webpage Embed
<!-- Embed external webpage -->
<iframe
src="https://news-website.com/article"
width="100%"
height="800"
scrolling="auto"
title="News article embed">
</iframe>
<!-- With sandbox restrictions -->
<iframe
src="https://external-site.com"
sandbox="allow-scripts allow-same-origin"
width="100%"
height="600"
title="Sandboxed webpage">
</iframe>
Interactive Iframe Examples
<!-- Calculator iframe -->
<iframe
src="https://calculator-app.com"
width="400"
height="500"
style="border: 1px solid #ddd;"
title="Calculator application">
</iframe>
<!-- Form iframe -->
<iframe
src="https://form-service.com/contact"
width="100%"
height="600"
style="border: none;"
title="Contact form">
</iframe>
Advanced Iframe Patterns
<!-- Lazy loaded iframe -->
<iframe
src="about:blank"
data-src="https://heavy-content.com"
loading="lazy"
width="100%"
height="400"
title="Lazy loaded content">
</iframe>
<!-- Responsive iframe with CSS -->
<div class="responsive-iframe-wrapper">
<iframe
src="https://example.com"
title="Responsive iframe">
</iframe>
</div>
Ready to Create Your Own Iframe Examples?
Now that you've seen these iframe examples, use our free iframe generator tool to create your own custom iframe code.
Try Iframe Generator