HTML Video
Before HTML5, playing a video on a web page needed a browser plug-in such as Flash. Today the <video> element does it natively: the browser downloads the file, decodes it and shows built-in playback controls — no extra software required.
The basic <video> element
<video width="640" height="360" controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.webm" type="video/webm">
Sorry, your browser does not support embedded video.
</video>What each part does:
controlsshows play/pause, the timeline, volume and fullscreen buttons.widthandheightreserve space so the page does not jump around while the video loads.- Each
<source>offers the same video in a different format. The browser picks the first one it can play. - The
typeattribute lets the browser skip formats it does not support without downloading them. - Any text inside
<video>(but outside<source>) is fallback content, shown only by browsers that do not understand<video>at all.
Note: The playgrounds on ExcelTAD run offline in a sandbox, so they cannot load real video files. The examples below use an inline SVG poster image and show what happens when the source is missing. In your own projects, point
srcat real.mp4or.webmfiles.
Video attributes
| Attribute | Effect |
|---|---|
controls |
Show the browser's playback controls |
autoplay |
Start playing as soon as possible (most browsers only allow this when muted is also set) |
muted |
Start with the sound off |
loop |
Start again from the beginning when finished |
poster |
Image shown before the video starts playing |
preload |
Hint: none, metadata or auto |
playsinline |
On phones, play inside the page instead of going fullscreen |
width / height |
Display size in CSS pixels |
A poster image
The poster attribute shows a picture until the user presses play. Here it is an SVG embedded as a data URI, so it works without any files.
<!DOCTYPE html>
<html>
<body>
<video controls width="320" height="180"
poster="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='320' height='180'%3E%3Crect width='320' height='180' fill='%23263238'/%3E%3Cpolygon points='140,60 140,120 190,90' fill='white'/%3E%3Ctext x='160' y='160' fill='%23cfd8dc' font-family='sans-serif' font-size='14' text-anchor='middle'%3EMy holiday video%3C/text%3E%3C/svg%3E">
<source src="holiday.mp4" type="video/mp4">
Your browser does not support the video element.
</video>
<p>The poster appears, but there is no <code>holiday.mp4</code> here, so the video itself cannot play.</p>
</body>
</html>Autoplay rules
Browsers protect users from noisy pages. A video that autoplays with sound is normally blocked. The reliable recipe for a background-style video is:
<video autoplay muted loop playsinline>
<source src="background.mp4" type="video/mp4">
</video>Tip: Never autoplay video with sound, and always give users a way to pause moving content — it is distracting and can be a real problem for people with attention or vestibular disorders.
Controlling video with JavaScript
The <video> element has a JavaScript API: methods such as play() and pause(), properties such as currentTime, duration, paused and volume, and events such as play, pause, ended and error. You can build your own buttons with it. In this sandbox there is no file to load, so the error event fires — a good chance to see how to handle a failure gracefully.
<!DOCTYPE html>
<html>
<body>
<video id="vid" width="240" height="135" style="background:#000">
<source src="clip-that-does-not-exist.mp4" type="video/mp4">
</video>
<div>
<button id="play">Play</button>
<button id="pause">Pause</button>
</div>
<p id="status">Status: waiting…</p>
<script>
const vid = document.getElementById('vid');
const status = document.getElementById('status');
document.getElementById('play').onclick = function () {
vid.play().catch(function (err) {
status.textContent = 'Status: could not play (' + err.name + ')';
});
};
document.getElementById('pause').onclick = function () { vid.pause(); };
vid.addEventListener('play', function () { status.textContent = 'Status: playing'; });
vid.addEventListener('pause', function () { status.textContent = 'Status: paused'; });
// Errors on <source> elements fire on the source, not the video
vid.querySelector('source').addEventListener('error', function () {
status.textContent = 'Status: the video file could not be loaded.';
});
</script>
</body>
</html>play() returns a Promise, which rejects if playback is not allowed or fails — always add a .catch().
Supported formats
| Format | MIME type | Notes |
|---|---|---|
| MP4 (H.264) | video/mp4 |
Plays almost everywhere — the safe default |
| WebM (VP9/AV1) | video/webm |
Open format, smaller files, widely supported |
| Ogg (Theora) | video/ogg |
Legacy; rarely needed today |
Embedding videos from YouTube and similar sites
Video-hosting sites do not hand you a raw file; they give you an <iframe> embed code instead. That is covered in the Iframes lesson. Use <video> when you host the file yourself.
Responsive video
A fixed width can overflow a small phone screen. A little CSS makes the video shrink with its container while keeping its shape:
<!DOCTYPE html>
<html>
<head>
<style>
video { width: 100%; max-width: 480px; height: auto; aspect-ratio: 16 / 9; background: #37474f; }
</style>
</head>
<body>
<video controls>
<source src="demo.mp4" type="video/mp4">
</video>
<p>Resize the panel: the (empty) player keeps its 16:9 shape.</p>
</body>
</html>Test yourself
Why might you list several <source> elements inside one <video>?
- To play them one after another
- To offer the same video in different formats so the browser picks one it supports
- To show several videos side by side
- To add subtitles
Which combination lets a video autoplay in modern browsers?
autoplayaloneautoplay controlsautoplay mutedloop poster
What does the poster attribute set?
- The video title
- An image shown before playback starts
- The subtitle file
- The download link