Projects

Videos in HTML

Hello there! In this lesson, we'll be exploring videos - a powerful multimedia element that can bring your web pages to life. Videos can engage your users, convey complex information, and enhance the visual appeal of your website. Let's dive into the world of videos in HTML and learn how to embed and customize videos effectively.

Embedding Videos in HTML

In HTML, video is embedded using the <video> tag. The <video> tag is **not** a self-closing tag. This means it requires a closing tag (</video>) to properly define the audio element and its content. Here's the basic structure of embedding audio in HTML:


<video src="video.mp4" controls>
  Your browser does not support the video element.
</video>

        

In the code above, the <video> tag is used to embed a video file. The "src" attribute specifies the source or URL of the video file. The "controls" attribute adds playback controls to the video player, allowing users to play, pause, and control the volume.

Supporting Multiple Video Formats

Similar to audio, different browsers support different video formats. It'sa good practice to provide multiple video formats within the <video>tag using the <source> tag. Here's an example:


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

In the code above, we included two <source> tags within the<video> tag. Each <source> tag specifies a different video format using the "src" attribute, and the "type" attribute indicates the MIME type of the video file.

Autoplay and Looping Videos

You can configure the <video> tag to automatically play the video when the web page loads and to loop the video continuously. Here's how you can do that:


        
<video src="video.mp4" controls autoplay loop>
  Your browser does not support the video element.
</video>

In the code above, we added the "autoplay" attribute to make the video start playing automatically when the web page loads. We also added the "loop" attribute to make the video repeat continuously until the user stops it.

You can also watch a video on how to upload from your computer

Customizing Video Player Appearance

By default, the video player in HTML is a simple rectangular box with playback controls. However, you can customize the appearance of the video player using CSS styles. While we won't dive into CSS in this lesson, it's important to know that you can style the video player to match your website's design or create custom video players.

Best Practices for Using Videos

Practice Time!

Now, let's put your knowledge into practice! Open your code editor and create a new HTML file. Experiment with embedding videos, providing different video formats, and customizing their appearance. Here's a simple exercise to get you started:

  1. Create a new HTML file and save it as "video.html" in your workspace folder.
  2. Embed a video file using the <video> tag. You can use a video file from your computer or a publicly available video file on the web. For example,
    
                
    <video src="video.mp4" controls>
     Your browser does not support video.
    </video>
                
    .
  3. Try providing multiple video formats using the <source> tag. Experiment with different video files and observe how the video player behaves.
  4. Customize the appearance of the video player using CSS styles (if you have knowledge of CSS). Otherwise, focus on understanding the basic structure of the <video> tag.

Conclusion

In this lesson, we've explored videos in HTML, including how to embed videos, provide multiple video formats, autoplay and loop videos, and more. Videos can enhance the user experience on your website, adding a dynamic and engaging element. Remember to use videos wisely, follow best practices, and always test your videos to ensure they play correctly on different devices and browsers. In the next lesson, we'll move on to creating forms in HTML, allowing users to input data and interact with your website. Stay tuned, and happy coding!