Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 122 ยท Images and Media

Video

Embed video with the HTML video element. Use controls, multiple sources, a poster image, autoplay rules, muted, loop, playsinline, preload and sizing.

Before HTML5, playing a video on a web page meant installing a plug-in such as Flash. Today every browser has a built-in video player, and you control it with one element: <video>. It gives you play and pause buttons, a timeline, volume, fullscreen and picture-in-picture, all without a single line of JavaScript.

In this lesson you will add a video with controls, offer it in more than one format, show a preview image, understand when browsers allow autoplay, and keep videos from breaking your layout. Everything here uses an eight-second clip made for this course.

Your first video

The simplest useful video needs a src and the controls attribute:

A basic video

controls is a boolean attribute: present means "show the browser's built-in controls". Without it the video still loads, but the visitor has no way to start it unless you add your own buttons with JavaScript. For almost every video, include controls.

Each browser draws its own controls, so the player looks a little different in Chrome, Firefox and Safari. That is normal. The keyboard and screen-reader support built into those controls is a good reason to keep them.

Formats and <source> fallbacks

A video file is a container (such as MP4 or WebM) holding codecs that compress the picture and sound. Not every browser plays every combination, so you can offer several files and let the browser choose:

HTML
<video controls width="640" height="360">
  <source src="/media/trailer.webm" type="video/webm">
  <source src="/media/trailer.mp4" type="video/mp4">
  <p>
    Your browser cannot play this video.
    <a href="/media/trailer.mp4">Download the MP4 instead</a>.
  </p>
</video>

The browser reads the <source> elements in order and plays the first one it supports. The type attribute lets it skip unsupported formats without downloading anything, so always include it. Put the version you would prefer people get first.

Container Typical codecs Support
MP4 H.264 video + AAC audio Every current browser and device. The safe default.
WebM VP9 or AV1 video + Opus audio All current major browsers. Often smaller at the same quality.
MP4 AV1 or H.265 (HEVC) video AV1 is widely supported on newer devices; HEVC support is uneven.

If you only have one file, make it an H.264 MP4.

You can be more specific with a codecs parameter, for example type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'. This helps the browser decide when the same container may hold different codecs. Note the single quotes around the attribute value, since the codec list itself needs double quotes.

What is the text inside <video> for? Content between the tags (other than <source> and <track>) is shown only by browsers that do not support <video> at all โ€” which today means essentially none. It is not shown when the browser simply cannot play your files. A download link there is still a harmless courtesy.

A poster image

Until the video starts, the browser shows its first frame โ€” or a blank box if it has not loaded any frames yet. The poster attribute sets a still image to show instead:

Sources and a poster

A good poster is an attractive, representative frame, or a title card. Use the same aspect ratio as the video so nothing gets letterboxed.

Preloading

The preload attribute hints how much the browser should download before the visitor presses play:

Value Meaning
none Download nothing until the user plays. Saves the most data.
metadata Download just enough to know the duration and dimensions (and often the first frame).
auto The browser may download the whole file in advance.

It is only a hint. Browsers apply their own policies โ€” mobile browsers in particular may ignore auto to save data โ€” and the default varies between browsers, though metadata is the common choice. For a page with several videos, preload="none" plus a poster keeps the page light.

Watching the loading events

The console shows loadedmetadata straight away: the browser already knows the clip lasts 8 seconds. Change preload to none, run again, and that event waits until you press play.

Autoplay, muted, loop and playsinline

autoplay asks the browser to start the video as soon as it can. Browsers do not always obey, because unexpected sound is one of the most annoying things on the web. The rules in every major browser today boil down to this:

  • Muted autoplay is allowed. A video with both autoplay and muted will usually start on its own.
  • Autoplay with sound is usually blocked until the visitor has interacted with the site (for example, clicked or tapped). Chromium browsers also remember sites where people often play media and may allow sound there; Safari and Firefox let users choose per site.
  • Power-saving modes can block autoplay entirely, even when muted, for example iOS Low Power Mode.

So treat autoplay as a request, never a guarantee, and design the page so it still works if the video stays paused.

Three more attributes often travel with it:

  • muted โ€” starts the video with the sound off. Viewers can still unmute with the controls.
  • loop โ€” jumps back to the start when the video ends.
  • playsinline โ€” on iPhones, plays the video inside the page instead of switching to fullscreen. Muted inline autoplay on iOS needs it.

This combination is common for short, silent, decorative clips that behave like an animated image:

A muted, looping clip

Give people a way to stop motion. Moving backgrounds can distract readers and make some people feel ill. Keep controls on anything longer than a few seconds, never autoplay video with sound, and respect the prefers-reduced-motion setting in CSS or JavaScript (for example, by pausing the clip and showing the poster instead). Accessibility guidelines require a way to pause any motion that lasts longer than five seconds.

Sizing videos

Like images, videos take width and height attributes in CSS pixels, without units. They do the same two jobs: set a default size and let the browser reserve the right amount of space before the file loads. To keep the video fluid in a responsive layout, add the same CSS you use for images:

CSS
video {
  max-width: 100%;
  height: auto;
}

If the box you give a video has a different shape from the video itself, the browser letterboxes it: the picture keeps its proportions and the leftover space stays empty (usually black). To fill the box and crop the edges instead, use CSS object-fit: cover, the same property that works for images.

Other useful attributes

  • disablepictureinpicture โ€” hides the picture-in-picture option where browsers offer one.
  • crossorigin โ€” needed when you load video or captions from another domain and want to use them with features that require CORS.
  • controlslist="nodownload" โ€” hides the download button in Chromium browsers. It is not part of the HTML standard and does not stop anyone from saving the file.

Videos also deserve captions for people who cannot hear the sound. You will add them in Captions and Subtitles. And when your video lives on a hosting platform rather than your own server, see Embedding Videos, Maps and Other Content.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A video has autoplay but does not start on its own in most browsers. What is the most likely fix?

    Browsers generally allow autoplay only for muted video until the user has interacted with the site.
  2. 2. Why should every source element include a type attribute?

    With type, the browser knows from the HTML alone whether it can play the file and moves on to the next source if not.
  3. 3. When is the text placed inside the video element shown?

    Fallback content is for browsers without video support. If the files simply cannot be played, the browser shows an empty player instead.
  4. 4. What does playsinline do?

    Without playsinline, iOS Safari plays videos fullscreen, and muted inline autoplay there requires it.

Summary

  • <video> embeds video natively; add controls so people can play, pause and seek.
  • Offer several formats with <source> elements and type attributes; the first playable one wins. H.264 MP4 is the safe default.
  • poster shows an image before playback; preload (none, metadata, auto) hints how much to download early.
  • Autoplay is a request: browsers allow it reliably only with muted, and iOS also needs playsinline.
  • Set width and height and use max-width: 100%; height: auto for fluid, shift-free videos.
  • Give viewers a way to stop motion and respect prefers-reduced-motion.

Next up: sound without pictures in Audio.