Loading...

HTML essentialsMedia

Audio, video & embed

6 min lesson

Next
  • HTML can play audio and video directly in the page without any plugin.
  • <audio> plays sound, <video> plays moving pictures, and both use <source> to list file formats.
  • Think of a media player as a radio: you give it a few stations (formats), and it plays the first one it can handle.
  • The controls attribute shows play, pause and volume buttons; without it, the player is invisible.
  • A <track> adds subtitles or captions to a video for accessibility.
  • This builds on Interactive elements: both add behavior without a page reload.
  • The example shows a video with two source formats, controls and a caption track; you will see a playable video with subtitles.
  • Next, Responsive images: srcset, sizes & picture makes images adapt to screen size.
Keep in mind
  • Always add controls so users can play, pause and adjust volume.
  • List sources from most to least preferred; the browser picks the first it supports.
  • Always add a captions track to any video with speech.
  • Provide a fallback text inside the tag for old browsers.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Media</title>
</head>
<body>
  <!-- A video player with controls and captions -->
  <video controls width="480">
    <!-- List sources: the browser uses the first it supports -->
    <source src="clip.webm" type="video/webm">
    <source src="clip.mp4" type="video/mp4">
    <!-- Captions for users who cannot hear the audio -->
    <track kind="captions" src="clip.en.vtt" srclang="en" label="English">
    <!-- Fallback text for very old browsers -->
    Your browser does not support video.
  </video>

  <!-- An audio player with controls -->
  <audio controls>
    <source src="song.mp3" type="audio/mpeg">
    Your browser does not support audio.
  </audio>
</body>
</html>

What happens: A page with a video player (480 pixels wide) showing play and volume controls and a captions button, and below it an audio player with play and volume controls.

Breaking it down

video

<video controls width="480">

Creates a video player. Without controls, there are no buttons; without width, it uses the file's natural size.

source

<source src="clip.webm" type="video/webm">

One file format option. The browser uses the first source it can play and ignores the rest.

track

<track kind="captions" src="clip.en.vtt">

Loads a captions file. It needs a WebVTT file with the same name pattern as the video.

audio

<audio controls>

Creates a sound player. Same source and fallback pattern as video, but no picture.

Media attributes

AttributeEffect
controlsShows play and volume buttons
autoplayStarts playing automatically (often blocked)
mutedStarts with sound off
loopRepeats when finished
posterAn image shown before video plays

A helpful analogy

A media element is like a TV with several input cables plugged in. The browser tries each cable (source) until it finds a signal it can display. Controls are the remote; captions are the subtitles on screen.

Nesting rules

  • 1.source and track must sit inside audio or video, never outside.
  • 2.Put text fallback after the sources but still inside the media element.

Common mistakes

Forgetting controls

Avoid

<video><source src="clip.mp4"></video>

Use instead

<video controls><source src="clip.mp4"></video>

Without controls, the player is invisible and users cannot start the video. It looks like nothing loaded.

Forgetting captions on a video with speech

Avoid

<video controls><source src="talk.mp4"></video>

Use instead

Add <track kind="captions" src="talk.en.vtt">

Deaf users cannot follow the speech. Captions are also required by accessibility guidelines in many places.

Quick tips

  • •Mute autoplay videos; browsers block autoplay with sound, so the video may not start at all.
  • •Add a poster image to your video so the first frame is not a black box.
Previous
Next