Back to HTML

The HTML video Element Explained

Coding Tutorials Team 6 July 2026

Video used to require a plugin. It now works natively in every browser through a single element, and most of the difficulty has moved from playback to encoding and file size.

The basic form

<video src="/media/demo.mp4" controls width="800" height="450"> Your browser does not support the video element. </video>

Unlike an image, this is not a void element. The content between the tags is fallback text shown only when the element is unsupported, which in practice is almost never, so it is a courtesy rather than a real safety net.

The attributes that matter

The controls attribute displays the browser's playback interface. Without it, there is no way for a user to start, pause or seek unless you build your own controls with JavaScript. Omitting it accidentally produces a video that appears frozen on its first frame.

The poster attribute specifies an image shown before playback begins, which is otherwise a blank rectangle or an arbitrary first frame. Choosing a deliberate poster image makes a considerable difference to how finished a page looks.

The preload attribute hints at how much to fetch in advance. A value of metadata retrieves only the duration and dimensions, which is the sensible default for anything the user has not asked to play. A value of none defers everything, and auto invites the browser to download the whole file, which is rarely justified.

The muted, loop and playsinline attributes are boolean switches, and playsinline matters specifically because some mobile browsers otherwise take video fullscreen the moment it starts.

Autoplay requires muted

Autoplay is the attribute that appears not to work. Browsers block automatic playback with sound, because unexpected audio is disruptive and was abused heavily enough that the restriction is now universal.

<video src="/media/loop.mp4" autoplay muted loop playsinline></video>

Muted autoplay is permitted. That combination, with looping and inline playback, is how decorative background video is done, and it is the correct replacement for an animated GIF, typically at a fraction of the file size.

Some browsers additionally allow unmuted autoplay on sites where the user has previously engaged with media, which means behaviour varies between visitors. Never rely on sound playing automatically.

Multiple formats

Source elements let you offer several encodings, and the browser picks the first it supports.

<video controls poster="/media/poster.jpg" width="800" height="450"> <source src="/media/demo.webm" type="video/webm"> <source src="/media/demo.mp4" type="video/mp4"> </video>

Specifying the type attribute matters, because it lets the browser choose without downloading part of each file to find out. Order from most efficient to most compatible.

In practice MP4 with H.264 video and AAC audio plays essentially everywhere, so a single well encoded MP4 is a reasonable minimum. WebM offers better compression where it is supported.

File size is the real problem

Video is heavy, and a poorly encoded file can be many times larger than it needs to be. Three things dominate the result: resolution, bitrate and duration.

Serving a large video to a phone wastes bandwidth that the viewer may be paying for. There is no equivalent of srcset for video, so adaptive delivery requires streaming formats that switch quality automatically, which is why hosting platforms exist and why self hosting is only sensible for short, small clips.

Accessibility is not optional

Video needs captions for anyone who cannot hear the audio, and captions also serve people in noisy environments or watching without sound, which is a large share of viewers. The track element supplies them and is covered separately.

Where important information is conveyed visually without narration, an audio description or a transcript is needed as well. A transcript has the additional benefit of being text on the page, which is indexable, searchable and quotable in a way the video itself is not.

Scripting playback

The element exposes a full media interface to JavaScript, covering play and pause, the current position, volume, playback rate and a set of events fired as loading and playback progress. This is what custom players are built on.

One constraint governs all of it: playback started by script is subject to the same autoplay rules as the attribute, so a play call that was not triggered by a user gesture will be rejected. The call returns a promise, and handling its rejection is how you avoid an unexplained silent failure.

Styling

The element responds to ordinary CSS for sizing, and the object fit property applies exactly as it does to images, so a video can be cropped to fill a container without distortion. The browser controls themselves are not stylable in any consistent way, which is the usual reason projects end up building custom controls.