Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to embed video in HTML

Understanding the Basics

Before we delve into the specifics of embedding videos in HTML, it is essential first to get a grasp of what HTML is. HTML, which stands for Hypertext Markup Language, is the standard markup language for documents designed to be displayed in a web browser.

Think of HTML as the skeleton of a webpage. It structures the content on the site and includes everything from headings and paragraphs to links, images, and yes, videos.

The Video Element in HTML

To embed a video in HTML, we use the <video> element. It's like a container that holds your video. It's as if you're telling the website, "Hey, there's going to be a video here. Get ready to display it."

Here's a simple example of how to use the <video> element:

<video width="320" height="240" controls>
  <source src="movie.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

In this block of code, the width and height attributes define the size of the video player on your webpage. The controls attribute adds video controls, like play, pause, and volume. The <source> tag is where you specify the video file's location and its type.

The text "Your browser does not support the video tag" will only be displayed if the user's browser does not support the <video> tag.

Different Video Formats

You might notice that the type attribute in the <source> tag above is set to "video/mp4". This signifies that our video file is in MP4 format. However, not all web browsers support all video formats.

For instance, while MP4 is supported by most browsers, older versions of Firefox might not support it. To ensure your video plays on all browsers, you can provide alternate video formats. Just remember, the browser will play the first source it understands, so order matters.

<video width="320" height="240" controls>
  <source src="movie.mp4" type="video/mp4">
  <source src="movie.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video>

In this example, if the browser does not support MP4, it will try to play the OGG video.

Controlling Video Playback

The controls attribute lets the user play, pause, and adjust the video's volume. But what if you want the video to start playing as soon as the page loads? That's where the autoplay attribute comes in.

<video width="320" height="240" autoplay>
  <source src="movie.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

In this code, the autoplay attribute tells the browser to start playing the video as soon as it's ready. Be cautious when using autoplay, as it can be disruptive to users.

Looping Videos

You can also make your video loop, meaning it will start over once it's finished, by using the loop attribute.

<video width="320" height="240" controls loop>
  <source src="movie.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

Muting Videos

To mute a video by default, use the muted attribute.

<video width="320" height="240" controls muted>
  <source src="movie.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

Poster Images

Sometimes, you might want to display an image before the video loads. This can be achieved with the poster attribute.

<video width="320" height="240" controls poster="poster.jpg">
  <source src="movie.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

In this example, "poster.jpg" is the image that will be displayed before the video loads.

Conclusion

Embedding videos in HTML is not as daunting as it may seem. With the use of the <video> element and a few key attributes, you can control how the video is displayed and behaves on your webpage. Remember to provide alternate video formats for maximum browser compatibility and use autoplay, loop, and mute attributes sparingly to ensure a positive user experience. With a little practice, you'll be able to incorporate videos into your web pages with ease.