Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to embed a youtube video in HTML

Grasping the Concept

To start, let's imagine that HTML is a digital canvas. Just like how you would place elements such as colors, shapes, and textures on a physical canvas to create an artwork, HTML allows you to place elements such as text, images, and in this case, videos, on a digital canvas to create a webpage.

Understanding Embedding

Embedding is like pasting a sticker on your digital canvas. When you embed a Youtube video in HTML, you're essentially pasting a 'video sticker' onto your webpage. The video isn't technically 'inside' your website. Instead, your website just holds a window that shows the video from its original Youtube location.

The Basics of Embedding a Youtube Video

Let's start with the basic structure of the HTML code used to embed Youtube videos.

<iframe width="560" height="315" src="https://www.youtube.com/embed/VIDEO_ID" frameborder="0" allowfullscreen></iframe>

The iframe tag is the sticker that holds the video window. Inside this tag, there are several attributes that define the properties of the window, such as its width, height, and the video it's supposed to display.

width and height: these two attributes define the size of the video window on your webpage. You can adjust these numbers to fit your design needs.

src: this is perhaps the most important attribute as it specifies the source of the video, i.e., the video's URL. Note that it's not the standard Youtube link that you'd see in your address bar when watching a video. Instead, it's a link specifically designed for embedding, and you can find it in the 'Share' menu on Youtube. The VIDEO_ID part is unique to each video and you'll have to replace it with the ID of the video you want to embed.

frameborder: this attribute is used to define whether or not to display a border around the video. '0' means no border, and '1' means there is a border.

allowfullscreen: this attribute is used to specify whether or not to allow the video to be played in full-screen mode.

Finding the Video ID

The VIDEO_ID is a unique identifier associated with every Youtube video. It's a string of characters that you can find in the video's URL. For example, in the link https://www.youtube.com/watch?v=dQw4w9WgXcQ, the video ID would be dQw4w9WgXcQ.

To get the embed link, you can click on the 'Share' button under the video, then click on 'Embed'. A box with the embed link will pop up. You can copy this link and replace VIDEO_ID in the basic HTML structure with the ID of your video.

Customizing The Video Player

Youtube also allows you to customize the video player to some extent. For example, you can decide whether or not to show video controls, autoplay the video, or loop the video. These options can be added to the src URL as URL parameters.

  • To autoplay the video, add ?autoplay=1 at the end of the URL.
  • To hide video controls, add ?controls=0.
  • To loop the video, add ?loop=1.

Remember that if you're using multiple parameters, they need to be separated by an '&' symbol. For example, to autoplay and loop a video, the URL would look like this https://www.youtube.com/embed/VIDEO_ID?autoplay=1&loop=1.

Wrapping Up

Embedding a Youtube video in HTML is a simple process that involves adding an iframe tag to your HTML code and customizing it to fit your needs. By understanding and manipulating the attributes in the iframe tag, you can control how the video is displayed on your webpage.

Remember, learning to code is like learning a new language. It takes time and practice. So, don't worry if you don't get it right away. Keep experimenting and tweaking the code, and soon enough, it will become second nature.