Explain it simply
Explain it simply
The <video> element puts a real video player into your page without any plugin or JavaScript. You point it at one or more video files, and the browser draws the picture, the play button, the timeline and the volume slider for you.
Think of a TV that comes with its remote control. The controls attribute is the remote: leave it out and the TV still exists, but the visitor has no button to start, pause or mute it. A <track> file is the caption card that lets people follow the speech when they cannot hear it.
Problem → Why → Solution
The problem: a hero video that was supposed to start by itself
A designer wants a moving background on the homepage, so the developer writes the code below: one MP4, autoplay and loop, nothing else. On the developer's own laptop it seemed to work once, after they had been clicking around the site for a while.
On a new visitor's first visit, Chrome, Safari and Firefox refuse to start it, because it would start with sound. The visitor sees a frozen frame or an empty box, and with no controls there is no button to start it. In the cases where a browser does allow it, the clip loops forever with sound and offers no way to stop it. And anyone who cannot hear the narration gets nothing from it, because there are no captions.
<!-- Blocked on most first visits, no way to pause, no captions -->
<video src="promo.mp4" autoplay loop></video>Why browsers push back on this code
Unexpected sound is one of the most disliked things on the web, so browsers block autoplay with sound by default; muted autoplay is allowed. Even when a clip may play, WCAG asks for a way to pause moving content that lasts longer than five seconds, and for captions on prerecorded speech. The fix is controls, captions and a deliberate autoplay decision, not a different file format.
Core concept: what happens inside <video>
A <video> element is a container with three kinds of children: <source> elements that offer the same clip in different formats, <track> elements that add timed text, and fallback content for very old browsers. Attributes on the <video> tag itself decide how the player behaves before and during playback. The browser works through four stages.
1. It picks a source
It walks the <source> children from top to bottom and skips any whose type it knows it cannot play. It starts loading the first candidate, and if that file fails to load it moves on to the next one. Put the smaller modern format (WebM) first and the almost universally supported MP4 (H.264) second.
2. It prepares the first view
Until playback starts, poster shows the image you chose. width and height reserve the box, so the page does not jump when the video loads. preload is only a hint: none, metadata (duration and size only) or auto, and the browser may ignore it; when autoplay is present, autoplay overrides it.
3. It attaches the text tracks
Each <track> points to a WebVTT file, and kind says what the text is: captions (speech plus important sounds, for viewers who cannot hear), subtitles (a translation of the dialogue), descriptions or chapters. srclang and label name the language in the captions menu, and default switches one track on from the start.
4. The user stays in charge
controls gives you the built-in player: play, seek, volume, fullscreen and a captions menu, all usable from the keyboard. autoplay only works reliably together with muted, and iPhones also want playsinline so the clip stays inside the page instead of jumping to fullscreen.
| Attribute | What it does |
|---|---|
| controls | Built-in player UI that works with the keyboard |
| poster | Image shown before playback starts |
| preload | Hint: none / metadata / auto |
| muted | Starts silent; needed for autoplay |
| playsinline | iPhone plays inside the page, not fullscreen |
Fallback content is not an error message
The text inside <video> appears only in browsers that do not understand <video> at all. If every source fails to load, modern browsers show an empty player, not your fallback paragraph, so break a file name on purpose and look at what visitors would really get.
- Pick the first playable <source>
- Show the poster in the reserved box
- Load the default caption track
- Wait for the user (or muted autoplay)
When a stage fails (a missing file, a track that will not load) there is usually no visible message, so check the Console and Network tabs in DevTools.
General pattern
<video controls width="W" height="H" poster="cover.jpg">
<source src="clip.webm" type="video/webm">
<source src="clip.mp4" type="video/mp4">
<track kind="captions" src="clip.vtt" srclang="xx" label="Language">
Fallback text with a download link
</video>List the sources in order of preference and add at least one caption track. width/height reserve the box, and controls hand control to the user.
A captioned video for a course page
This example uses controls, a poster and a fallback message, and adds what a real course page needs: a second format, a caption track and a transcript link. The four file names are placeholders: save your own clip, poster image and .vtt file next to the HTML page under those names.
<!-- Placeholder files: put lesson-intro.webm, lesson-intro.mp4,
intro-poster.jpg and intro-captions.en.vtt next to this page. -->
<figure>
<video controls width="640" height="360" preload="metadata"
poster="intro-poster.jpg">
<source src="lesson-intro.webm" type="video/webm">
<source src="lesson-intro.mp4" type="video/mp4">
<track kind="captions" src="intro-captions.en.vtt"
srclang="en" label="English" default>
<p>Your browser cannot play this video.
<a href="lesson-intro.mp4">Download the MP4</a> instead.</p>
</video>
<figcaption>
Lesson intro (2 min).
<a href="lesson-intro-transcript.html">Read the transcript</a>
</figcaption>
</figure>Code breakdown
- Player box and behaviour
<video controls width="640" height="360" preload="metadata"controls switches the player UI on. width/height reserve a 640×360 area so the text below does not move when the video loads, and preload="metadata" hints that only the duration and size should be fetched up front.
- Preferred format first
<source src="lesson-intro.webm" type="video/webm">A browser that can play WebM takes this file. Thanks to type, a browser that cannot play it skips it without downloading anything, and if the file fails to load the MP4 below is tried next.
- The caption track
<track kind="captions" src="intro-captions.en.vtt"kind="captions" says the file contains the speech plus [sound] notes. It must be a WebVTT file from the same origin as the page, or one that is allowed through CORS.
- Language, menu name, on by default
srclang="en" label="English" default>srclang is the track's language and label is the name shown in the captions menu. default turns the captions on without the user having to find the setting.
- Fallback for very old browsers
<p>Your browser cannot play this video.Only browsers that do not know <video> render this paragraph and its download link. A broken source file does not make it appear.
- Transcript outside the player
<a href="lesson-intro-transcript.html">Read the transcript</a>A transcript helps people who cannot hear and people who cannot see the picture, and search engines can read it. It sits in the figcaption rather than inside the player, so it is always visible.
A 640×360 player with play, timeline, volume, fullscreen and a ⋮ menu (with a captions option), and the line "Lesson intro (2 min). Read the transcript" below it. With your own files next to the page, intro-poster.jpg shows until you press play and English captions appear on the picture. Without the files, Chrome shows an empty box with greyed-out controls; the fallback paragraph is not displayed.Writing the caption file (WebVTT)
WebVTT is a plain text file. The first line must be WEBVTT, and each cue has a "start --> end" timing line followed by the text to show. A blank line separates cues. Because these are captions, important sounds such as [keyboard clicking] are written down too.
WEBVTT
00:00:00.500 --> 00:00:03.000
Welcome to the HTML course.
00:00:03.200 --> 00:00:06.500
[keyboard clicking]
Let's open the code editor.Captions often fail when you double-click the HTML file
Chrome treats each file:// page as a unique origin and blocks the .vtt file with an "Unsafe attempt to load URL" console error. Serve the folder with a local server, such as VS Code Live Server, when you test captions.
Autoplay done responsibly
If a hero section really needs a silent loop, combine muted, playsinline and controls that let people stop it. muted is what makes browsers allow the autoplay, playsinline keeps it inside the page on iPhones, and controls satisfy the WCAG rule that motion lasting more than five seconds can be paused. When JavaScript calls video.play(), it returns a promise that rejects with NotAllowedError if the browser blocks playback.
<video autoplay muted loop playsinline controls
width="1280" height="720" poster="hero-still.jpg">
<source src="hero-loop.webm" type="video/webm">
<source src="hero-loop.mp4" type="video/mp4">
</video>In real projects
In real projects
Course platforms, product demo pages and documentation sites host short clips themselves and show them with a plain <video>. Their checklist is always similar: compress the clip for the web, export WebM and MP4, make the poster from a good frame of the video, and publish the caption file and transcript together with the video.
Long videos and live streams usually use adaptive streaming (HLS or DASH), which switches quality to match the connection speed. Safari can play HLS natively, while other browsers need a JavaScript player library. Videos hosted on YouTube or Vimeo are embedded with an <iframe>, which you will meet in the iFrames lesson. Whichever route you take, the rules stay the same: captions, keyboard control and no surprise autoplay.
Common mistakes
Common mistakes
Expecting the fallback text to cover broken files
If movie.mp4 is misspelled, visitors do not see "Your browser does not support..."; they see an empty player that does nothing. Check the file path in the Network tab, and keep a download link inside the fallback anyway.
Writing autoplay without muted
Your own browser may allow autoplay with sound because you visit the site often, but a new visitor's browser blocks it. If you need autoplay, add muted and playsinline and give people a way to pause.
Serving the track file where it cannot load
Loading the caption file from another domain such as a CDN without the crossorigin attribute and a CORS header, testing from file://, or forgetting the WEBVTT first line all make captions vanish without a visible error. Check the Console.
Leaving out width and height
The player resizes when the video's metadata arrives, and the text below jumps. Give width/height with the right ratio, then make it responsive in CSS with max-width: 100%; height: auto;.
Mini challenge
A product demo with captions in two languages
Build the page for a 90-second app demo. The video needs two formats, English captions and Myanmar subtitles, and it must not use the visitor's data until they press play.
- <video> has controls, 960×540 width/height, the poster demo-poster.jpg and preload="none"
- Add demo.webm first and demo.mp4 second, each with its type
- Add demo.en.vtt as English captions (default) and demo.my.vtt as subtitles with srclang="my"
- Add a fallback paragraph with a download link, and a transcript link below the video
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>App demo</title>
</head>
<body>
<h1>See the app in 90 seconds</h1>
<!-- TODO: <video> with controls, 960x540, poster, preload="none" -->
<!-- TODO: demo.webm source, then demo.mp4 source (with type) -->
<!-- TODO: English captions track, on by default -->
<!-- TODO: Myanmar subtitles track (srclang="my") -->
<!-- TODO: fallback paragraph with a download link -->
<!-- TODO: transcript link after the video -->
</body>
</html>Expected: Below the heading, a 960×540 player whose ⋮ menu offers two caption choices, English and မြန်မာ (when the files exist and the page is served by a local server). The transcript link appears below the player; the fallback paragraph stays hidden in modern browsers.
Show hint
The two tracks have different kinds: the Myanmar one is a translation, so it is subtitles, and it can have a menu name like label="မြန်မာ". Put default on only one track.
Show solution
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>App demo</title>
</head>
<body>
<h1>See the app in 90 seconds</h1>
<video controls width="960" height="540" poster="demo-poster.jpg"
preload="none">
<source src="demo.webm" type="video/webm">
<source src="demo.mp4" type="video/mp4">
<track kind="captions" src="demo.en.vtt" srclang="en"
label="English" default>
<track kind="subtitles" src="demo.my.vtt" srclang="my"
label="မြန်မာ">
<p>This browser cannot play the demo.
<a href="demo.mp4" download>Download demo.mp4</a></p>
</video>
<p>
Prefer reading?
<a href="demo-transcript.html">Open the full transcript</a>
</p>
</body>
</html>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- The browser takes the first playable <source> and tries the next one if loading fails
- Fallback text is for browsers without <video>, not for broken files
- controls, width/height, poster and preload shape the experience and the performance
- Give spoken content a kind="captions" WebVTT track and a transcript
- If you need autoplay, combine muted, playsinline and controls that can pause