Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to embed youtube video in HTML

Understanding Embedding

Imagine you're reading a fantastic book, and in the middle of the story, there's a stunning picture that adds to the narrative. This picture wasn't painted directly on the page; instead, it was created separately and then placed in the book during the printing process. This is a simple analogy for embedding, where we insert a separate piece of content (like a YouTube video) into another medium (like an HTML document).

What is a YouTube Video Embed?

Embedding a YouTube video means that you're taking that video from YouTube's platform and including it in your webpage. This allows viewers to watch the video directly from your page without having to leave and go to YouTube's site.

How YouTube Makes Embedding Possible

YouTube provides a feature that allows you to embed videos on your website directly. They create a unique piece of code for each video that you can copy and paste into your HTML document. This code is an 'iframe', which stands for inline frame.

An iframe can be thought of as a window on your webpage that shows another website. In this case, the website it's showing is a YouTube video player with your selected video.

Breaking Down the Embed Code

Let's look at an example YouTube embed code:

<iframe width="560" height="315" src="https://www.youtube.com/embed/3JZ_D3ELwOQ" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

This looks complex, but we can break it down:

  • <iframe>: This is the HTML tag that creates the window.
  • width and height: These set the size of the window.
  • src: This specifies the URL of the webpage to display in the window. It's a specific YouTube link that plays the video without showing the rest of the YouTube interface.
  • frameborder: This sets the border around the iframe. "0" means no border.
  • allow: This sets several options for the video player. For example, "autoplay" allows the video to start playing as soon as it's loaded.
  • allowfullscreen: This allows the video to be played in full screen.

How to Embed a YouTube Video in HTML

Now that we understand the structure of the embed code, let's see how we can use it:

  1. Find the Video You Want to Embed

Go to YouTube and find the video you want to include in your webpage. Under the video, click on the "Share" button, and then click on the "Embed" button in the pop-up window. This will bring up the embed code.

  1. Copy the Embed Code

The embed code will be in a text box. Just click the "Copy" button to copy it to your clipboard.

  1. Paste the Embed Code into Your HTML Document

Go to your HTML document and decide where you want the video to be. Paste the code at that location in your HTML.

And voila! You've embedded a YouTube video in your HTML. It's as simple as copying and pasting.

Customizing the Embed Code

The default embed code works fine, but you can also customize it to better fit your webpage. For example, you can change the width and height to make the video player larger or smaller. Just be sure to maintain the aspect ratio to prevent the video from being distorted.

<iframe width="800" height="450" src="https://www.youtube.com/embed/3JZ_D3ELwOQ" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

In this example, we changed the width to 800 and the height to 450.

Conclusion

Embedding a YouTube video in HTML can seem daunting at first, but once you understand the structure of the embed code, it's a straightforward process. Just find the video you want, copy the embed code, and paste it into your HTML. And remember, you can always customize the code to make the video fit better on your webpage. Happy embedding!