Full answer
The video and audio elements play media without plugins. Add the controls attribute so users get play, pause, volume and seek buttons. Without it, nothing is shown unless you build your own controls.
Offer more than one file with source elements. The browser uses the first file it can play, based on the type. Text between the tags is a fallback for browsers that do not support the element.
Captions and subtitles use the track element with a WebVTT file. kind="captions" includes speaker sounds as well as speech, kind="subtitles" is a translation of speech, and kind="descriptions" describes visuals. Set srclang and a label, and add default to turn one on by default. Captions help deaf and hard-of-hearing viewers, people in noisy places, and learners who read along. Audio-only content should have a transcript on the page.
Other useful attributes: poster is the image shown before play, preload hints how much to download (none, metadata, auto), loop repeats, and playsinline lets video play inside the page on some phones instead of full screen.
Autoplay is limited by browsers. Playback with sound usually needs a user action first, while muted autoplay is generally allowed. Do not autoplay sound, because it surprises users and can make a screen reader hard to hear.
The video has two source files, one captions track in English, and a poster. The audio element downloads nothing until the user presses play, because of preload="none", and the transcript link serves people who cannot listen.
The file names in this example are placeholders, so use your own files when you try it.
<video id="lesson" controls preload="metadata" width="480" poster="lesson-poster.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" default>
<p>Your browser cannot play this video. <a href="lesson.mp4">Download it</a>.</p>
</video>
<audio id="podcast" controls preload="none" src="episode-1.mp3"></audio>
<p><a href="/episode-1-transcript">Read the transcript of episode 1</a></p>The media files and the caption file are placeholders. A local test server returned an empty body for the video and a small caption file for the track, so the check read the track elements and their attributes, not playback. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- controls gives users play, pause, volume and seek.
- Several source elements let the browser pick a format it can play.
- track with kind, srclang, label and a WebVTT file adds captions or subtitles.
- poster, preload and playsinline control the first impression and data use.
- Audio needs a transcript on the page.
Common mistakes
- Skipping captions because the page already has a transcript. Both help, and captions are expected for video.
- Autoplaying with sound. Browsers often block it and users dislike it.
- Forgetting the type attribute on source, so the browser downloads parts of files to find out if it can play them.
Follow-ups
- What is the difference between captions and subtitles?
- Why does muted autoplay usually work while autoplay with sound is blocked?
Tips
- Name the kinds of track (captions, subtitles, descriptions) and the file format (WebVTT) in your answer.
- Mention preload="metadata" or "none" to save data on pages with many media files.