HTML Video

Imagine you want to show a home movie in your living room to your friends. In the real world, you'd need a TV or a projector and a way to play your movie, right? Similarly, when you want to display a video on a web page, HTML5 provides you with the "TV" (a <video> tag) to screen your video.

The Basics: Setting Up Your TV

In HTML, to start showing your video, you use the <video> element - think of it as deciding where to place your TV in the living room. Here’s a simple way to put it in your web page "living room":

<video src="movie.mp4" controls>
  Your browser does not support the video tag.
</video>
  • src="movie.mp4": This part is like choosing which DVD to play; it tells the browser the source file of your video. Instead of "movie.mp4", you would use the actual path to your video file.
  • controls: This attribute adds play, pause, and volume controls to your video "TV", just like a remote control does for your home TV.
  • The text inside the <video> tags is shown only if the browser doesn't support the video element, informing the visitor much like a "No Signal" message on your TV.

Diving Deeper: More Control Over Your Video Playback

But what if your friends prefer watching with subtitles, in fullscreen, or you have different formats of your home movie because not all TVs play the same format? HTML5 <video> has got you covered, like a versatile media player. Let's see how:

Supporting Different Formats

Not all browsers support the same video formats, so it's like having friends with different kinds of DVD players. To ensure everyone can watch your movie, you can offer different formats:

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

Here, source elements are used inside the video tag to specify multiple video formats. The browser will try to play the first one it supports.

Adding Subtitles

For subtitles, you can use the <track> tag to add a caption file, like handing out printed dialogues of your movie:

<video controls>
  <source src="movie.mp4" type="video/mp4">
  <track src="subtitles_en.vtt" kind="subtitles" srclang="en" label="English">
  Your browser does not support the video tag.
</video>
  • src="subtitles_en.vtt" is the path to your subtitles file.
  • kind="subtitles" tells the browser this track is for subtitles.
  • srclang="en" indicates the language is English.
  • label="English" is what will be displayed in the browser's menu for user selection.

Making It Look Good: Styling with CSS

Remember how CSS is like the paint and decorations for your house? You can also use CSS to style your video element:

video {
  width: 100%;
  height: auto;
  border: 2px solid black;
}

This would make your video player take the full width of its container (like adjusting your TV screen size), maintain its aspect ratio (so the video doesn’t look stretched), and add a nice border around it (just making it look a bit nicer on the webpage).

Conclusion

Just like setting up a TV and making sure everyone can watch your home movie comfortably, using the HTML5 <video> element allows you to embed videos on your web pages, ensuring broader compatibility and a better user experience. Start with the basic setup and experiment with additional features to enhance the viewing experience. Remember, the key is to ensure that your video content is accessible and enjoyable for all your website visitors, just as you'd ensure all your friends can watch your home movie comfortably in your living room.