Skip to main content

HTML Images and Media

HTML Video

Written by Updated HTML Video exercises

The video tag plays video on your page with the browser's own player: play, pause, volume, fullscreen. Add controls, point it at an MP4 file, and it works in every current browser, no plugin needed.

HTML <video> Syntax

HTML Video Example

html

<video controls>
  <source src="https://ik.imagekit.io/html5andcss3/lesson-samples/sample_video.mp4" type="video/mp4">
  <source src="https://ik.imagekit.io/html5andcss3/lesson-samples/sample_video.webm" type="video/webm">
  Your browser does not support the video tag.
</video>
  • controls shows the player buttons. Without it, the video just sits there.
  • Each <source> is a version of the video. The browser plays the first one it supports, so put the smaller, more modern format first if you have one.
  • The text inside is only shown in very old browsers.

Attributes

AttributeWhat it does
controlsShows the play, pause, volume and fullscreen buttons.
posterAn image to show before the video starts. Without it, you get a black box or the first frame.
width, heightThe size of the player, in pixels. Set them, so the page does not jump while the video loads.
autoplayStarts playing by itself, but only if the video is also muted.
mutedStarts with the sound off.
loopStarts again when it ends.
playsinlineOn iPhones, plays inside the page instead of jumping to fullscreen.
preloadnone, metadata or auto: how much to download before play is pressed.

Example with Multiple Attributes

html

<video width="640" height="360" controls autoplay muted loop poster="https://ik.imagekit.io/html5andcss3/lesson-samples/sample_thumbnail.png">
  <source src="https://ik.imagekit.io/html5andcss3/lesson-samples/sample_video.mp4" type="video/mp4">
  <source src="https://ik.imagekit.io/html5andcss3/lesson-samples/sample_video.webm" type="video/webm">
  Your browser does not support the video tag.
</video>

This is the pattern for a silent background video: autoplay muted loop, with a poster image while it loads. Browsers block autoplay with sound, so without muted the video will not start on its own.

Which format?

  • MP4 (H.264) plays everywhere. If you provide one file, make it MP4.
  • WebM is often smaller for the same quality, and supported by all current browsers.
  • OGG/OGV is an old open format; you no longer need it.

Example: Complete HTML Video Player

HTML Video Complete Example

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>HTML Video Example</title>
</head>
<body>

<h2>Watch Our Tutorial Video</h2>
<video width="640" height="360" controls poster="https://ik.imagekit.io/html5andcss3/lesson-samples/sample_thumbnail.png">
  <source src="https://ik.imagekit.io/html5andcss3/lesson-samples/sample_video.mp4" type="video/mp4">
  <source src="https://ik.imagekit.io/html5andcss3/lesson-samples/sample_video.webm" type="video/webm">
  Your browser does not support the video tag.
</video>

<p>Enjoy the video directly in your browser with built-in controls!</p>

</body>
</html>

A player with a poster image appears under the heading. Nothing plays until the reader presses play.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesPartially

Captions and size

  • Add captions with the track tag. Many people watch with the sound off, and deaf viewers need them.
  • To make the player fit any screen, add video { max-width: 100%; height: auto; } in CSS.
  • Video files are big. Compress them, and for long videos consider hosting on a video service and embedding it in an iframe.

HTML Video exercises

A video player

Add a video player that plays this file and shows its controls: https://ik.imagekit.io/html5andcss3/lesson-samples/sample_video.mp4

Result

Check your answer to see it here.