Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Videos in HTML?

In the early days of the internet, adding videos to a website was a complicated task. You had to use third-party plugins like Flash, which not only slowed down the loading time but also caused compatibility and security issues. Thankfully, with the introduction of HTML5, adding videos to a web page has become much simpler. In this blog post, we will explore the basics of using videos in HTML, explain the <video> element, and provide examples to help you understand how to add videos to your web pages.

What is the <video> element?

The <video> element is an HTML5 feature that allows you to embed videos directly into your web page. It is a container for video files and contains attributes that help you control the appearance and behavior of the video, such as width, height, autoplay, or controls. It is important to note that the <video> element only works in modern browsers that support HTML5.

Let's take a look at a basic example of the <video> element:

<video src="example-video.mp4" controls></video>

In this example, we have a video file called example-video.mp4. We add the controls attribute to the <video> element to display playback controls, such as play, pause, and volume buttons.

The <source> element

In some cases, you might want to provide multiple video formats to ensure compatibility with different browsers. To do this, you can use the <source> element within the <video> element. The <source> element allows you to specify multiple video files with different formats, and the browser will choose the first compatible format it encounters.

Here's an example of using the <source> element:

<video controls>
  <source src="example-video.mp4" type="video/mp4">
  <source src="example-video.webm" type="video/webm">
  Your browser does not support the video tag.
</video>

In this example, we provide two video formats: MP4 and WebM. The type attribute specifies the MIME type of the video file. If the browser doesn't support any of the provided formats, it will display the text "Your browser does not support the video tag."

Video attributes

The <video> element has several attributes that allow you to customize the appearance and behavior of the video. Some of the most commonly used attributes are:

width and height: These attributes allow you to set the dimensions of the video. You can use either pixels or percentages, e.g., width="640" or width="100%".

autoplay: If you add this attribute to the <video> element, the video will start playing as soon as it's ready. It is important to note that some browsers block autoplay to provide a better user experience.

controls: As mentioned earlier, this attribute displays the default video controls, such as play, pause, and volume buttons.

loop: This attribute, when added to the <video> element, will cause the video to restart automatically once it finishes playing.

muted: Adding this attribute to the <video> element will mute the audio by default.

poster: This attribute allows you to specify an image file that will be displayed as a placeholder before the video starts playing. You can use a JPEG, PNG, or GIF image file format.

Here's an example that demonstrates the use of these attributes:

<video src="example-video.mp4" width="640" height="360" controls autoplay loop muted poster="example-poster.jpg"></video>

In this example, we have a video with a width of 640 pixels and a height of 360 pixels. The video will autoplay, loop, and have its audio muted by default. We also added a poster image called example-poster.jpg.

Styling the <video> element

You can also apply CSS styles to the <video> element, just like you would with any other HTML element. For example, you can add a border, apply a margin, or even add a box-shadow to the video. Here's an example of using CSS to style a video:

<!DOCTYPE html>
<html>
<head>
<style>
  video {
    border: 3px solid #ccc;
    margin: 20px;
    box-shadow: 5px 5px 10px #888;
  }
</style>
</head>
<body>

<video src="example-video.mp4" width="640" height="360" controls></video>

</body>
</html>

In this example, we added a 3-pixel solid border, a 20-pixel margin, and a 5-pixel box-shadow to the video.

Conclusion

In this blog post, we learned about the <video> element in HTML5 and how it makes embedding videos in web pages much simpler. We explored the <source> element for providing multiple video formats, discussed the various attributes available for the <video> element, and demonstrated how to style it using CSS. With this knowledge, you can now easily add videos to your web pages and create a more engaging and interactive browsing experience for your users. Happy coding!