Audio and Video Elements

Beginner · 8 min

How video and audio embed media without plugins: controls, source fallbacks by type, poster, preload and the autoplay rules, with what a real browser did.

Before thisLazy Loading and Image Decoding

Miljan Peric · 8 min

Audio and Video Elements

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Miljan PericReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `<video controls src="clip.webm">` and `<audio controls src="a.mp3">` play media with no plugin. Without `controls` there is no built-in interface, so you must provide one.
  • Several `<source>` children with a `type` let the browser pick a format it can play. In a test, a source with a made-up type was skipped and the next one, `video/webm`, became `currentSrc`.
  • `preload` controls how much loads before play. In a test `preload="none"` made no request at all, while `metadata` and the default fetched the file's head.
  • Autoplay with sound is blocked by browsers until the user interacts. `autoplay muted` is the form that is allowed to work, and `playsinline` keeps iOS from forcing fullscreen.
  • Text inside `<video>` or `<audio>` is fallback content for browsers that do not support the element. Captions and text tracks are separate, in a `<track>` child.

Media without plugins

Until the early 2010s, putting video on a page meant a plugin like Flash. <video> and <audio> moved playback into the browser itself, so a clip is as easy to include as an image:

<video controls width="640" poster="cover.jpg" preload="metadata">
  <source src="clip.webm" type="video/webm">
  <source src="clip.mp4" type="video/mp4">
  Your browser does not support video. <a href="clip.mp4">Download it</a>.
</video>

controls adds the browser’s own play, pause and volume interface. Without it nothing is drawn and the user can’t start playback unless you build controls with JavaScript. poster is an image shown before play. The text inside is shown only by browsers that don’t know <video>, which is now almost none, but a download link costs nothing. MDN’s data puts video and audio in the Baseline widely available set since January 2018.

Choosing a source by type

A container such as MP4 or WebM can hold different codecs, and not every browser plays every combination. The browser reads your <source> list in order and uses the first one it believes it can play, judged by type. I tested this in headless Chromium 159 with a recorded WebM clip: a first <source type="video/x-fake"> was skipped and currentSrc became the second source, the real video/webm one, which loaded at its true 320 by 240 size. Note that the canPlayType() answers I got (“probably” for VP8, VP9, MP4 with H.264 and MP3) describe that one browser build, not every browser. Browsers differ in which codecs they ship, so check the ones you target.

If you give a single src, there’s no fallback. Use <source> elements whenever you serve more than one format.

Loading: preload and poster

A video file is large, so preload matters. The values are none, metadata and auto. In a test with three videos on a page:

  • The default, and preload="metadata", requested the file (enough to read the duration and size).
  • preload="none" made no request for the file at all.

Pair preload="none" with a poster for videos most visitors won’t play. The element’s preload property reported metadata for an element with no attribute in this browser, but the spec leaves the default to the browser. Lazy-loading as in the previous lesson applies to images and iframes, while preload is the media equivalent.

Autoplay and its rules

Autoplay is the attribute people most often get wrong. Browsers block playback with sound until the visitor has interacted with the page, since a page that starts shouting is a nuisance. The form that’s allowed to work is autoplay muted, and on iOS you also need playsinline to avoid a forced fullscreen. MDN’s data lists playsinline in Chrome 75 and Safari 10 and not in Firefox, where inline playback is already the default. My headless test run did not enforce the autoplay policy, since autoplay without muted played anyway, so I can’t demonstrate the blocking here. Treat the behaviour as documented rather than tested. Autoplaying motion is also a reduced-motion concern, and a looping background clip should have a way to pause it.

Controls are not accessibility

The built-in controls are meant to be keyboard operable, though I didn’t test that, and they only play, pause and seek. They don’t make the content accessible: deaf visitors need captions and blind visitors need an audio description or a transcript. That is the next lesson. Audio is the same in principle: <audio controls src="a.mp3"> plus a transcript for speech.

What the video covers

The embed is Miljan Peric’s “HTML5 Tutorial #25: HTML Video Element Explained” (November 2026, about 8 minutes). The description lists the video and source elements, controls, autoplay, loop, muted, poster, width and height, preload, src and type, and fallback text, with a note that browsers block autoplay with sound. It’s a brand-new video from a small channel with few views, so it’s a pick on fit rather than reputation, and I could not confirm its audio language. Titles and descriptions are in English. I could not read a transcript, so I describe only the listed topics. The source-fallback and preload results above are this lesson’s own; the autoplay section is documented behaviour, not tested. The deeper link is the same creator’s audio episode, #24.

Where this fits

This comes after the image lessons, ending with lazy loading and decoding. Next, captions, subtitles and text tracks makes this media usable by everyone, and iframes cover media you embed from someone else’s player.

Check yourself

  1. 01You want a video to start on its own as a muted background loop. Which attributes?

    `autoplay muted loop playsinline`. Browsers allow autoplay when the video is muted, and `playsinline` keeps iOS Safari from opening it fullscreen. Do not autoplay with sound, since it is blocked and unwelcome. Also consider `poster` and a pause control for people sensitive to motion.

  2. 02What is the point of listing several `<source>` elements?

    Different browsers decode different formats. The browser reads the sources in order and uses the first whose `type` it can play. In a test a source of type `video/x-fake` was skipped and `video/webm` loaded. Put the preferred format first and keep a fallback.

  3. 03A page has ten videos and each downloads on page load. What attribute helps?

    `preload="none"` on the ones the visitor may never play, perhaps with a `poster` image. In a test `preload="none"` produced no request for the file, while the default `metadata` fetched the beginning to read duration and size.

  4. 04Why must a video you embed with `<video>` still have a text alternative?

    People who cannot hear need captions and people who cannot see need audio description or a transcript. The `controls` interface does not provide either. Add a `<track>` for captions, covered in the captions lesson, and a transcript nearby.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark