LESSON 9.5
Embedding Multimedia
Add YouTube videos and Google Maps to any page
Learn how to embed YouTube videos and Google Maps using iframes, make videos responsive with a CSS wrapper, and customize YouTube playback with URL parameters.
💡 The Concept
To embed YouTube video in HTML, you use an iframe element that loads the YouTube player directly inside your page. The <iframe> (inline frame) element lets you embed content from another source directly into your page. YouTube videos, Google Maps, and many other third-party tools all use iframes. You do not host the content yourself. You just paste a code snippet that points to it.
The trick to embeds is first to find the Share button, and from there the Embed button. This lesson shows more detail on how to do it on YouTube and Google Maps, but there are many more websites you can embed from, including Vimeo and Soundcloud.
Embedding a YouTube Video
YouTube generates the iframe code for you. Here is how to get it:
- Go to any YouTube video and click the Share button below the player.
- Choose Embed from the sharing options.
- Copy the
<iframe>code that YouTube generates and paste it into your HTML.



Simply copy the embed code and place it where you want it on your web page. The example below is broken into several lines to make it easier to read. In real code, developers usually do not break it out like this, but the line breaks do not change how it works:
<iframe width="560" height="315"
src="https://www.youtube.com/embed/VIDEO_ID"
title="YouTube video player"
frameborder="0"
allowfullscreen>
</iframe>
Making Videos Responsive
The embed code YouTube gives you uses fixed pixel dimensions, which means the video will not resize on smaller screens. The fix is to wrap the iframe in a container div and use a CSS padding trick to maintain the 16:9 aspect ratio at any width:
<div class="video-container">
<iframe src="https://www.youtube.com/embed/VIDEO_ID"
title="Video description"
frameborder="0"
allowfullscreen>
</iframe>
</div>
.video-container {
position: relative;
padding-bottom: 56.25%; /* 16:9 ratio: 9 ÷ 16 = 0.5625 */
height: 0;
overflow: hidden;
}
.video-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
Credit for this responsive video technique goes to Flavio Copes.
YouTube URL Parameters
You can customize how an embedded YouTube video behaves by adding parameters to the src URL. Here is an example that autoplays silently, shows captions, and loops:
autoplay=1: starts playing immediatelymute=1: required by most browsers for autoplay to workcc_load_policy=1: turns on captionsloop=1: replays the video (requiresplaylist=VIDEO_IDto work)
<iframe src="https://www.youtube.com/embed/VIDEO_ID?autoplay=1&mute=1&cc_load_policy=1&loop=1&playlist=VIDEO_ID"
title="Video description"
frameborder="0"
allowfullscreen>
</iframe>
Tip: To loop a single video, set playlist to the same video ID as the one you are embedding, just like in the example above. On its own, loop=1 does not loop a single video. YouTube needs you to list that same video as a one item playlist, and then it replays from the start when it reaches the end.
Embedding Google Maps
Google Maps works the same way. Go to Google Maps, search for a location, click Share, then choose Embed a map. Copy the iframe code Google provides.
Maps can be any aspect ratio, so it is easiest to set the width to 100% and use a fixed height:
<iframe
src="https://www.google.com/maps/embed?pb=..."
width="100%"
height="300"
style="border:0;"
allowfullscreen
loading="lazy"
title="Map of location">
</iframe>
🎬 The Video
This video demonstrates how to embed and manage the size of a Google Map and a YouTube video.
Exercise: Try It Yourself
Step 1. Open JSFiddle.
Step 2. Go to YouTube, find any video, and copy its embed code. Paste it into JSFiddle. It works! But the video is fixed in size.

Step 3. Wrap the iframe in a .video-container div and add the responsive CSS. Resize the JSFiddle preview to confirm it scales. Now the video will fit in at any width.

Step 4. Go to Google Maps, find a location, and copy the embed code. Add it to your JSFiddle below the video with width="100%" and a fixed height.

❓ Frequently Asked Questions
An iframe, short for inline frame, is an HTML element that loads another web page or tool inside your page. Instead of hosting a video or map yourself, you embed a small iframe that points to it, and the other site does the work of displaying it. YouTube, Google Maps, Vimeo, and many other services give you ready-made iframe code to copy and paste.
The embed code that YouTube gives you uses fixed pixel dimensions, so the video stays that size no matter how wide the screen is. To make it responsive, wrap the iframe in a container div and use the CSS padding trick shown above, which keeps the 16:9 aspect ratio at any width. After that, the video scales down to fit phones and up to fill larger screens.
Yes. Embedding is the intended, allowed way to share YouTube videos, and it does not use your own hosting or bandwidth, since the video streams from YouTube. The video owner still gets the views and keeps control of the content. This is different from downloading a video and re-uploading it, which can violate copyright.
Yes. Many sites provide embed codes that work the same way, including Vimeo and SoundCloud for video and audio, Google Calendar, and various forms and chat tools. The pattern is almost always the same: look for a Share button, then an Embed option, and copy the iframe code it gives you.
Embed a Google Form. Google Forms handles the input fields and stores every response for you, with no server-side code to write. Build your form in Google Forms, click Send, choose the embed option (the angle brackets icon), and copy the iframe code, then paste it into your page just like a YouTube video or a map. It is a fast way to collect responses when you do not want to set up your own back end.
★ Key Takeaways
Video and audio can make a web page much more engaging. This lesson covered how to embed YouTube videos and other media using the iframe tag. Next, you will get your first introduction to JavaScript, the programming language that adds interactivity to websites. It is the third of the three core web languages, alongside HTML and CSS.
Google Ads
The costs of this website are partially offset by revenue from Google Ads.Tools of the Trade
HTML/CSS Lessons
-
-
-
1.3. HTML Tag Preview
-
-
-
2.2. The HTML Framework
-
-
2.4. HTML Headings
-
-
-
-
-
-
3.01. Introduction to CSS
-
3.02. How CSS Cascades
-
-
-
-
3.05. CSS Colors
-
3.06. RGBA Colors
-
-
3.08. Font and Text Properties
-
3.09. Classes and IDs
-
-
-
-
-
4.3. HTML Embedded Images
-
-
-
-
-
-
5.1. CSS Borders
-
5.2. CSS Border Radius
-
5.3. CSS Gradients
-
5.4. CSS Box Shadows
-
5.5. CSS Text Shadows
-
7.1. The Display Property
-
-
-
7.4. The Box Model
-
-
-
-
-
-
9.1. Website Tables
-
9.2. HTML Forms
-
-
9.4. Special Form Fields
-
9.5. Embedding Multimedia
-
-
9.7. Keyboard Shortcuts
-
10.1. What Is a Framework?
-
10.2. Font Awesome Icons
-
-
10.4. Bootstrap Grid System
-
-
10.6. Bootstrap Navigation
-
-
11.2. Purchasing a Domain Name
-
11.3. Website Hosting
-
11.4. Nameservers and DNS
-
11.5. FTP
-
11.6. GitHub Pages
