Ad – 728Γ—90
🌐 Advanced HTML

HTML Video and Audio – video, audio, source, track Elements

HTML5 brought native <video> and <audio> elements β€” no Flash, no plugins. With a handful of attributes you get cross-browser playback, captions, accessible controls, and JavaScript-powered interaction. This lesson covers everything from basic embedding to performance optimisation.

⏱️ 18 min read🎯 BeginnerπŸ“… Updated 2026

The video Element

The <video> element embeds a video player directly in the page. The browser provides default controls β€” play/pause, seek bar, volume, fullscreen β€” when you add the controls attribute.

HTML
<!-- Simplest video with browser controls -->
<video src="intro.mp4" controls width="800" height="450">
  Your browser does not support the video element.
</video>

<!-- All common attributes -->
<video
  src="lesson.mp4"
  controls          <!-- show browser playback controls -->
  autoplay          <!-- start playing immediately -->
  muted             <!-- required for autoplay in most browsers -->
  loop              <!-- replay when finished -->
  playsinline       <!-- play inline on iOS instead of fullscreen -->
  poster="thumbnail.jpg"  <!-- image shown before video starts -->
  width="800"
  height="450">
  Your browser does not support the video element.
</video>
Autoplay policy: Most browsers block autoplay with sound. For autoplay to work, you must also add muted. This is intentional β€” unexpected audio is one of the most common user experience complaints.

Multiple Sources with the source Element

Different browsers support different video formats. Use the <source> element to list multiple formats β€” the browser picks the first one it can play.

HTML
<video controls width="800" height="450" poster="thumbnail.jpg">
  <!-- MP4 (H.264): supported by all modern browsers β€” list first -->
  <source src="video.mp4" type="video/mp4">

  <!-- WebM (VP9): smaller file size, supported by Chrome/Firefox/Edge -->
  <source src="video.webm" type="video/webm">

  <!-- OGG/Theora: legacy Firefox fallback -->
  <source src="video.ogv" type="video/ogg">

  <!-- Fallback text for very old browsers -->
  Your browser does not support HTML5 video.
  <a href="video.mp4">Download the video</a> instead.
</video>
FormatContainerCodecBrowser Support
MP4.mp4H.264All modern browsers
WebM.webmVP8/VP9/AV1Chrome, Firefox, Edge, Opera
OGG.ogvTheoraFirefox, Chrome (legacy)

In practice, MP4 is enough for most projects since all modern browsers support it. Add WebM as a second source if file size is a concern (WebM with VP9 is typically 30–40% smaller than MP4).

The audio Element

The <audio> element works identically to <video> β€” same attribute set, same <source> pattern β€” but it has no visual area beyond the browser controls.

HTML
<!-- Simple audio player -->
<audio src="podcast.mp3" controls>
  Your browser does not support the audio element.
</audio>

<!-- With multiple sources and all attributes -->
<audio controls loop preload="metadata">
  <source src="song.mp3" type="audio/mpeg">
  <source src="song.ogg" type="audio/ogg">
  <source src="song.wav" type="audio/wav">
  Your browser does not support HTML5 audio.
</audio>
FormatMIME typeNotes
.mp3audio/mpegUniversally supported, lossy compression
.oggaudio/oggOpen format, good quality, Chrome/Firefox
.wavaudio/wavUncompressed, large file size, all browsers
.aacaudio/aacApple devices, good compression
.webmaudio/webmChrome/Firefox, modern codec

The track Element – Subtitles and Captions

The <track> element adds timed text tracks to video and audio. Captions and subtitles are required for WCAG 2.1 Level AA compliance (Success Criterion 1.2.2 β€” Captions for pre-recorded content).

HTML
<video controls width="800" height="450">
  <source src="lesson.mp4" type="video/mp4">

  <!-- Captions (subtitles + sound descriptions for deaf viewers) -->
  <track
    kind="captions"
    src="captions-en.vtt"
    srclang="en"
    label="English captions"
    default>

  <!-- Subtitles (translation for hearing viewers who don't know the language) -->
  <track
    kind="subtitles"
    src="subtitles-es.vtt"
    srclang="es"
    label="EspaΓ±ol">

  <!-- Chapter markers (shown in seek bar in some browsers) -->
  <track
    kind="chapters"
    src="chapters.vtt"
    srclang="en"
    label="Chapters">
</video>

Track files use the WebVTT (.vtt) format. Here is a minimal example:

WebVTT
WEBVTT

00:00:00.000 --> 00:00:03.000
Welcome to this HTML tutorial.

00:00:03.500 --> 00:00:07.000
Today we'll learn about the video element.

00:00:07.500 --> 00:00:12.000
Let's look at the source and track elements.
Difference between captions and subtitles: Captions include all audio information β€” dialogue, sound effects, music descriptions β€” intended for deaf or hard-of-hearing viewers. Subtitles are translations of dialogue only, for viewers who can hear but do not understand the language. Always prefer captions for accessibility compliance.

Fallback Content for Unsupported Browsers

HTML
<video controls width="800" height="450">
  <source src="lesson.mp4" type="video/mp4">
  <source src="lesson.webm" type="video/webm">
  <track kind="captions" src="captions-en.vtt" srclang="en" label="English" default>

  <!-- Fallback: visible only if browser does not support <video> -->
  <p>
    Your browser doesn't support HTML5 video.
    <a href="lesson.mp4">Download the video (MP4, 45 MB)</a>
    or <a href="transcript.html">read the transcript</a>.
  </p>
</video>

JavaScript Control – play(), pause(), currentTime

The HTMLVideoElement (and HTMLAudioElement) expose a rich API for programmatic control.

HTML
<video id="myVideo" src="lesson.mp4" width="800" height="450"></video>
<button onclick="playVideo()">Play</button>
<button onclick="pauseVideo()">Pause</button>
<button onclick="skipTo(60)">Jump to 1:00</button>

<script>
  const video = document.getElementById('myVideo');

  function playVideo() {
    video.play();   // returns a Promise
  }

  function pauseVideo() {
    video.pause();
  }

  function skipTo(seconds) {
    video.currentTime = seconds;  // seek to position in seconds
  }

  // Useful properties
  console.log(video.duration);     // total length in seconds
  console.log(video.currentTime);  // current playback position
  console.log(video.paused);       // true / false
  console.log(video.volume);       // 0.0 to 1.0
  console.log(video.muted);        // true / false
  console.log(video.playbackRate); // 1.0 = normal speed, 2.0 = 2x speed

  // Events
  video.addEventListener('play',   () => console.log('Playing'));
  video.addEventListener('pause',  () => console.log('Paused'));
  video.addEventListener('ended',  () => console.log('Finished'));
  video.addEventListener('timeupdate', () => {
    // Fires multiple times per second during playback
    console.log('Position:', video.currentTime.toFixed(1), 's');
  });
</script>

Performance: The preload Attribute

The preload attribute controls how much data the browser downloads before the user presses play. On mobile networks and for large video files, choosing the right value saves significant bandwidth.

HTML
<!-- none: download nothing until user presses play
     Best for pages with many videos, or when bandwidth matters -->
<video src="large-video.mp4" controls preload="none" poster="thumb.jpg"></video>

<!-- metadata: download only duration, dimensions, first frame
     Good default β€” allows poster/duration display without big download -->
<video src="lesson.mp4" controls preload="metadata"></video>

<!-- auto: browser downloads as much as it thinks is appropriate
     Default behaviour if preload is omitted.
     Can waste bandwidth on pages where user might not watch the video -->
<video src="hero-video.mp4" controls preload="auto"></video>
Performance tip: Use preload="none" combined with a poster image for videos that are not the primary content of the page. This can save hundreds of megabytes of bandwidth for users who never watch the video.
Ad – 336Γ—280

πŸ“‹ Summary

  • Use <video controls> for video and <audio controls> for audio β€” controls shows the browser's built-in player UI.
  • Autoplay requires muted in modern browsers.
  • List multiple <source> elements for cross-browser format compatibility β€” MP4 first, then WebM.
  • The <track> element adds VTT captions/subtitles β€” required for WCAG 2.1 AA compliance on pre-recorded video.
  • Always provide fallback content between the opening and closing tags.
  • Control playback programmatically with video.play(), video.pause(), and video.currentTime.
  • Use preload="none" or preload="metadata" to reduce bandwidth usage on pages where the video is optional.

FAQ

Why won't my video autoplay?

Browsers block autoplay with audio to prevent unwanted noise. To enable autoplay you must add the muted attribute alongside autoplay. On mobile browsers you may also need playsinline. If you need audio to autoplay, there must be a user gesture (click/tap) first β€” use a JavaScript click handler to call video.play() after user interaction.

What is the difference between captions and subtitles in the track element?

Both display timed text over video. Captions (kind="captions") are designed for deaf and hard-of-hearing viewers β€” they include all meaningful audio: dialogue, speaker identification, sound effects, and music cues. Subtitles (kind="subtitles") are translations of the spoken dialogue for viewers who can hear but do not understand the language. For accessibility compliance, use kind="captions".

Should I host videos on my own server or use YouTube/Vimeo?

Self-hosting gives you full control over the player, no ads, and no third-party tracking, but you pay for storage and bandwidth (video files are large). Third-party hosting (YouTube, Vimeo, Cloudflare Stream) handles transcoding, CDN delivery, and adaptive bitrate streaming for free or low cost. For most educational sites, hosting on YouTube and embedding via <iframe> with the /embed/ URL is the most practical approach β€” see the Iframe lesson for the correct embed pattern.