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
| Attribute | Description |
|---|---|
| controls | Displays play, pause, volume, etc. |
| autoplay | Starts playing the video automatically (muted is often required) |
| loop | Repeats the video when it's finished |
| muted | Starts video with audio off (useful for autoplay) |
| poster | Image shown before video plays |
| preload | Indicates 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.β π―