What is the <video> tag?

<video> embeds video playback natively in the page, with built-in controls, captions, a poster image and a JavaScript API (play(), pause(), currentTime), with no plugin needed. You can provide several <source> formats and <track> elements for captions and subtitles.

Key attributes

  • controls: show the browser's play/pause, volume and fullscreen UI.
  • poster: an image shown before playback.
  • autoplay: only allowed by browsers when combined with muted; add playsinline for iOS.
  • loop, muted, preload (none, metadata, auto).
  • width/height to reserve space and prevent layout shift.

Formats

List <source> elements in order of preference; the browser plays the first one it supports: AV1/WebM for efficiency, H.264 MP4 as the universal fallback.

Accessibility

  • <track kind="captions" srclang="en" src="captions.vtt" default>: captions are required for meaningful audio (WCAG).
  • Never autoplay with sound, and offer pause controls for anything that moves longer than five seconds.
  • Honor prefers-reduced-motion for decorative background videos.

Performance

  • Use preload="none" or metadata for videos below the fold.
  • Replace animated GIFs with short muted looping videos; they are often 5 to 10 times smaller.
  • For long or adaptive streaming, use HLS/DASH (often through a player library or a video platform).

Interview tip

Mention the autoplay policy: muted autoplay works, autoplay with sound is blocked until the user interacts.

Code example

html
1<style>
2 body { font-family: system-ui; }
3 video { width: 100%; max-width: 560px; height: auto; border-radius: 8px; background: #000; }
4</style>
5<video id="v" controls muted playsinline preload="metadata" width="640" height="360"
6 poster="https://picsum.photos/id/1011/640/360">
7 <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm" type="video/webm">
8 <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" type="video/mp4">
9 <track kind="captions" srclang="en" label="English" src="captions-en.vtt" default>
10 Your browser does not support HTML video. <a href="flower.mp4">Download the video</a>.
11</video>
12<p>
13 <button id="toggle">Play</button>
14 <output id="time">0.0s</output>
15</p>
16<script>
17 const v = document.getElementById("v");
18 const btn = document.getElementById("toggle");
19 btn.onclick = () => (v.paused ? v.play() : v.pause());
20 v.onplay = () => (btn.textContent = "Pause");
21 v.onpause = () => (btn.textContent = "Play");
22 v.ontimeupdate = () => (document.getElementById("time").textContent = v.currentTime.toFixed(1) + "s");
23</script>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.

Open in study app