Twenty years ago, putting a video on a web page meant installing a browser plugin like Adobe Flash. If the visitor didn't have the plugin, they saw a grey box and an error. Flash is long dead, and today the browser itself knows how to play sound and video.
HTML gives us two elements for this: <audio> and <video>. In this chapter you'll learn how to use them, how to support every browser with multiple file formats, how to add captions, and which attributes (like autoplay) behave differently from what beginners expect.
The
<video>and<audio>elements embed media that the browser plays natively, without any plugin. They are called media elements and share almost all of their attributes.
Your First Video
The smallest useful video needs only two things: the file and a way for the user to play it.
<video src="lesson.mp4" controls></video>srcis the URL of the video file, just likesrcon an<img>(from the Images chapter).controlstells the browser to show its built-in play/pause button, timeline, volume and full-screen buttons.
Without controls, the browser shows just the first frame, and the user has no way to start the video (unless you write JavaScript). This is the most common beginner mistake with media elements.
controls,autoplay,mutedandloopare boolean attributes (see the Elements & Attributes chapter). Their presence means "on". Writingmuted="false"still mutes the video, because the attribute is present. To turn it off, remove the attribute completely.
A Complete, Real-World Example
Here is a full page with a video and an audio player, written the way you would write it on a real site:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Audio and Video Demo</title>
</head>
<body>
<h1>Lesson 1: Arrays</h1>
<video controls width="640" height="360" poster="poster.jpg" preload="metadata">
<source src="lesson.webm" type="video/webm">
<source src="lesson.mp4" type="video/mp4">
<track src="lesson-en.vtt" kind="captions" srclang="en" label="English" default>
<p>Your browser cannot play this video. <a href="lesson.mp4">Download the MP4</a> instead.</p>
</video>
<h2>Listen to the podcast</h2>
<audio controls preload="none">
<source src="song.ogg" type="audio/ogg">
<source src="song.mp3" type="audio/mpeg">
<a href="song.mp3">Download the MP3</a>
</audio>
</body>
</html>What you see in Chrome (we tested with a 6-second clip and a plain blue poster image; your own files will show their own picture and length):
Lesson 1: Arrays
┌──────────────────────────────────────────────┐
│ │
│ (poster image: a plain blue frame) │
│ │
│ ▶ 0:00 / 0:06 🔊 ⛶ ⋮ │
└──────────────────────────────────────────────┘
Listen to the podcast
( ▶ 0:00 / 0:00 ──────── 🔊 ⋮ )Notice two details:
The video shows its length (0:06 for our test clip) before you press play. Because of
preload="metadata", the browser downloaded just enough of the file to learn its duration and size.The audio player shows 0:00 / 0:00. With
preload="none"the browser hasn't touched the file yet, so it doesn't know the length. It will download it when the user presses play.
Let's go through every piece of this example.
Video Attributes
| Attribute | What it does |
|---|---|
src | URL of the media file (or use <source> children instead) |
controls | Show the browser's built-in controls |
width, height | Display size in CSS pixels (plain numbers, no %). Setting both reserves space so the page doesn't jump while loading |
poster | Image shown before playback starts. Without it, the browser shows the first frame once it's available |
preload | A hint: none, metadata or auto |
autoplay | Try to start playing as soon as possible (often blocked, see below) |
muted | Start with the sound off |
loop | Start again from the beginning when it ends |
playsinline | Play inside the page instead of jumping to full screen on phones (mainly matters on iPhone Safari) |
crossorigin | Fetch the file using CORS (needed for captions or canvas use from another domain) |
<audio> supports the same attributes except width, height, poster and playsinline, because audio has no picture.
The preload Attribute
| Value | Meaning | Good for |
|---|---|---|
none | Don't download anything until the user presses play | Pages with many players, users on mobile data |
metadata | Download only duration, size and the first frame | Most cases; the spec suggests this as a sensible default |
auto | Download as much as you like, the user will probably play it | The main video of a page |
It is only a hint. The browser may ignore it (for example, to save data on a phone), and the default when you leave it out differs between browsers. If autoplay is present, the browser obviously has to download the file, so autoplay wins over preload.
Multiple Formats with <source>
A video file is really two things: a container (the file format, like MP4 or WebM) and codecs (how the picture and sound inside are compressed, like H.264 or AV1). Not every browser supports every combination.
Instead of a single src, you can list several <source> elements. The browser goes through them from top to bottom and plays the first one it can handle:
<video controls width="640" height="360">
<source src="lesson.webm" type="video/webm">
<source src="lesson.mp4" type="video/mp4">
</video>Browser reads source 1: type="video/webm"
├─ "I can play WebM" → download lesson.webm, stop looking
└─ "I can't play WebM" → skip it WITHOUT downloading
Browser reads source 2: type="video/mp4" → ...The type attribute (a MIME type) is important: it lets the browser skip formats it can't play without downloading them first. Without type, the browser may have to fetch part of each file to find out.
Put the format you prefer first (usually the smaller one), and a widely supported format last as the safety net.
Which Formats Should You Use?
| Type | Format | MIME type | Notes |
|---|---|---|---|
| Video | MP4 (H.264 video + AAC audio) | video/mp4 | Plays in every modern browser. The safest choice |
| Video | WebM (VP9 or AV1 video + Opus audio) | video/webm | Smaller files at the same quality. AV1 decoding may depend on the device's hardware, so keep an MP4 fallback |
| Audio | MP3 | audio/mpeg | Plays everywhere |
| Audio | AAC in MP4 (.m4a) | audio/mp4 | Plays everywhere, good quality |
| Audio | Ogg or WebM with Opus | audio/ogg, audio/webm | Excellent quality at small sizes |
| Audio | WAV | audio/wav | Uncompressed, so files are huge. Avoid on the web |
In practice, one MP4 file (H.264 + AAC) works for everyone. Adding a WebM version is an optimisation that saves bandwidth.
Notice the MIME type for MP3 is
audio/mpeg, notaudio/mp3. This trips up a lot of people.
Fallback Content
Anything inside <video> or <audio> that isn't a <source> or <track> is fallback content:
<video controls src="lesson.mp4">
<p>Your browser cannot play this video. <a href="lesson.mp4">Download the MP4</a> instead.</p>
</video>Be precise about when it appears: fallback content is shown only by browsers that don't understand the <video> element at all. If a modern browser understands <video> but can't play any of your files, it does not show the fallback. It shows a broken player instead. That's why choosing a widely supported format matters more than writing fallback text.
Autoplay: Why It Often Doesn't Work
Imagine opening a news website and a video starts shouting at you. Users hate this, so all major browsers have an autoplay policy:
Media with sound is usually not allowed to start by itself. The browser blocks it until the user interacts with the page (clicks, taps, presses a key), though some browsers relax this for sites the user visits often.
Media that is muted (or has no audio track) is generally allowed to autoplay.
So a background video for a website header is written like this:
<video autoplay muted loop playsinline width="480" height="270">
<source src="lesson.mp4" type="video/mp4">
</video>| Attribute | Why it's there |
|---|---|
autoplay | Start immediately |
muted | Without it, the browser will most likely block autoplay |
loop | Keep playing forever |
playsinline | On iPhones, stay inside the page instead of opening full screen |
Autoplay is valid HTML, but use it with care. Moving content distracts some users, and sound that starts by itself is a real accessibility problem for screen-reader users (it talks over their screen reader). Some linters, such as html-validate with its recommended rules, flag
autoplayfor this reason. If you autoplay, keep it muted and give users a way to pause.
Captions and Subtitles with <track>
Deaf and hard-of-hearing users, people watching on a noisy train, and people watching with the sound off all need text. The <track> element attaches a timed text file to a video:
<video controls width="640" height="360">
<source src="lesson.mp4" type="video/mp4">
<track src="lesson-en.vtt" kind="captions" srclang="en" label="English" default>
<track src="lesson-hi.vtt" kind="subtitles" srclang="hi" label="हिन्दी">
</video>The text file uses the WebVTT format (.vtt). It's plain text, so you can write it in any editor:
WEBVTT
00:00:00.000 --> 00:00:03.000
Welcome to the lesson!
00:00:03.000 --> 00:00:06.000
[soft beep]Rules of WebVTT:
The file must start with the word
WEBVTT.Each cue has a time range
start --> end(hours:minutes:seconds.milliseconds) followed by the text.Cues are separated by a blank line.
When the video above plays, Chrome draws "Welcome to the lesson!" in white text on a dark strip at the bottom of the video for the first three seconds, then "[soft beep]". Because the track has default, captions are on from the start; otherwise the user turns them on from the player's menu.
track Attributes
| Attribute | Meaning |
|---|---|
src | The .vtt file (required) |
kind | subtitles (the default), captions, descriptions, chapters or metadata |
srclang | Language of the text, like en or hi. Required when kind="subtitles" |
label | The name shown in the player's caption menu |
default | Turn this track on by default. A media element may have at most one default track among its subtitles/captions tracks (and at most one each among descriptions and chapters) |
Subtitles vs Captions
| Subtitles | Captions |
|---|---|
| For people who can hear but don't understand the language | For people who can't hear the audio |
| Only the spoken words (often translated) | Spoken words plus sounds: [door slams], [music playing], who is speaking |
Two practical gotchas: the
.vttfile must come from the same origin as the page (or the video needs acrossoriginattribute and the server must allow it), and the server should send it with the MIME typetext/vtt. Tracks also don't load when you open the page directly as afile://URL in Chrome, so test with a local server (for example VS Code's Live Server).
Lazy Loading Media (New in 2026)
You may know loading="lazy" from the Images chapter. In 2026 the same attribute was added to the HTML standard for <video> and <audio>, so media far down the page isn't fetched until the user scrolls near it. Browser support is still rolling out; browsers that don't support it simply ignore the attribute and load normally, so it's safe to add. If you use it, always set width and height: a lazy video with no size is drawn as a default 300 × 150 box and then jumps to its real size once the file loads, shifting the page. Also note that an <audio> element only lazy-loads if it has controls, because an audio element without controls isn't displayed at all.
What About YouTube Videos?
<video> plays a media file (.mp4, .webm). A YouTube link like https://www.youtube.com/watch?v=... is a web page, not a video file, so this does not work:
<!-- ❌ Wrong: a YouTube page is not a video file -->
<video controls src="https://www.youtube.com/watch?v=abc123"></video>YouTube, Vimeo and similar sites give you an <iframe> embed code instead. We'll cover that in the next chapter, Iframes & Embedding Content.
Also remember that hosting large videos yourself costs bandwidth. A 100 MB video watched by 1,000 students is 100 GB of data from your server. For long videos, a video platform or a CDN with adaptive streaming is usually the better choice; <video> is perfect for short clips, demos and background loops.
Controlling Media with JavaScript (Preview)
Media elements also have a JavaScript API with methods like play() and pause() and properties like currentTime and volume. That's how custom players are built. You don't need it for normal pages, because controls gives you a complete, accessible player for free.
Common Mistakes
Forgetting
controls. The video appears but nobody can start it.Expecting
autoplayto work with sound. Browsers block it. Addmutedor let the user press play.Writing
muted="false"orautoplay="false". Boolean attributes are on whenever present. Remove the attribute instead.Using
type="audio/mp3". The correct MIME type isaudio/mpeg.Putting the least supported format last with no fallback. End the
<source>list with MP4 (video) or MP3 (audio).Expecting fallback text when the format isn't supported. Fallback content only appears in browsers that don't know
<video>at all.Putting a YouTube page URL in
src. Use the platform's iframe embed.Skipping captions. Captions help deaf users, people without headphones, and non-native speakers.
Interview Questions
Q1. A video has autoplay but doesn't start when the page loads. What's the most likely reason and how do you fix it?
The browser's autoplay policy is blocking it because the video has sound. Add muted (and playsinline for iPhones) so it can autoplay silently, or remove autoplay and let the user start it with controls.
Q2. Why do we give each <source> a type attribute?
So the browser can tell from the MIME type alone whether it can play that format, and skip unsupported files without downloading them. The browser plays the first source it can handle, in document order.
Q3. What is the difference between kind="subtitles" and kind="captions"?
Subtitles transcribe or translate the dialogue for viewers who can hear but don't understand the language. Captions are for viewers who can't hear the audio, so they include the dialogue plus important sounds and speaker information.
Q4. What does preload="metadata" do, and is the browser forced to obey it?
It asks the browser to download only the metadata (duration, dimensions, maybe the first frame) and not the whole file. It is only a hint; the browser may download more or less, and autoplay overrides it.
Q5. Which attributes of <video> are not available on <audio>?
width, height, poster and playsinline, since they only make sense for something with a picture.
Conclusion
<video> and <audio> let the browser play media natively. Add controls, offer a widely supported format, and add captions.
Use
<source>with atypeto offer several formats; the first playable one wins.MP4 (H.264 + AAC) and MP3 play everywhere; WebM/Opus are smaller optimisations.
preloadis a hint:none,metadataorauto.Autoplay with sound is blocked; muted autoplay generally works.
<track>+ a WebVTT file adds captions and subtitles.Set
widthandheightto avoid layout jumps, and useposterfor a nice preview image.
Next, in Iframes & Embedding Content, you'll learn how to embed YouTube videos, maps and even whole web pages inside your page, and how to do it safely.