HTML Video

πŸ“Ή Introduction to HTML <video> Element

The <video> element in HTML5 allows you to embed video content directly into web pages without needing third-party plugins. It's powerful, flexible, and widely supported across modern browsers. 🌐

>>β€œThe web is no longer static β€” bring it to life with video!” πŸŽ₯

πŸ› οΈ Basic Syntax

Here's the basic structure for using the <video> tag:

Basic Video Tag

<video width="640" height="360" controls>
  <source src="movie.mp4" type="video/mp4" />
  Your browser does not support the video tag.
</video>

Note

πŸ’‘ Always include fallback text for browsers that don’t support HTML5 video.

🎞️ Video Attributes

AttributeDescription
controlsDisplays play, pause, volume, etc.
autoplayStarts playing the video automatically (muted is often required)
loopRepeats the video when it's finished
mutedStarts video with audio off (useful for autoplay)
posterImage shown before video plays
preloadIndicates if/how the browser should load the video (none, metadata, auto)

πŸ”„ Using Multiple Video Sources

To maximize compatibility across browsers, provide multiple <source> elements with different video formats.

Multiple Source Example

<video controls>
  <source src="movie.mp4" type="video/mp4" />
  <source src="movie.webm" type="video/webm" />
  <source src="movie.ogg" type="video/ogg" />
  Your browser does not support the video tag.
</video>

Note

πŸ’‘ Browsers play the first format they support. MP4 is the most widely supported.

πŸ“ Add a Poster Image

Use the poster attribute to display a preview image before the video starts.

Poster Example

<video width="320" height="240" controls poster="poster.jpg">
  <source src="video.mp4" type="video/mp4" />
</video>

🧠 Accessibility & Best Practices

  • Use captions with the <track> tag for accessibility.
  • Keep file sizes optimized for faster load times.
  • Host videos on CDNs or platforms like YouTube for better performance.
  • Avoid autoplay with sound β€” it frustrates users and is often blocked.

πŸ§ͺ Track Subtitles & Captions

Video with Subtitles

<video controls>
  <source src="movie.mp4" type="video/mp4" />
  <track src="subtitles.vtt" kind="subtitles" srclang="en" label="English" />
</video>

Note

⚠️ The subtitle file must be in the .vtt format (WebVTT).

πŸ”— Useful Resources

>>β€œA well-placed video tells a story faster than a thousand lines of code.” 🎯