Modern browsers play audio and video without plugins. The <audio> and <video> elements work almost identically, so learning one teaches you both. Our cafe will add a short welcome clip and a playlist sample.
The video element#
<video controls width="640" height="360" poster="images/welcome-poster.jpg" preload="metadata">
<source src="media/welcome.webm" type="video/webm">
<source src="media/welcome.mp4" type="video/mp4">
<track kind="captions" src="media/welcome.en.vtt" srclang="en" label="English" default>
<p>Your browser cannot play this video. <a href="media/welcome.mp4">Download it instead</a>.</p>
</video>Key attributes:
| Attribute | Purpose |
|---|---|
controls | Shows play, pause, volume and seek controls |
poster | Image displayed before playback starts |
preload | none, metadata or auto: how much to fetch early |
loop | Restart when finished |
muted | Start without sound |
autoplay | Start automatically (heavily restricted) |
playsinline | Play within the page on iOS rather than full screen |
Multiple sources#
Browsers support different formats. List several <source> elements, each with a type, and the browser uses the first it can play. MP4 (H.264) is the widest supported, and WebM or newer codecs can be smaller. Anything between the tags (the paragraph above) is fallback content.
The audio element#
<audio controls preload="none">
<source src="media/cafe-playlist.ogg" type="audio/ogg">
<source src="media/cafe-playlist.mp3" type="audio/mpeg">
Your browser does not support audio. <a href="media/cafe-playlist.mp3">Download the sample</a>.
</audio>Without controls, an audio element is invisible. Always include controls unless your own scripts provide an accessible alternative.
Captions and accessibility#
Media must be usable by everyone:
- Captions (
<track kind="captions">) help deaf and hard-of-hearing viewers, and anyone watching without sound. Files use the WebVTT (.vtt) format. - Transcripts for audio let people read instead of listen, and help search engines.
- Audio descriptions support blind viewers when important visuals have no narration.
- Never start sound automatically. It startles users and makes screen readers hard to hear.
<track kind="subtitles" src="media/welcome.ne.vtt" srclang="ne" label="Nepali">Autoplay rules#
Browsers block autoplay with sound. A video may autoplay only when it is muted (and usually playsinline). Even then, think carefully: moving content distracts and uses data. Provide a way to pause if the video plays longer than five seconds.
Common mistakes#
- Forgetting
controls, so nothing can be played. - Offering only one format that some browsers cannot play.
- No captions or transcript.
- Autoplaying with sound.
- Uploading a raw 500 MB camera file.
Try it yourself#
Place a short video and an audio file in a media folder. Embed both with controls, two source formats each and a fallback link. Add a captions file with at least three cues (a simple VTT needs a WEBVTT line, then timestamps and text), and verify the captions button appears. Try adding autoplay and observe what the browser allows.
Recap#
video and audio embed media natively, with source elements offering formats, controls for the interface, track for captions and a fallback inside. Respect autoplay limits and provide captions and transcripts.
Check your understanding
Lesson quiz
OptionalA short quiz to check what you just learned.
- Questions
- 4
- Time limit
- 5 min
- Pass mark
- 70%
- Best score
- -