We’ve covered the basics of adding a video to your web page using the
<video> element and briefly listed the
<video> element’s more advanced features in an earlier blog post.
In this blog post, we’ll take a deeper look at 10 of those advanced features and explain with code examples how you can use them on your website in your HTML video players.
- Specifying multiple sources for a video
- Showing or hiding the video player’s controls
- Start or stop the video at a certain point or timestamp
- Show a video poster or thumbnail before the video is started
- Preload the video before playback
- Play a video inline in Safari in iOS
- Autoplay, loop and mute
- Showing captions or subtitles during playback
- Fitting portrait videos in landscape players using the object-fit CSS property
Specifying multiple sources for a video
Not all browsers support all video containers and codecs. To suit all browsers you can provide multiple video files as the source for one video player.
Multiple sources can be specified by using
<source> elements. It is also recommended that you specify the MIME type using the optional
Here’s an example:
<video controls> <source src="vid1.webm" type='video/webm;codecs="vp8, opus"'/> <source src="vid2.mp4" type='video/mp4;codecs="avc1.4D401E, mp4a.40.2"'/> </video>
The list of sources is tried from top to bottom.
If only one video format is available, it can be specified directly using the
Showing or hiding the video player’s controls
Controls like play/pause, volume, full-screen toggle and the seek slider can easily be toggled using the
<video controls src="https://addpipe.com/sample_vid/short.mp4" preload="none"></video>
If you don’t specify the attribute the controls won’t be shown.
Some specific controls can be hidden separately by using the
controlsList attribute (Chrome 58+ only), for example:
<video controls controlsList="nofullscreen nodownload" src="https://addpipe.com/sample_vid/short.mp4" preload="none"> </video>
In the example above the fullscreen and download buttons have been turned off.
You can find more examples on how to control the browser UI here.
Start or stop the video at a certain point or timestamp
Using Media Fragments (the
#t= anchor in the
src) you can specify the time at which the video should start playback and end playback. In this example, the video playback will start at second 15 and end at second 20:
<video controls src="https://addpipe.com/sample_vid/short.mp4#t=15,20" preload="metadata"> </video>
#t=10,20 => results in the time interval
#t=,20 => results in the time interval
#t=10 => results in the time interval
Show a video poster or thumbnail before the video is started
If the video is not played automatically it’s a good idea to show a video thumbnail and thus giving viewers a glimpse of the content.
For the video to have a poster you simply need to add the
poster attribute and the URL to the poster image:
<video controls src="https://addpipe.com/sample_vid/short.mp4" poster="https://addpipe.com/sample_vid/poster.png" preload="none"> </video>
Preload the video before playback
A video can be preloaded in multiple ways by adding the
The following options exist:
- none – no preload is done
- metadata – only the metadata is preloaded: dimensions, first frame, track list, duration, etc
- auto – the audio/video should start loading as soon as the page loads
In all the above players we’ve used
preload="none" to prevent the video from being loaded as it might use considerable data on mobile devices. Here’s how to allow the browser to preload the video metadata and show the 1st frame as the poster:
<video preload="metadata" controls src="https://addpipe.com/sample_vid/short2.webm" preload="metadata"> </video>
Play a video inline in Safari on iOS
Safari on iOS 10+ supports inline playback of videos. Up until and including iOS9, web videos, when played, would show full screen on the device. To enable inline playback need to use the
<video playsinline src="https://addpipe.com/sample_vid/short.mp4"> </video>
Autoplay, loop and mute
A video can be auto played as soon as it is ready by adding the
<video autoplay controls src="https://addpipe.com/sample_vid/short.mp4"> </video>
Muted autoplay works in Safari on iOS10+ (while not in low power mode) and in Chrome 53+ on Android. Check New <video> Policies for iOS and Muted Autoplay on Mobile for more details. On mobile devices, Google and Apple previously blocked videos from autoplaying.
If you wish the video to play in a loop you can use the
<video loop controls src="https://addpipe.com/sample_vid/short.mp4"></video>
Mute the sound
Sometimes the audio of the video is not necessary. A good example of this would be if you are capturing the live feed of a webcam and displaying it in a video element and you wish the avoid the audio loopback.
To turn off the audio of the video you can simply specify the
<video muted controls src="https://addpipe.com/sample_vid/short.mp4"> </video>
Showing captions or subtitles during playback
By using the
<track> element you can add subtitles, screen reader descriptions, and captions to the video.
<track> element functions exactly like a
<source> element within the
<video> element. It has a
src attribute that points to a file in WebVTT format. By using the
label you can specify the name that will be displayed to the user in the UI. With
srclang you can specify the language for the captions. The
kind attribute will set how the track should be used with the following options: subtitles (default), captions, descriptions, chapters and metadata.
<video controls src="https://addpipe.com/sample_vid/short2.webm"> <track src="https://addpipe.com/sample_vid/example.vtt" label="English subtitles" kind="subtitles" srclang="en"> </track> </video>
Fitting portrait videos in landscape players using object-fit
contain– the video will be scaled to fit the container while preserving the aspect ratio, letterboxing will be present around the video
cover– the video is scaled to fill the container while preserving the aspect ratio, the video will be clipped
fill– the video is scaled to fill the container without preserving the aspect ratio, the video will be stretched
none– video is not resized
Here’s a portrait video placed in a landscape video player using the
object-fit:contain CSS. The video is scaled down to fit the container. The portrait aspect ratio of the video is maintained so as to not distort the video resulting in letterboxing on the sides: