How to add a video
Point the <video> element at a video file and add controls so the reader can play it:
<video src="movie.mp4" controls width="640"></video>
controls shows the browser's own player: play and pause, a timeline, volume and fullscreen. Without it the video just sits there. This preview cannot load video files, so the examples on this page feed the element from a small canvas animation drawn by a script; the attributes work exactly as they do with a file:
Because the source is a live stream rather than a file, the timeline cannot be dragged here and there is no fixed length. With a real .mp4, the same player gets a full seek bar.
Video attributes
| Attribute | What it does |
|---|---|
controls | Shows the browser's player controls |
autoplay | Starts playing as soon as it can (only when muted, in practice) |
muted | Starts with the sound off |
loop | Starts again from the beginning when it ends |
playsinline | Plays in the page on iPhone instead of switching to fullscreen |
poster="thumb.jpg" | Image shown before the video plays |
preload | none, metadata (just size and length) or auto (the browser decides how much to download) |
width, height | Size in pixels; setting both reserves space before the video loads |
Autoplay, muted and loop
Browsers block videos that start by themselves with sound. A muted video may autoplay, which is how background videos and short looping clips work:
The four attributes work as a set: autoplay to start, muted so the browser allows it, loop for a clip, and playsinline so iPhones do not open it fullscreen. Autoplaying video with sound is not only blocked, it is also a problem for screen reader users, whose speech gets drowned out. If a clip plays longer than five seconds, give people a way to pause it.
Poster image and preload
poster shows an image before the video plays, like a thumbnail. Here the video has no source at all, so the poster is all there is to see:
With preload="none" the browser downloads nothing until the reader presses play, and the poster keeps the space from looking empty. Use preload="metadata" when you want the duration visible before playing. Without a poster, most browsers show the first frame once they have loaded it.
Formats and fallbacks with source
Instead of src, you can list several files with <source> elements. The browser plays the first one it supports:
<video controls width="640" poster="thumb.jpg">
<source src="movie.webm" type="video/webm">
<source src="movie.mp4" type="video/mp4">
<track kind="captions" src="captions.vtt" srclang="en" label="English">
<p>Your browser cannot play this video. <a href="movie.mp4">Download it</a>.</p>
</video>
MP4 with H.264 video plays in every current browser, so it is the safe choice when you have only one file. The type attribute lets the browser skip formats it cannot play without downloading them. The text at the end shows only in browsers that do not support <video> at all.
Captions with track
<track kind="captions"> loads a WebVTT file, a plain text file of timed lines. The browser adds a captions button to its controls and draws the text over the video:
WEBVTT
00:00:00.000 --> 00:00:03.000
Welcome to the course.
00:00:03.000 --> 00:00:06.500
Today we build our first web page.
Captions help deaf and hard of hearing viewers, people watching with the sound off, and anyone following a video in a second language. Add default to a track to show it without a click. A caption file from another domain needs CORS headers and crossorigin on the video.
Style and control it with JavaScript
A video is a replaced element, like an image: width: 100% with height: auto makes it responsive, and object-fit decides how it fills a box of a different shape. JavaScript gets play(), pause(), currentTime, volume and events such as play, pause and ended:
play() returns a promise that rejects when the browser blocks playback (for example, unmuted autoplay), so production code should catch it. Building your own controls is optional; the built-in controls are keyboard accessible and translated, which custom buttons have to reproduce. For videos hosted on YouTube or Vimeo, you embed their player with an iframe instead.
Common mistakes
- Autoplay without
muted. The browser blocks it. Addmutedandplaysinline. - Forgetting
controls. Unless you build your own, the reader has no way to start the video. - No
widthandheight. The page jumps when the video loads. Set both, then scale with CSS. - Huge files. Compress videos for the web and use
preload="none"or"metadata"for videos lower on the page. - No captions on videos with speech. Add a
<track kind="captions">.
Frequently Asked Questions
How do I add a video in HTML?
Use <video src="movie.mp4" controls width="640"></video>. The controls attribute shows the browser's play button, timeline and volume. Without it, the video shows its first frame (or poster) and cannot be started by the user.
Why is autoplay not working on my video?
Browsers block autoplay with sound. Add muted next to autoplay, and playsinline so iPhones play it in the page instead of fullscreen: <video autoplay muted loop playsinline>.
What video format should I use for the web?
MP4 with H.264 video plays in every current browser, so it is the safe default. You can list a smaller WebM file first with <source> and let the browser pick the first format it supports.
How do I add a thumbnail to a video?
Use the poster attribute with an image URL: <video poster="thumb.jpg" controls>. The image shows until the video starts playing.
How do I add subtitles to an HTML video?
Add a <track kind="captions" src="captions.vtt" srclang="en" label="English"> inside the video. The file uses the WebVTT format, and the browser shows a captions button in its controls.
How do I make a video responsive?
Set width: 100% and height: auto in CSS (or max-width: 100%). The video scales with its container and keeps its aspect ratio.