Skip to content
Kuza Ndoto Academy

Audio & Video

BeginnerChapter 5 · Page 20 of 254 min readUpdated 15 Nov 2026

Modern HTML can play sound and video directly, with no plug-ins. The <audio> and <video> elements give you a built-in player with play, pause, volume and progress controls, and they work on phones and computers alike.

The video element

<video src="media/robotics-demo.mp4" controls width="640" height="360"
       poster="media/robotics-poster.jpg">
  Your browser can't play this video. <a href="media/robotics-demo.mp4">Download it instead</a>.
</video>
  • src is the video file.
  • controls (a boolean attribute) shows the browser's player controls. Without it, the user has no way to start or stop the video unless you build controls with JavaScript.
  • width and height reserve space, just like for images.
  • poster is an image shown before the video starts. Without it, browsers show the first frame or a blank box.
  • The content between the tags is fallback, shown only by browsers that don't support the element at all.

The audio element

<audio src="media/episode-12.mp3" controls>
  <a href="media/episode-12.mp3">Download episode 12 (MP3)</a>
</audio>

This shows a compact player bar, perfect for a podcast episode or a recorded sermon. Without controls, an audio element is invisible.

Offering several formats

Not every browser supports every file format. Instead of a src attribute, you can list several <source> elements. The browser plays the first one it supports and ignores the rest.

<video controls width="640" height="360" poster="media/poster.jpg">
  <source src="media/demo.webm" type="video/webm">
  <source src="media/demo.mp4" type="video/mp4">
  <p>Your browser can't play this video.</p>
</video>

The type attribute lets the browser skip formats it can't play without downloading them first.

FormatMIME typeNotes
MP4 (H.264 video, AAC audio)video/mp4Plays in all modern browsers; the safest single choice
WebM (VP9 or AV1)video/webmOpen format, often smaller files
MP3audio/mpegUniversal audio support
Ogg (Vorbis or Opus)audio/oggOpen audio format

Autoplay, muted, loop and preload

  • autoplay starts playback as soon as possible. Browsers block autoplay with sound in most situations, because unexpected noise annoys users. A video with autoplay muted is usually allowed.
  • muted starts with the sound off.
  • loop starts again from the beginning when it ends.
  • playsinline asks phones (especially iPhones) to play the video inside the page instead of going full screen.
  • preload hints how much to download before the user presses play: none, metadata (just the length and size) or auto. Choose none or metadata to save visitors' mobile data.
<!-- A silent looping background clip -->
<video src="media/waves.mp4" autoplay muted loop playsinline width="640" height="360"></video>
Warning

Moving content that plays automatically can distract people with attention difficulties and can be uncomfortable for some users. If a video autoplays, keep it short or make sure users can pause it, for example by including controls.

Captions and subtitles

Captions make video usable for Deaf and hard-of-hearing viewers, people watching in a noisy matatu or a quiet library, and learners following a second language. Add them with a <track> element pointing to a WebVTT file (.vtt), a plain-text file of timings and lines.

<video controls width="640" height="360">
  <source src="media/lesson.mp4" type="video/mp4">
  <track kind="captions" src="media/lesson-en.vtt" srclang="en" label="English" default>
  <track kind="subtitles" src="media/lesson-sw.vtt" srclang="sw" label="Kiswahili">
</video>

A WebVTT file looks like this:

WEBVTT

00:00:00.000 --> 00:00:03.500
Welcome to lesson one.

00:00:03.500 --> 00:00:07.000
Today we build a simple website.

kind="captions" means a transcription of speech plus important sounds, such as "[phone rings]". kind="subtitles" means a translation of the dialogue. The player's CC button lets users switch tracks, and default turns that track on at the start.

Note

Caption files must usually come from the same site as the page (or from a server that allows cross-origin requests), so a .vtt file opened from your disk with file:/// may not load. Test captions with Live Server.

Try it

There is no real media file in this example, so nothing will play, but you can see how browsers draw the players. The poster is a data URL drawing. Replace the src values with your own files when you practise locally.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Audio and video</title>
  </head>
  <body>
    <h1>Media corner</h1>
    <h2>Video</h2>
    <video controls width="320" height="180" preload="none"
           poster="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20320%20180'%3E%3Crect%20width='320'%20height='180'%20fill='%231f3a5f'/%3E%3Cpath%20d='M140%2065L190%2090L140%20115Z'%20fill='%23ffcc00'/%3E%3C/svg%3E">
      <source src="lesson.webm" type="video/webm">
      <source src="lesson.mp4" type="video/mp4">
      <p>Your browser can't play this video.</p>
    </video>
    <h2>Podcast</h2>
    <audio controls preload="none">
      <source src="episode-1.mp3" type="audio/mpeg">
      <a href="episode-1.mp3">Download episode 1</a>
    </audio>
  </body>
</html>

The preview shows a dark blue poster with a yellow play triangle and video controls, then a slim audio player bar. Pressing play does nothing (or shows an error) because the files don't exist. Try removing controls from the audio element: the player disappears completely.

Key points

  • <video> and <audio> embed media; add controls so users can play and pause.
  • List several <source> elements with type; the browser plays the first one it supports. MP4 is the safest single choice.
  • Autoplay with sound is usually blocked; autoplay muted is allowed. Use preload to save data.
  • Add captions with <track kind="captions"> and a WebVTT file.
Up next · Media, Head & SEOIframes & EmbedsEmbed maps, videos and other pages safely.
→

Tip: use the ← and → keys to move between pages.