How to add a video in HTML

problem

You’re learning HTML and wish to place a video item on your page. In this post we will see how to load and play a video from a local video file and remotely via a URL.

SOLUTION

Using the <video> tag. It has an opening and closing tag. 

Inside it we can use nested tags like <source> to specify the source of the video file and the type. 

Also on the video tag we can specify via its attributes to show player controls or not. Let’s see a simple example of loading a local mp4 file.

The video file is titled example.mp4.

I will create a directory video example and place the video file inside.

Then I’ll create the video.html file with the following contents.

So the directory will look like this:

video example directory

In video.html, place the following code

video.html
				
					<!DOCTYPE html>

<body>
	<video controls="true">
  	    <source src="example.mp4" type="video/mp4" />
      	<p>
          	Oops, the browser does not support video
      	</p>
	</video>
</body>

</html>

				
			
video.html
				
					<!DOCTYPE html>

<body>
	<video controls="true">
  	    <source src="example.mp4" type="video/mp4" />
      	<p>
          	Oops, the browser does not support video
      	</p>
	</video>
</body>

</html>

				
			
output
HTML video output

Note: this video has no audio.

As you can see above, at line 6, the <p> tag is used. That is , if the current browser does not support the video tag, it will show the message “OOps, the browser does not support video”. But nowadays most of the browsers do support it as you can see from the screenshot below and on caniuse website.

HTML video tag baseline compatibility MDN

I added the attribute controls=”true” so we can see the buttons to click play, pause, adjust the volume, etc.

There are more attributes that can be added and change the tag’s behavior for example the

autoplay attribute (Boolean).

If we set it to true:

video.html
				
						<video controls="true" autoplay="true">
				
			

The video will start playing automatically. Note: the user has to approve the autoplay beforehand.

Autoplay warning in Firefox

To enable it in Firefox, click on the icon appearing in the address bar as shown below:

 

enable autoplay on Firefox
Loading from a remote source

All we need to do is to change the src attribute as follows:

video.html
				
					<source src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4" type="video/mp4" />
				
			
output
video output (remote)

Note: this video has audio.

We can also add the attribute poster that will show a poster image before the video starts playing.

For example:

video.html
				
					<video controls="true" poster="https://programmerabroad.com/wp-content/uploads/2021/01/free_logo.png">
				
			
output
video output with poster attribute

conclusion

In this post we saw how to use the <video> tag in HTML and specify the control, autoplay, poster attributes. Moreover, the tag can handle more than one video type like MP4, WebM and OGG.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x