HTML Audio and Video
A web page that only shows text and pictures is like a book. With audio and video, the page can talk, sing, teach, and show how things move. A cooking page can play the sizzle, a language lesson can play the pronunciation, and a tutorial can show every click.
For many years, this needed a plugin. Today, it is just HTML: two elements, <audio> and <video>, play media directly in the browser, with built-in controls, keyboard support, and no extra software.
This section shows you how to add media, give it more than one format, add captions for people who cannot hear it, and make it work on every screen size.
What You Will Learn in This Sectionβ
- Adding sound and video with
<audio>and<video> - Offering several file formats with
<source> - Using the built-in controls, and building your own
- Adding captions and subtitles with
<track>and WebVTT files - Making media accessible to everyone
- Making video responsive, so it fits any screen
The Big Ideaβ
- HTML Code
- What Each Part Does
<video controls width="480" poster="cover.jpg">
<source src="lesson.webm" type="video/webm">
<source src="lesson.mp4" type="video/mp4">
<track kind="captions" src="lesson.en.vtt" srclang="en" label="English">
<p>
Your browser cannot play this video.
<a href="lesson.mp4">Download it instead</a>.
</p>
</video>
| Part | Its job |
|---|---|
controls | Shows play, pause, volume, and a progress bar |
poster | A picture shown before the video plays |
source (twice) | Two formats: the browser uses the first one it can play |
track | A captions file for people who cannot hear the sound |
| The text inside | A fallback for browsers that cannot play video at all |
The browser reads the children from top to bottom, picks the first source it can play, loads the captions, and shows the controls. You do not write any JavaScript.
Try It: What Can Your Browser Play?β
Not every browser plays every format. The canPlayType() method lets the page ask. Here it is, asking about common formats:
- HTML Code
- Browser Output
const video = document.createElement("video");
console.log(video.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"'));
console.log(video.canPlayType('video/webm; codecs="vp9, opus"'));
// Each answer is "probably", "maybe", or "" (empty: not supported)
Checkingβ¦
The answer is the browser's own guess. Only "probably" means it recognised the exact codecs, and even then, a broken file can still fail.
You will see this question again in the Source lesson. In practice, you rarely call canPlayType() yourself, because the <source> element lets the browser choose for you.
Which Element Do I Need?β
- HTML Code
- Browser Output
<!-- Sound only -->
<audio controls src="episode.mp3"></audio>
<!-- Sound and picture -->
<video controls src="tutorial.mp4"></video>
<!-- Same media, several formats -->
<video controls>
<source src="clip.webm" type="video/webm">
<source src="clip.mp4" type="video/mp4">
</video>
<!-- Add captions -->
<video controls src="clip.mp4">
<track kind="captions" src="clip.en.vtt" srclang="en" label="English">
</video>
Use the <audio> element. Learn it in the Audio lesson.
<audio controls src="episode.mp3"></audio>What Is in This Sectionβ
ποΈ Audio
Learn the HTML audio element: adding sound with src, controls, loop, muted, preload and autoplay, choosing MP3, AAC, Ogg or WAV, fallback content, the autoplay rules, controlling playback with JavaScript, transcripts, and accessibility, with a live audio player you can try.
ποΈ Video
Learn the HTML video element: embedding video with src, controls, width, height, poster, preload, muted, loop, playsinline and autoplay, choosing MP4 or WebM, avoiding layout shift, background videos done safely, compressing files, and accessibility, with a live video player you can try.
ποΈ Source
Learn the HTML source element: offering several formats for audio and video with src and type, how the browser picks the first source it can play, codecs in the type attribute, the media attribute, fallback content, and common mistakes, with a live demo that shows the browser choosing.
ποΈ Media Controls
Learn HTML media controls: the built-in controls attribute, what browsers add, how to build your own accessible player with buttons and sliders using the media API (play, pause, currentTime, volume, playbackRate and events), keyboard support, and common mistakes, with a live custom player.
ποΈ Track
Learn the HTML track element: adding captions, subtitles, descriptions, chapters and metadata to audio and video with kind, src, srclang, label and default, how WebVTT files work, the TextTrack JavaScript API and cuechange event, cross-origin rules, and common mistakes, with live demos.
ποΈ Captions
Learn how to add and write good captions for HTML video: the track element with kind captions, what captions must include (speech, speakers, sounds), timing and reading speed, line length, styling and contrast, open versus closed captions, reviewing automatic captions, and common mistakes, with live checkers.
ποΈ Subtitles
Learn how to add subtitles in several languages to HTML video: track elements with kind subtitles, srclang and native-language labels, language codes, translation tips, right-to-left and Asian scripts, choosing a language with JavaScript, and common mistakes, with a live multi-language demo.
ποΈ Accessible Media
Make HTML audio and video work for everyone: captions, transcripts and audio description, keyboard-friendly controls, autoplay and audio control, reduced motion, flashing and seizure safety, labelling embedded players, and a practical checklist for every kind of media, with live demos.
ποΈ Responsive Video
Make HTML video fit every screen: width and height attributes with max-width and height auto, the CSS aspect-ratio property, object-fit and object-position, responsive iframe embeds, preventing layout shift, saving data on mobile, vertical video, and common mistakes, with live resizable demos.
Good Habits from the Startβ
Before you read the lessons, remember these four ideas. They come back in every one:
- Always show controls (
controls), unless you build your own full set. A video with no way to pause it is a trap. - Never start sound on its own. Autoplay with sound surprises people, and browsers usually block it anyway.
- Provide a text alternative: captions for video, and a transcript for audio.
- Mind the weight. Media files are the heaviest part of most pages, so compress them, and do not load what nobody has asked for.
Anything a browser can play, a visitor can download. A hidden source or a "disable right-click" script does not protect a file. If media must be private, serve it from a server that checks who is asking.
Accessibility at a Glanceβ
- Add captions to every video with speech or meaningful sound.
- Add a transcript for audio-only content, and for video if you can.
- Keep controls visible and keyboard-operable. The built-in controls already are.
- Do not autoplay sound, or flashing or moving content that cannot be paused.
- Do not flash more than three times in any second.
- Give media a nearby heading or label, so people know what it is before they play it.
Security and Privacy Notesβ
- Embedding a third-party player (for example through an
iframe) can let that company track your visitors. Check what is loaded, and ask for consent where the law requires. - Only link to media you have the rights to share. Music, film clips, and voice recordings are usually protected by copyright.
- Serve media over HTTPS, so browsers do not block it as mixed content.
- Be careful with user uploads. Check the type and size on the server, and never trust the file name or the type the browser reports.
Global Audience Notesβ
- Media is heavy. Many visitors have slow, expensive, or limited connections. Compress your files, and use
preload="none"orpreload="metadata"where it makes sense. - Offer captions in more than one language (the Subtitles lesson shows how).
- Do not rely on sound alone to carry instructions: a visitor may be on a train, in a library, or deaf.
- Check the format support of your audience's browsers. Which formats work best can change over time, so test before you decide.
- Right-to-left languages need captions that display correctly. WebVTT supports this, and you will see how in the Captions lesson.
Common Mistakes to Avoidβ
- Forgetting
controls, so nobody can play, pause, or change the volume. - Autoplaying with sound.
- Only one format, so some browsers cannot play it.
- No captions or transcript.
- Huge, uncompressed files that make the page slow.
- Fixed
widthandheightonly, so the video overflows on phones. - No fallback content for browsers that cannot play the media.
- Missing
typeonsource, so the browser downloads files it cannot play. - Embedding sound that cannot be paused (looping background music).
- Assuming a hidden file is private.
Practice Timeβ
Try these in your editor, using any short MP3 and MP4 you have the right to use:
- Add an
audioelement withcontrols, and play it with the keyboard only. - Add a
videoelement withcontrolsand aposterimage. - Give the video two
sourceelements, and check which one loads in the network panel. - Add fallback text inside the video, and read it by pointing at an unsupported type.
- Run
canPlayType()in the console for three formats. - Add
preload="none"and see what the page downloads before you press play. - Make the video fit a narrow phone width, with
max-width: 100%. - Write a three-line WebVTT file, and attach it with
track. - Mute the sound, and check that the video still makes sense. What is missing?
- Bonus: list every file in your project that is over 1 MB, and decide how to shrink each one.
Quick Recapβ
Click a question to check your answer.
1. Which elements play media in HTML?
audio for sound and video for sound with pictures. Both work without plugins, and both can show built-in controls with the controls attribute.
2. Why use more than one source?
Browsers support different formats. With several source elements, each with a type, the browser plays the first one it understands and skips the rest.
3. How do you make video accessible to people who cannot hear it?
Add captions with a track element and a WebVTT file. Captions show speech and important sounds. A transcript helps too, especially for audio-only content.
4. Why should media never autoplay with sound?
It surprises and distracts visitors, can disturb people around them, makes screen reader speech hard to hear, and most browsers block it anyway.
5. Is media you hide on a page private?
No. Anything the browser can play can be downloaded. Private media must be protected on the server.
Key Takeawaysβ
<audio>and<video>play media directly in the browser, with no plugins.- Add
controls, and use<source>to offer more than one format. - Use
<track>to add captions and subtitles, and provide transcripts as well. - Never autoplay sound, and keep everything operable by keyboard.
- Media is heavy: compress it, choose
preloadcarefully, and make it responsive. - Hidden media is not private, and embedded players can track visitors.
- Plan for slow connections, many languages, and visitors who cannot hear or see the media.
Ready to start? Begin with Audio, the simplest media element.