How to embed a YouTube video: the code, privacy and layout
A YouTube embed is an iframe: a small window in your page that loads the YouTube player. Knowing what each part of the embed code does lets you make it responsive, lighter and kinder to visitors’ privacy.
The embed code, piece by piece
The Share > Embed option under a video gives code of this shape. Here the address is shown without its scheme; the real code starts with the secure https prefix.
<iframe width="560" height="315"
src="…www.youtube-nocookie.com/embed/VIDEO_ID"
title="YouTube video player"
allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"
allowfullscreen></iframe>
| Part | What it does |
|---|---|
src | The player address. VIDEO_ID is the 11-character ID from the watch address, the part after v=. |
width, height | The default size in CSS pixels. 560 × 315 is a 16:9 shape. |
title | The name a screen reader announces for the frame. Change it to describe the video. |
allow | Browser features the player may use, such as picture-in-picture. |
allowfullscreen | Lets the viewer switch to full screen. |
Privacy-enhanced mode
The embed dialog has an option for privacy-enhanced mode. It changes the host in the address from youtube.com to youtube-nocookie.com. YouTube describes this mode as reducing the tracking of visitors who load your page but do not play the video. It does not make the video private and does not stop data collection once someone presses play. If your site needs consent before loading third-party content, the safest approach is to show a placeholder and load the iframe only after the visitor clicks, as described below. Read YouTube’s own help page on embedding for the current details.
Useful player parameters
Add options to the end of the src address after a ?, separated by &:
start=90begins playback at 90 seconds. Convert minutes by multiplying by 60: 2 minutes 15 seconds is 2 × 60 + 15 = 135.end=150stops playback at 150 seconds.autoplay=1asks the player to start on load. Browsers usually block sound on autoplay, so it often needsmute=1too, and autoplaying video annoys many visitors.loop=1repeats a single video, but only together withplaylist=VIDEO_IDset to the same ID.controls=0hides the player controls. Keep them unless you supply your own.cc_load_policy=1shows captions by default when they exist.
Example: …/embed/VIDEO_ID?start=135&cc_load_policy=1. In HTML source, an & inside an attribute is safest written as &.
Making it responsive
A fixed 560-pixel frame overflows a 390-pixel phone screen. Remove the fixed size and let CSS keep the shape:
.video iframe {
width: 100%;
height: auto;
aspect-ratio: 16 / 9;
border: 0;
}
Check. At 560 pixels wide: 560 × 9 ÷ 16 = 315. At 640 pixels wide the frame becomes 360 pixels tall. Older layouts got the same result with a wrapper using padding-top: 56.25%, because 9 ÷ 16 = 0.5625.
Keeping the page fast
Each embedded player loads its own scripts and images before anyone presses play. Two techniques help:
- Lazy loading. Add
loading="lazy"to the iframe so the browser waits until the frame is near the visible area. - A click-to-load facade. Show a still image of your own with a play button. When the visitor clicks, your script replaces it with the real iframe. The page then loads nothing from the video host until the visitor asks, which also helps with consent requirements.
Common mistakes
- Pasting the normal watch address into an iframe. Only the
/embed/address works inside a frame. - Leaving the generic
titleon every video, so screen reader users cannot tell them apart. - Embedding several autoplaying videos on one page.
- Forgetting that the video owner can disable embedding, in which case the player shows an error.
Questions
Why does this page not show a live example?
Loading a real player would contact a third-party server for every reader. The code above is the complete pattern; paste it into your own page with your video ID to see it work.
Can I embed a playlist?
Yes. Use /embed/videoseries?list=PLAYLIST_ID as the path, with the playlist ID from the playlist address.