HTML Audio & Video

Play video and sound natively with controls, posters and captions.

Lesson 32 of 44Intermediate13 min read

HTML can play video and audio without any plugins. The <video> and <audio> elements give you a complete player, with a play button, timeline, volume and full-screen mode, from a single attribute: controls.

Before these elements existed, websites needed add-ons such as Flash to play media, and many phones couldn't play it at all. Today the browser handles everything, which means media works on every device and respects each visitor's settings, such as reduced data usage.

In this lesson you'll learn the attributes that control playback, how to offer more than one file format, and how to add captions.

The video element and its attributes

HTML
<video src="trailer.mp4" controls width="640" poster="cover.jpg"></video>
  • controls shows the player buttons. Without it, visitors have no way to start the video.
  • poster sets an image to show before playback starts.
  • width and height set the size of the player.
  • preload="metadata" only fetches the length and size of the video until the visitor presses play, which saves data.

The <audio> element works the same way, without the visual attributes: <audio src="episode.mp3" controls></audio>.

Autoplay rules

Browsers block videos that autoplay with sound, because unexpected noise annoys almost everyone. If you need autoplay, for example for a silent background video, you must add muted:

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

loop repeats the video forever, and playsinline stops iPhones from switching to full screen.

Use autoplay with care. Moving video can distract people who are trying to read, and it uses data on mobile connections.

Offering multiple formats with source

Not every browser plays every format. List your options with <source> elements and the browser uses the first one it supports. Text between the tags is only shown by very old browsers that can't play media at all.

FormatType
MP4 (H.264)video/mp4, plays in every modern browser
WebMvideo/webm, often smaller files
MP3audio/mpeg
OGGaudio/ogg

Adding captions with track

The <track> element adds subtitles or captions from a .vtt (WebVTT) text file:

HTML
<video src="lesson.mp4" controls>
  <track src="lesson-en.vtt" kind="captions" srclang="en" label="English">
</video>

Captions help deaf and hard-of-hearing viewers, people watching in noisy places, and language learners. A .vtt file is plain text listing each line of speech with its start and end time, so you can write one in any text editor.

HTML Video and Audio Syntax

Video with sources and captions

<video controls width="640" poster="cover.jpg">
  <source src="clip.webm" type="video/webm">
  <source src="clip.mp4" type="video/mp4">
  <track src="captions-en.vtt" kind="captions" srclang="en" label="English">
  Your browser does not support video.
</video>

HTML Video and Audio Summary

  • Use <video> and <audio> to play media without plugins.
  • Add controls so visitors can start, pause and adjust playback.
  • Autoplaying video must be muted; use it sparingly.
  • List several <source> formats so every browser can play your media.
  • Add captions with <track> and a WebVTT file.

HTML Video and Audio Examples

Each example runs in the page. Edit the code and press Run to see what changes.

Example 1

A video player

Press play in the result. Then remove controls and run again. Notice there's no way to start it.

<video
  controls
  width="400"
  poster="https://picsum.photos/id/1039/400/225"
  preload="metadata">
  <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm" type="video/webm">
  <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" type="video/mp4">
  Sorry, your browser can't play this video.
</video>
ResultEdit the code, then press Run or Ctrl+Enter
Line-by-line explanation
  1. Line 2controls shows play, pause, timeline, volume and fullscreen.
  2. Line 4The poster image shows before the video starts.
  3. Line 5Only load the video's details until someone presses play.
  4. Lines 6 to 7Two formats. The browser uses the first one it can play.
  5. Line 8Fallback text for very old browsers.
Example 2

An audio player

<p>Listen to the rain:</p>
<audio controls preload="none">
  <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3" type="audio/mpeg">
  Your browser doesn't support audio.
</audio>
<p><small>OK, it's actually a T-rex. Surprise!</small></p>
ResultEdit the code, then press Run or Ctrl+Enter
Line-by-line explanation
  1. Line 2<audio> with controls gives a compact player. preload="none" downloads nothing until play is pressed.
  2. Line 3An MP3 source.
Example 3

A silent looping background video

<video autoplay muted loop playsinline width="400">
  <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" type="video/mp4">
</video>
<p>No controls needed: it's decoration, muted and looping.</p>
ResultEdit the code, then press Run or Ctrl+Enter
Line-by-line explanation
  1. Line 1autoplay only works here because the video is muted. loop repeats it; playsinline keeps it in the page on iPhones.

Common HTML Video and Audio Mistakes

Autoplay with sound

Don't
<video src="ad.mp4" autoplay></video>
Do
<video src="ad.mp4" controls></video>
<!-- or, for silent background video: -->
<video src="bg.mp4" autoplay muted loop playsinline></video>

Browsers block un-muted autoplay, so the first version just sits there frozen.

Forgetting controls

Don't
<video src="lesson.mp4"></video>
Do
<video src="lesson.mp4" controls></video>

Without controls (or your own JavaScript buttons), users can't play the video.

HTML Video and Audio in Real Projects

Self-hosted video vs embeds

For large public videos, most sites embed YouTube or Vimeo with an iframe, because those services handle streaming and heavy traffic. <video> is ideal for short product clips, background loops and videos you don't want adverts on. Online course platforms often use <video> with captions in several languages.

HTML Video and Audio Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Show the player buttons and a cover image.

<video src="cats.mp4"  ="cats-cover.jpg" width="480"></video>
Multiple choicemedium

Which attribute is required for autoplay to work in modern browsers?

Multiple choicemedium

Which element adds captions to a video?

Fix the bugmedium

This background video never plays. Make it autoplay correctly, loop, and play inline on phones.

<video autoplay width="300">
  <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" type="video/mp4">
</video>
ResultEdit the code, then press Run or Ctrl+Enter
Checks
  • The video is muted
  • The video loops
  • The video plays inline
  • Still autoplays

HTML Video and Audio Quiz

3 questions to check what you learned.

  1. Which attribute shows play/pause buttons?

    • controls
    • buttons
    • player
    • ui
  2. Why list several <source> elements?

    • To play them all at once
    • So each browser can pick a format it supports
    • To make the video longer
    • It's required for audio only
  3. What file format do captions for <track> use?

    • .txt
    • .srt only
    • .mp4
    • .vtt (WebVTT)

HTML Video and Audio Challenge

Build a podcast player

Create:

  1. An <h3> with an episode title
  2. An <audio> element with controls and preload="none"
  3. A <source> with src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3" and type="audio/mpeg"
ResultEdit the code, then press Run or Ctrl+Enter
Checks
  • An h3 title
  • Audio with controls
  • preload is none
  • A source of type audio/mpeg