Difficulty: Intermediate
Embedding allows you to include external content from other websites or services directly within your page. The most common method is the <iframe> (inline frame) element, which loads a completely separate HTML document inside a rectangular region on your page. Iframes are used extensively for embedding YouTube videos, Google Maps, social media posts, payment forms, and third-party widgets.
The <iframe> element creates a nested browsing context -- essentially a browser window within your page. The src attribute specifies the URL of the page to embed. Important attributes include width and height for sizing, title for accessibility (screen readers announce the iframe by its title), and loading="lazy" for performance. The frameborder attribute is deprecated in HTML5; use CSS border: none instead.
Security is a critical concern with iframes. The sandbox attribute restricts what the embedded content can do. Without sandbox, an embedded page has the same capabilities as if the user navigated to it directly, including running scripts, submitting forms, and using popups. The sandbox attribute with no value applies maximum restrictions. You can selectively allow capabilities with values like allow-scripts, allow-same-origin, allow-forms, and allow-popups. Always sandbox untrusted content.
The <embed> and <object> elements are older ways to embed content but are still occasionally used. <embed> is a void element that embeds external content, often used for plugins (though Flash is dead, PDF embeds still use it). <object> is more feature-rich and can include fallback content between its tags. In practice, <iframe> has replaced most use cases for both elements. The main remaining use for <object> is embedding SVG files with fallback images, and <embed> is sometimes used for inline PDF viewing.
Making embedded content responsive, especially videos, requires a wrapper technique because iframes have fixed dimensions. The modern approach uses the CSS aspect-ratio property on a container. The traditional approach uses the padding-bottom percentage trick to maintain a 16:9 or other aspect ratio. Both methods ensure the embed scales proportionally on different screen sizes while maintaining the correct aspect ratio.
<!-- Embed a YouTube video -->
<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ"
width="560"
height="315"
title="Video: Introduction to Web Development"
loading="lazy"
allowfullscreen>
</iframe>
<!-- Embed Google Maps -->
<iframe src="https://maps.google.com/maps?q=new+york&output=embed"
width="600"
height="450"
title="Map of New York City"
loading="lazy"
style="border: none;">
</iframe>
The title attribute is essential for accessibility -- screen readers announce it so users know what the iframe contains. Use loading='lazy' for embeds below the fold. The allowfullscreen attribute enables the fullscreen button in video players.
<!-- Maximum restrictions (nothing allowed) -->
<iframe src="https://untrusted-site.com/widget"
sandbox
title="Third-party widget">
</iframe>
<!-- Selective permissions -->
<iframe src="https://payment-form.example.com"
sandbox="allow-scripts allow-forms allow-same-origin"
title="Payment form">
</iframe>
<!-- Allow popups for OAuth flows -->
<iframe src="https://auth-widget.example.com"
sandbox="allow-scripts allow-popups allow-same-origin"
title="Login widget">
</iframe>
The sandbox attribute with no value applies all restrictions: no scripts, no forms, no popups, no same-origin access. Add specific allow-* values to grant only the permissions the embedded content needs. This follows the principle of least privilege.
<!-- Modern approach with aspect-ratio -->
<div class="embed-container">
<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ"
title="Tutorial video"
allowfullscreen>
</iframe>
</div>
<style>
.embed-container {
position: relative;
width: 100%;
aspect-ratio: 16 / 9;
}
.embed-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: none;
}
</style>
The aspect-ratio CSS property sets the container to maintain a 16:9 ratio at any width. The iframe is positioned absolutely inside to fill the container completely. This ensures the video scales properly on all screen sizes.
<!-- Embed a PDF document -->
<object data="/docs/report.pdf"
type="application/pdf"
width="800"
height="600">
<p>Your browser does not support PDF viewing.
<a href="/docs/report.pdf">Download the PDF</a>.
</p>
</object>
<!-- Embed an SVG with fallback -->
<object data="/images/diagram.svg"
type="image/svg+xml"
width="400"
height="300">
<img src="/images/diagram.png"
alt="System architecture diagram">
</object>
<!-- Simple embed (void element, no fallback) -->
<embed src="/docs/report.pdf"
type="application/pdf"
width="800"
height="600">
The <object> element can include fallback content between its tags, shown when the primary content cannot be displayed. This makes it useful for PDF embeds with download fallbacks and SVG embeds with PNG fallbacks. <embed> is simpler but offers no fallback mechanism.
iframe Element, embed Element, object Element, sandbox Attribute, Security, Responsive Embeds