HTML YouTube
In this page:
<iframe src="https://www.youtube.com/embed/VIDEO_ID" width="width" height="height"></iframe>
Embedding YouTube Videos
You can embed any YouTube video on your webpage by using an iframe tag. Instead of linking to the normal video page, you point the iframe's src attribute to YouTube's dedicated embed URL, which contains the video's unique ID.
- Locate the unique 11-character video ID at the end of the YouTube video link to construct your embed code.
- Accepted attributes:
- src — https://www.youtube.com/embed/VIDEO_ID
- width / height — pixels
- title — accessible description
- allow — accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share
- allowfullscreen — boolean
- referrerpolicy / loading
Example: Embedding YouTube Videos
<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ"></iframe>
Controlling Video Autoplay
You can configure your embedded YouTube video to start playing automatically when the page loads by appending the autoplay=1 parameter to your source URL. Modern browsers require you to mute the video for autoplay to work.
- Always pair autoplay=1 with mute=1 to make sure browsers do not block your auto-playing videos.
- Accepted values:
- autoplay — 0 | 1 (URL parameter)
- mute — 0 | 1 (autoplay usually needs mute=1)
- allow="autoplay" — iframe permission
Example: Controlling Video Autoplay
<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=1&mute=1"></iframe>
Creating Looping Playbacks
To make your video play continuously, you can append the loop=1 parameter to your embed URL. YouTube's player also requires you to specify the playlist parameter with the same video ID to enable looping.
- Using a short, looping video is a great way to create interactive, dynamic page headers.
- Accepted values:
- loop — 0 | 1 (URL parameter)
- playlist — VIDEO_ID (required to loop a single video)
Example: Creating Looping Playbacks
<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ?loop=1&playlist=dQw4w9WgXcQ"></iframe>
Customizing Player Controls
You can customize the look of the YouTube player on your page by appending parameters. For example, controls=0 hides the play and volume buttons, while fs=0 disables the fullscreen button.
- Hiding controls is useful for background videos but can frustrate users trying to watch tutorials.
- Accepted values:
- controls — 0 | 1 (URL parameter)
- start — seconds
- end — seconds
- rel — 0 | 1
Example: Customizing Player Controls
<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ?controls=0&fs=0"></iframe>
YouTube Player Security
To protect user privacy and secure your website, always load embedded videos over secure HTTPS connections and apply standard iframe sandbox attributes to restrict permissions.
Example: YouTube Player Security
<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" sandbox="allow-scripts allow-same-origin"></iframe>
Privacy-Enhanced Mode (youtube-nocookie.com)
Using youtube-nocookie.com instead of the regular youtube.com domain for embeds prevents YouTube from setting tracking cookies in the visitor's browser until they actually choose to play the video.
This is a common requirement for sites that need to comply with strict privacy regulations before user consent is given.
- Use the nocookie domain by default for any embedded video shown before a user has accepted cookie tracking, especially on sites serving visitors in the EU.
- Accepted attributes:
- src — https://www.youtube-nocookie.com/embed/VIDEO_ID
- title — accessible description
- allowfullscreen — boolean
Example: Privacy-Enhanced Mode (youtube-nocookie.com)
<iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ"></iframe>
Embedding a Playlist
Instead of embedding a single video, you can embed an entire YouTube playlist using the videoseries endpoint along with a playlist ID, letting visitors watch through a whole curated sequence of videos without leaving your page.
- Use the listType=playlist and list parameters together to embed a full playlist rather than just one video.
- Accepted values:
- listType — playlist
- list — PLAYLIST_ID
- index — position in playlist (URL parameters)
Example: Embedding a Playlist
<iframe src="https://www.youtube.com/embed/videoseries?list=PLxxxxxxxxxxxx"></iframe>
- Using the standard YouTube watch URL in your iframe src instead of the dedicated embed path.
- Attempting to auto-play videos without muting them first, which browsers will block.
- Applying a strict sandbox without allowing scripts, which prevents the YouTube player from loading.
- Embed YouTube videos on your page by pointing an iframe src to YouTube's dedicated embed URL path.
- Pair the autoplay parameter with mute to start videos automatically on page load.
- Use sandboxing and secure HTTPS links to protect user privacy and secure your site's layouts.
Standard YouTube embeds and parameters are natively supported by all modern web browsers.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: