Academic Block

HTML YouTube Get Certified

How to Add a YouTube Video to HTML

You can easily embed YouTube videos into your web pages using an <iframe>. YouTube provides a ready-made embed code that you can copy and paste. Below you will find the most common and useful methods.

HTML YouTube Code Examples

1. Standard YouTube Embed (iframe)

The simplest and most recommended way to embed a YouTube video.

What this code does: Displays a YouTube video player using the official embed URL inside an iframe.

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>YouTube Embed Example</title>
  <style>
    body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
    iframe { max-width: 100%; }
  </style>
</head>
<body>
  <h2>Standard YouTube Embed</h2>
  <iframe width="560" height="315" 
          src="https://www.youtube.com/embed/ScMzIvxBSi4" 
          title="YouTube video player"
          frameborder="0" 
          allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" 
          allowfullscreen>
  </iframe>
  <p>Replace the video ID (ScMzIvxBSi4) with any public YouTube video ID.</p>
</body>
</html>

2. Responsive YouTube Embed

Make the video scale properly on mobile and tablet screens.

What this code does: Uses a container with a 16:9 aspect ratio so the video stays fully responsive.

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Responsive YouTube Embed</title>
  <style>
    body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
    .video-container {
      position: relative;
      padding-bottom: 56.25%; /* 16:9 */
      height: 0;
      overflow: hidden;
      max-width: 800px;
    }
    .video-container iframe {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      border: 0;
    }
  </style>
</head>
<body>
  <h2>Responsive YouTube Video</h2>
  <div class="video-container">
    <iframe src="https://www.youtube.com/embed/ScMzIvxBSi4" 
            title="YouTube video player"
            allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" 
            allowfullscreen>
    </iframe>
  </div>
  <p>This video scales correctly on all screen sizes.</p>
</body>
</html>

3. YouTube Embed with Privacy-Enhanced Mode

YouTube offers a privacy-enhanced mode that does not set cookies until the user plays the video.

What this code does: Uses the youtube-nocookie.com domain for better privacy.

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Privacy-Enhanced YouTube Embed</title>
  <style>
    body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
    iframe { max-width: 100%; }
  </style>
</head>
<body>
  <h2>Privacy-Enhanced Mode</h2>
  <iframe width="560" height="315" 
          src="https://www.youtube-nocookie.com/embed/ScMzIvxBSi4" 
          title="YouTube video player"
          frameborder="0" 
          allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" 
          allowfullscreen>
  </iframe>
  <p>Using youtube-nocookie.com reduces tracking until the user clicks play.</p>
</body>
</html>

How to Get the Embed Code from YouTube

  1. Open the YouTube video you want to embed.
  2. Click the Share button.
  3. Select Embed.
  4. Copy the provided <iframe> code and paste it into your HTML.

Important Notes

  • Always replace VIDEO_ID with the actual ID of the video.
  • The old Flash-based <embed> method is obsolete and no longer works reliably.
  • Prefer the responsive technique for mobile-friendly websites.
  • Use the privacy-enhanced domain (youtube-nocookie.com) when privacy is a concern.

Web Resources on HTML YouTube

1. MDN : Embedding YouTube Videos with iFrame
2. HTML.com : Embed YouTube Videos
3. Northwestern University : Embed Videos or Web Forms
4. Drexel University : Embed YouTube & Vimeo Videos
5. University of Queensland : Embed a YouTube Video

Questions and Answers related to HTML YouTube

+ How do I embed a YouTube video in HTML? >

Go to the YouTube video → click Share → Embed → copy the iframe code and paste it into your HTML page. Replace the video ID if needed.

+ How can I make a YouTube embed responsive? >

Wrap the iframe in a container that uses the padding-bottom technique (56.25% for 16:9) and position the iframe absolutely inside it. This keeps the correct aspect ratio on all screen sizes.

+ What is the privacy-enhanced mode? >

Use https://www.youtube-nocookie.com/embed/VIDEO_ID instead of the regular YouTube domain. Cookies are not set until the user actually plays the video.

+ Can I embed a YouTube video without using an iframe? >

The old Flash-based <embed> method is obsolete and no longer recommended. The official and most reliable way is still the <iframe> provided by YouTube.

+ Why does my YouTube embed sometimes show “refused to connect”? >

This usually happens because of Content Security Policy (CSP) restrictions, X-Frame-Options, or embedding a private/unlisted video. Make sure the video is public and that your site’s CSP allows YouTube.

🧪 Test Your HTML Code

Edit the HTML code on the left and click “Run Code” to see the result on the right.

📝 HTML Code
👁️ Preview (Rendered HTML)

Click “Run Code” to see the result here.