How to Embed a YouTube Video in HTML (Complete Guide)
To embed a YouTube video in HTML, insert an <iframe> tag into your page and point the src attribute to YouTube's embed URL format (https://www.youtube.com/embed/VIDEO_ID). This renders the native YouTube media player directly inside your webpage without requiring custom video player software. Below is a complete walkthrough showing how to get the code, customize player behavior, and make the embed fully responsive across mobile and desktop screens.
The Quick Way: Copying the Default Embed Code
YouTube provides a pre-configured HTML snippet for every public and unlisted video on its platform. If you only need a basic player, you can grab this code in seconds.
- Open the YouTube video in your web browser.
- Click the Share button located beneath the video title.
- Select the Embed icon from the share menu.
- Copy the code displayed in the right-hand panel.
- Paste the snippet directly into your HTML document where you want the video to appear.
<iframe
width="560"
height="315"
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
title="YouTube video player"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
referrerpolicy="strict-origin-when-cross-origin"
allowfullscreen>
</iframe>
How to Embed YouTube Video in HTML Manually
While copying from YouTube works, writing or editing the <iframe> manually gives you full control over accessibility, performance, and player behavior. If you only have a video link or watch URL, you can extract the eleven-character identifier using our YouTube Video ID Finder & Link Converter and assemble your own clean tag.
Essential Iframe Attributes
src: Specifies the embed URL. Always usehttps://www.youtube.com/embed/VIDEO_ID. Do not use a standard watch link (youtube.com/watch?v=...) here, as YouTube will block watch links from rendering inside frames.title: Provides an accessible label for screen readers. Replacing the generic "YouTube video player" with the actual video title improves your site's accessibility.allow: Defines the browser feature policies granted to the frame, such asautoplay,encrypted-media, andpicture-in-picture.allowfullscreen: Lets viewers expand the video to full-screen mode.loading="lazy": Defers loading the video player until the user scrolls near it, significantly improving initial page load speed.
Here is an optimized manual snippet:
<iframe
src="https://www.youtube.com/embed/M7lc1UVf-VE"
title="YouTube Video Player Tutorial"
width="560"
height="315"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
referrerpolicy="strict-origin-when-cross-origin"
allowfullscreen>
</iframe>Making Your YouTube Embed Responsive
By default, YouTube assigns static dimensions (width="560" and height="315"). On a smartphone screen, a 560-pixel-wide player overflows the layout, creating awkward horizontal scrolling. You can make your embed fluid and maintain its standard 16:9 aspect ratio using modern CSS.
Option 1: Modern CSS (aspect-ratio)
Modern browsers support the CSS aspect-ratio property, which allows you to build a responsive player with just a few lines of CSS:
<div class="video-container">
<iframe
src="https://www.youtube.com/embed/M7lc1UVf-VE"
title="Responsive YouTube Video"
allowfullscreen>
</iframe>
</div>.video-container {
width: 100%;
max-width: 800px; /* Adjust as needed */
margin: 0 auto;
}
.video-container iframe {
width: 100%;
aspect-ratio: 16 / 9;
border: 0;
}Option 2: The Padding-Bottom Technique (Legacy Support)
If you need compatibility with legacy browsers that lack aspect-ratio support, wrap the iframe in a container that calculates height based on a percentage of its width (9 / 16 = 56.25%):
.responsive-wrapper {
position: relative;
padding-bottom: 56.25%; /* 16:9 ratio */
height: 0;
overflow: hidden;
max-width: 100%;
}
.responsive-wrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}Customizing Playback with URL Parameters
You can alter how YouTube behaves by appending query parameters to the src attribute. Combine multiple parameters using an ampersand (&).
| Parameter | Values | Purpose |
|---|---|---|
start | Integer (seconds) | Starts playback at a precise second mark |
end | Integer (seconds) | Stops playback at a specified timestamp |
autoplay | 1 or 0 | Automatically starts playback on load (requires mute=1) |
mute | 1 or 0 | Mutes video audio by default |
controls | 1 or 0 | Shows or hides the player controls at the bottom |
loop | 1 or 0 | Loops playback (requires playlist=VIDEO_ID) |
Example: Muted Autoplay
Most modern browsers prevent videos from autoplaying with sound. If you want a video to begin playing automatically upon page load, you must combine autoplay=1 with mute=1:
<iframe
src="https://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&mute=1"
title="Autoplaying YouTube Embed"
width="560"
height="315"
allow="autoplay; encrypted-media"
allowfullscreen>
</iframe>Example: Starting at a Specific Time
To skip introductory titles or jump straight to a tutorial step, append the start parameter with the exact time in seconds. For a step-by-step breakdown on calculating these values, check our guide on how to start a YouTube video at a specific time.
If you prefer not to write parameter strings by hand, our free YouTube Embed Code Generator creates clean, custom embed code with your desired controls, colors, and start times automatically.
Common Embed Mistakes and How to Avoid Them
Even experienced web developers run into playback errors when working with iframe embeds. Keep these rules in mind:
- Using the wrong URL format: Pasting
https://www.youtube.com/watch?v=VIDEO_IDinto an<iframe>results in a refused connection or blank box. Always swap/watch?v=for/embed/. - Video privacy settings: Private videos cannot be embedded on external websites. If an embed displays an error, verify in YouTube Studio that the video is set to Public or Unlisted.
- Embedding restrictions: Creators and rights holders can disable embedding for specific videos in their advanced settings. If a video refuses to load despite correct code, the owner has disabled third-party player embeds.
- Missing
border: 0: Older browsers apply an ugly beveled border around iframes. Includeborder: 0;in your CSS orframeborder="0"in your HTML to keep presentation clean.
For more advanced integration techniques, responsive layouts, and optimization tips, explore our dedicated Embedding Guides.
Frequently asked questions
Can I embed a YouTube video without showing related videos at the end?
YouTube no longer allows hiding related videos completely. Setting the parameter rel=0 ensures that recommended videos shown at the end come only from your own channel rather than competing channels.
Why is my embedded YouTube video showing a black screen or error?
This usually happens if you used a standard watch URL instead of the /embed/ URL, or if the video creator has turned off third-party embeds in YouTube Studio.
How do I embed a YouTube Shorts video in HTML?
Take the 11-character video ID from the Shorts URL (youtube.com/shorts/VIDEO_ID) and insert it into a standard embed URL: https://www.youtube.com/embed/VIDEO_ID. You can then adjust the iframe dimensions to 9:16 vertical ratio if desired.

