What is a Video Tag
Syntax of the video tag
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Video Example</title>
</head>
<body>
<video src="video1.mp4" controls width="640" height="360"></video>
</body>
</html>
Explain the code
Attributes | Description | Basic Syntax | Type | Note |
---|---|---|---|---|
src |
Specifies the URL of the video file to be played. | <video src="movie.mp4"></video> |
This attribute can be omitted if you use the <source> tag to define multiple video formats. |
|
controls |
Adds playback controls like play, pause, volume, etc., to the video player. | <video controls src="movie.mp4"></video> |
Boolean attribute | Does not require a value; simply adding it enables controls. |
autoplay |
The video automatically starts playing as soon as it is ready. | <video autoplay src="movie.mp4"></video> |
Boolean attribute | Some browsers may block autoplay, especially if the video has sound. |
loop |
The video will start over again, looping indefinitely, once it reaches the end. | <video loop src="movie.mp4"></video> |
Boolean attribute | |
muted |
The video will be muted by default when it starts playing. | <video muted src="movie.mp4"></video> |
Boolean attribute | |
poster |
Specifies an image to be shown while the video is loading, before the user plays it, or if the video fails to load. | <video poster="image.jpg" src="movie.mp4"></video> |
The poster image will be replaced by the first frame of the video once it starts playing. | |
preload |
Provides a hint to the browser about whether the video data should be preloaded or not. |
<video preload="auto" src="movie.mp4"></video>
|
|
|
width & height |
Specifies the width and height of the video player in pixels. | <video width="640" height="360" src="movie.mp4"></video> |
Number (pixels) | If not specified, the video will use its intrinsic dimensions. |
playsinline |
Ensures that the video plays inline within the webpage, instead of in fullscreen, on mobile devices. | <video playsinline src="movie.mp4"></video> |
Boolean attribute | Especially useful on iOS devices. |
crossorigin |
Specifies how the video file should be handled in terms of CORS (Cross-Origin Resource Sharing). | <video crossorigin="anonymous" src="movie.mp4"></video> |
|
This attribute is particularly useful when the video is hosted on a different domain. |
Use all attributes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Video Example</title>
</head>
<body>
<video
src="video1.mp4"
controls
width="640"
height="360"
autoplay
loop
muted
poster="poster-image.jpg"
preload="auto"
playsinline
crossorigin="anonymous">
Your browser does not support the video tag.
</video>
</body>
</html>