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="controls" preload="none" src="/static/short.mp4" width="600" height="360"></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="controls" preload="none" controlsList="nofullscreen nodownload" src="/static/short.mp4" width="600" height="360"></video>
In the example above the fullscreen and download buttons have will be turned off or hidden but only if the user is on Chrome.
You can find more examples on how to control the default Chrome video player 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="controls" preload="metadata" src="/static/short.mp4#t=15,20" width="600" height="360"></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 give 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="controls" poster="/static/poster.png" preload="none" src="/static/short.mp4" width="600" height="360"> </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 most of the above players we’ve used
preload="none" to prevent the video from being loaded together with the web page because it might use considerable data even if the user will not play the video. We might thus receive a high data transfer bill from our hosting provider if this blog post gets a lot of views.
Here’s how to allow the browser to preload just the video metadata and show the 1st frame as the poster:
<video controls="controls" preload="metadata" src="/static/short.mp4" width="600" height="360"></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="/static/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="/static/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="/static/short.mp4"></video>
Mute the sound
Sometimes the audio of the video is not necessary. To turn off the audio of the video you can simply add the
muted attribute to the
<video muted controls src="/static/short.mp4"> </video>
Here's the same video embedded but this time with the muted attribute added:
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.
When loading cross origin captions you might have to specify the
crossorigin="anonymous" attribute. The web server serving the .vtt files might also have to provide the
Content-Type:text/vtt header for .vtt files.
<video controls="" preload="metadata" width="422" height="253"> <source src="/static/the-web-is-always-changing.webm" type="video/webm"> <track label="English subtitles" kind="subtitles" srclang="en" src="/static/the-web-is-always-changing.vtt" default=""> </video>
For more info on the topic I recommend Ian Devlin's articles on the subject: Help with WebVTT, Google Chrome supports WebVTT Subtitles, WebVTT and Audio and HTML5 Video Captions – Current Browser Status.
Fitting portrait videos in landscape players using object-fit
object-fit CSS property specifies how the contents of img and video elements should be resized to fit their container.
object-fit can take several values:
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: