HTML5 Video
Overview
The HTML5 <video> element provides a standard way to embed video in webpages without relying on third-party plugins (like Flash).
Basic Syntax
html
<video src="movie.mp4" controls>
Your browser does not support the video tag.
</video>Video Attributes
Common Attributes
html
<video
src="movie.mp4"
width="640"
height="360"
controls
autoplay
loop
muted
poster="thumbnail.jpg"
preload="auto">
Your browser does not support the video tag.
</video>Attributes:
controls- Display playback controlsautoplay- Auto-play (most browsers require muted)loop- Loop playbackmuted- Mute audioposter- Video thumbnailpreload- Preload strategy (none/metadata/auto)widthandheight- Video dimensions
Multiple Format Support
Provide multiple video formats for cross-browser compatibility:
html
<video width="640" height="360" 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>Video Formats
| Format | MIME Type | Browser Support |
|---|---|---|
| MP4 | video/mp4 | Chrome, Firefox, Safari, Edge |
| WebM | video/webm | Chrome, Firefox, Opera |
| Ogg | video/ogg | Firefox, Opera |
JavaScript Control
Basic Control
html
<video id="myVideo" width="640" height="360">
<source src="movie.mp4" type="video/mp4">
</video>
<button onclick="document.getElementById('myVideo').play()">Play</button>
<button onclick="document.getElementById('myVideo').pause()">Pause</button>
<script>
var video = document.getElementById("myVideo");
function playVideo() {
video.play();
}
function pauseVideo() {
video.pause();
}
</script>Video Events
html
<script>
video.addEventListener("play", function() {
console.log("Video started playing");
});
video.addEventListener("pause", function() {
console.log("Video paused");
});
video.addEventListener("ended", function() {
console.log("Video ended");
});
video.addEventListener("timeupdate", function() {
console.log("Current time: " + video.currentTime);
});
</script>Responsive Video
html
<style>
.video-wrapper {
position: relative;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
height: 0;
overflow: hidden;
}
.video-wrapper video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
<div class="video-wrapper">
<video controls>
<source src="movie.mp4" type="video/mp4">
</video>
</div>Subtitles Support
html
<video width="640" height="360" controls>
<source src="movie.mp4" type="video/mp4">
<track
kind="subtitles"
src="subtitles_en.vtt"
srclang="en"
label="English"
default>
<track
kind="subtitles"
src="subtitles_zh.vtt"
srclang="zh"
label="Chinese">
</video>Best Practices
- Provide multiple formats for cross-browser compatibility
- Use poster attribute for video thumbnails
- Optimize file size for faster loading
- Avoid autoplay unless necessary and muted
- Provide subtitles for accessibility
- Responsive design for different screen sizes
- Provide fallback content for unsupported browsers
Summary
HTML5 Video element provides a powerful and flexible solution for video playback on webpages. By properly using its attributes and JavaScript API, you can create feature-rich video experiences.