iframes and Embeds
The <iframe> tag embeds another web page inside your page. Used for YouTube videos, Google Maps, and external widgets. The name stands for Inline Frame.
Basic iframe
<iframe src="https://example.com" width="600" height="400" title="Example website" loading="lazy"> </iframe>
Embedding a YouTube Video
<!-- From YouTube: Share > Embed > copy the iframe --> <iframe width="560" height="315" src="https://www.youtube.com/embed/VIDEO_ID" title="HTML Tutorial for Beginners" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope" allowfullscreen loading="lazy"> </iframe>
Responsive iframe
iframes have a fixed size by default which breaks on mobile. Wrap in a div to make it responsive:
<style> .video-wrap { position: relative; padding-bottom: 56.25%; /* 16:9 */ height: 0; } .video-wrap iframe { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } </style> <div class="video-wrap"> <iframe src="https://www.youtube.com/embed/VIDEO_ID" title="Video"></iframe> </div>
When embedding untrusted content, add sandbox="" to block scripts, forms, and popups inside the frame. You can selectively allow: sandbox="allow-scripts allow-forms".
Many sites set X-Frame-Options: DENY to prevent being embedded. If an iframe shows blank, the site has blocked embedding. YouTube, Google Maps, and Spotify all support embedding.
iframes were introduced in HTML 4 in 1997. They solve the problem of including third-party content — payment widgets, maps, video players — without running their full code in your page context.