HTML <video> Tag โ Playing Videos
๐ Introduction
The <video> tag is used to embed and play video files in a webpage.
It allows users to:
- Watch videos directly in the browser
- Control playback (play, pause, volume)
- Use multiple video formats
๐งฑ Basic Syntax
<video src="video.mp4" controls></video>
๐ก Example
<!DOCTYPE html>
<html>
<head>
<title>Video Example</title>
</head>
<body>
<h1>My Video</h1>
<video src="movie.mp4" controls width="400"></video>
</body>
</html>
๐ฅ๏ธ Output Explanation
- The video appears in the browser
controlsadds play/pause buttons- Users can interact with the video
๐ฏ Important Attributes
1. controls
<video src="video.mp4" controls></video>
โ Adds play, pause, volume, fullscreen buttons
2. width / height
<video src="video.mp4" controls width="500"></video>
โ Controls video size
3. autoplay
<video src="video.mp4" autoplay></video>
โ ๏ธ Starts video automatically (often muted by browsers)
4. muted
<video src="video.mp4" autoplay muted></video>
โ Required for autoplay in many browsers
5. loop
<video src="video.mp4" controls loop></video>
โ Repeats video automatically
๐ก Better Practice: Multiple Formats
Different browsers support different formats:
<video controls width="400">
<source src="video.mp4" type="video/mp4" />
<source src="video.webm" type="video/webm" />
Your browser does not support the video tag.
</video>
โ More compatible across browsers
๐ฏ When to Use <video>
Use it for:
- Tutorials
- Ads
- Product demos
- Background videos
- Educational content
โ ๏ธ Important Notes
- Always include
controlsfor usability - Use optimized video sizes
- Avoid heavy videos without compression
๐ซ Common Mistakes
- Missing
controlsโ - Using only one format โ
- Large unoptimized videos โ
- Forgetting fallback text โ
๐จ Styling Example
<video controls width="300" style="border-radius:10px;">
<source src="video.mp4" type="video/mp4" />
</video>
๐งช Mini Exercise
Create a page with:
A video with controls
A second video that:
- Autoplays (muted)
- Loops
Add a fallback message