Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to embed a video in HTML

Understanding the Basics

Before we dive into the practical steps of embedding a video in HTML, it's crucial first to understand what HTML is. HTML, or HyperText Markup Language, is a coding language used to build and design web pages. It's like the foundation and structure of a house or the skeleton of a body. This analogy is to help you visualize what HTML does in a website's construction.

Now, when we talk about embedding a video in HTML, imagine you're adding furniture (in this case, the video) to your already built house (the webpage). The furniture enhances the look and functionality of the house, just as the video enhances the user's experience on your webpage.

The Video Element: The Basics

Embedding a video into your HTML code is a lot simpler than it may seem. HTML5 introduced the <video> tag, which has made it easier to embed multimedia content into web pages.

Here is a simple example of how the <video> tag is used:

<video width="320" height="240" controls>
  <source src="movie.mp4" type="video/mp4">
</video>

In this code snippet, we are defining a video's width and height, and adding controls so that a user can play, pause, or adjust the volume of the video. The source tag within the video tag is where we specify the video file's location.

Digging Deeper: Attributes

HTML tags often come with attributes that allow us to further customize the tag's behavior. For the <video> tag, some of the commonly used attributes are:

  • controls: This attribute adds video controls, like play, pause, and volume.
  • src: This attribute specifies the source file for the video.
  • width and height: These attributes specify the width and height of the video player.
  • autoplay: This attribute starts the video automatically once the page loads.
  • loop: This attribute loops the video playback.
  • muted: This attribute mutes the video by default.

Here's an example using these attributes:

<video width="320" height="240" controls autoplay loop muted>
  <source src="movie.mp4" type="video/mp4">
</video>

Source and Type Attributes

You may have noticed the src and type attributes inside the source tag in our examples. The src attribute is used to specify the path (URL) to the video file. The type attribute is used to specify the media type and its respective codec.

In simpler terms, the type attribute tells the browser what the file type is and how to decode it. For example, "video/mp4" means the file is a video file, and it should be decoded using the MP4 codec.

Providing Multiple Source Files

Sometimes, you might want to provide multiple source files for the same video, each in a different format. The reason for this is that not all browsers support all video formats. By providing multiple source files, you increase the chances that the video will play, regardless of which browser a visitor is using.

Here's an example of how you can provide multiple source files:

<video width="320" height="240" controls>
  <source src="movie.mp4" type="video/mp4">
  <source src="movie.ogg" type="video/ogg">
  <source src="movie.webm" type="video/webm">
</video>

In this code, we have three different formats of the same video. The browser will use the first recognized format.

Including a Fallback Message

Despite our best efforts, there may be instances where the video cannot be played, perhaps because the user's browser doesn't support the video tag, or the video formats provided. In these cases, it's a good practice to include a fallback message.

Here's how you can add a fallback message:

<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>

Wrapping Up

Embedding a video in HTML is not as complex as it may seem. With the <video> tag and a few attributes, you can easily and effectively include videos in your webpages. Remember to always provide multiple source files and include a fallback message to make your webpage more accessible to a wider range of users. Happy coding!