<video>

A video player embedded directly in the page.

Display
inline (replaced element)
Closing tag
Required
Permitted content
<source> and <track> elements, followed by fallback content
On this page

Example

#
HTML
<video controls width="300" height="200" preload="none" poster="/collections/covers/od-czego-zaczac-nauke-programowania.webp">
  <source src="/media/lekcja-1.webm" type="video/webm">
  <source src="/media/lekcja-1.mp4" type="video/mp4">
  <track kind="captions" src="/media/lekcja-1-pl.vtt" srclang="pl" label="Polish" default>
  <a href="/media/lekcja-1.mp4">Download the lesson as MP4</a>
</video>
Preview

The video file and the captions are skipped in the preview, so you see only the poster and the controls.

Definition and usage

#

The <video> element displays a video from a file without any external plugins. You give the source in the src attribute or in several <source> elements with different formats, and you add subtitles with <track> elements.

The controls attribute shows the built-in playback bar, and poster sets an image shown before the video starts. It is also worth providing width and height, so the browser reserves space for the player before the video loads.

Autoplay usually works only when muted, which is why short background animations are written as autoplay muted loop playsinline. You canโ€™t embed videos from services like YouTube with <video>. For those, use an <iframe> with the code the service provides.

Syntax

#
Syntax
<video src="video.mp4" controls width="โ€ฆ" height="โ€ฆ"></video>

Attributes

#
  • src

    URL

    The address of the video file. When you want to offer several formats, use <source> elements inside <video> instead of src.
  • controls

    boolean attribute

    Shows the built-in controls: play, timeline, volume, full screen. Without them the user has no way to control the video, unless you build your own buttons.
  • poster

    image URL

    An image displayed before the video starts. Without it the player shows the first frame of the video, or an empty rectangle until that frame loads.
  • width / height

    number of pixels

    The dimensions of the player. The video is fitted to this area while keeping its proportions, and with different proportions empty bars appear.
  • autoplay

    boolean attribute

    Starts the video right after loading. Browsers usually allow this only when the video is muted with the muted attribute.
  • muted

    boolean attribute

    Starts the video with the sound off. In practice essential with autoplay.
  • loop

    boolean attribute

    When the video reaches the end, it starts again from the beginning.
  • playsinline

    boolean attribute

    On iPhone, plays the video in place on the page instead of automatically switching to full screen. Important for short background animations.
  • preload

    none | metadata | auto

    How much data to download in advance. On a page with many videos, set none or metadata so you donโ€™t waste bandwidth.

The element also supports all global attributes, such as class, id, style and hidden.

Attributes in practice

#

controls

boolean attribute

Shows the built-in controls: play, timeline, volume, full screen. Without them the user has no way to control the video, unless you build your own buttons.

HTML
<video src="/media/lekcja-2.mp4" controls width="240" height="160" preload="none" poster="/collections/covers/programowanie-dla-dzieci.webp"></video>
Preview

The video file is skipped in the preview.

poster

image URL

An image displayed before the video starts. Without it the player shows the first frame of the video, or an empty rectangle until that frame loads.

HTML
<video src="/media/gra-w-kodowanie.mp4" controls width="240" height="160" preload="none" poster="/collections/covers/gry-do-nauki-programowania.webp"></video>
Preview

The video file is skipped in the preview, only the poster is visible.

More examples

#
A short background animation
HTML
<video autoplay muted loop playsinline width="300" height="200" poster="/collections/covers/vibe-coding.webp">
  <source src="/media/tlo-animacja.webm" type="video/webm">
  <source src="/media/tlo-animacja.mp4" type="video/mp4">
</video>
Preview

The video file is skipped in the preview, so only the poster is visible. On a real page the animation loops without sound.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Add subtitles with <track kind="captions">. Many people watch videos without sound, and for deaf people captions are the only way to follow along.
  • Donโ€™t upload huge files straight from the camera to the server. Compress the video and consider a lighter format, for example WebM, with MP4 as a fallback.
  • For YouTube or Vimeo videos, use an <iframe> with the embed code from the service, not <video>.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.