Audio & Video — HTML

<video src="..." controls width="..." height="..."> plays a video, and <audio src="..." controls> plays sound. controls shows the player's buttons. Without…

Do you want to put a tutorial video or a podcast recording on your own page? You don't need a plugin (an extra program for the browser). <video> plays a video and <audio> plays sound directly on the page. The browser draws the player itself: the play, pause and volume buttons and the timeline.

Video and audio, part by part

<video src="video.mp4" controls width="640" height="360">
  Your browser does not support video.
</video>

<audio src="music.mp3" controls>
  Your browser does not support audio.
</audio>

Part by part:

The result: a 640 by 360 pixel video frame with its buttons, and an audio player below it.

More playback attributes

All four can be used together:

<video src="background.mp4" autoplay muted loop poster="cover.jpg"></video>

Browsers block autoplay with sound until the visitor interacts with the page, for example with a click or a tap. This way nobody is surprised by sudden sound. A video with no sound is not blocked. That is why autoplay is almost always written together with muted.

For a background video like the example above, also add playsinline. Without playsinline, Safari on an iPhone plays the video in full screen, so it cannot play as a background inside the page. For example: <video src="background.mp4" autoplay muted loop playsinline poster="cover.jpg"></video>.

Several file formats with <source>

<audio controls>
  <source src="music.ogg" type="audio/ogg">
  <source src="music.mp3" type="audio/mpeg">
  Your browser does not support audio.
</audio>

Not every browser can play every format. If you have the same file in several formats, put each file in its own <source>, with a type that names its format. The browser uses the first file it can play. In this form, src moves from the <audio> tag to each <source>. When an exercise asks for src on the <audio> or <video> tag, write it there, as in the first example.

Today MP3 and MP4 play in every modern browser, so one file in src is enough. <source> is useful when you have a format with smaller files, such as WebM. Put WebM first, then MP4 as the fallback.

⚠️ Players that don't show up or do nothing

💡 A video with people talking needs captions. Add <track kind="captions" src="captions.vtt" srclang="en" label="English"> inside the <video>, so people can still understand the video without sound. captions write out the speech and important sounds for viewers who can't hear, while subtitles are for viewers who can hear but don't understand the language. src holds the text file (a .vtt file), srclang its language, and label its name in the player's menu.

TL;DR: <video src="..." controls width="..." height="..."> plays a video, and <audio src="..." controls> plays sound. controls shows the player's buttons. Without controls, audio is not visible at all. poster is the video's cover, loop repeats it, and autoplay starts it automatically. autoplay needs muted (no sound) so the browser doesn't block it. Text inside the tag only appears in browsers that don't support the tag. When there are several file formats, each file goes in its own <source>.

⚡ A player drawn by the browser

Write:

<video src="video.mp4" controls width="640" height="360">
  Your browser does not support video.
</video>

<audio src="music.mp3" controls>
  Your browser does not support audio.
</audio>
Guess first: what does an <audio> with no controls (the player's buttons) look like?

It is not visible at all, so visitors can't play it.

Meaning: controls shows the play, pause and volume buttons and the timeline. width and height are numbers only, without px. Text between the opening and closing tags only appears in browsers that don't support that tag.

⚡ autoplay needs muted

Write:

<video src="background.mp4" autoplay muted loop poster="cover.jpg"></video>

Meaning: autoplay starts the video as soon as the page opens, and muted turns its sound off. Browsers block autoplay with sound until the visitor clicks or taps the page. That is why autoplay almost always comes with muted. loop repeats the video, and poster is its cover.

⚡ Several file formats

Write:

<audio controls>
  <source src="music.ogg" type="audio/ogg">
  <source src="music.mp3" type="audio/mpeg">
</audio>

Meaning: When there are several formats, each file gets its own <source>, and the browser uses the first file it can play.

What you will learn