HTML provides built-in elements to play audio and video directly inside a web page without using external plugins. The
<audio> and <video> elements are supported by all modern web browsers.
<audio> and
<video> elements to easily embed multimedia on web pages.
The
<audio>
element is used to add audio files to a webpage.
<audio controls> <source src="music.mp3" type="audio/mpeg"> Your browser does not support the audio tag. </audio>
The controls attribute displays Play, Pause, Volume and Download controls.
<audio controls> <source src="song.mp3" type="audio/mpeg"> </audio>
Output:
| Attribute | Description |
|---|---|
| controls | Displays audio controls. |
| autoplay | Starts audio automatically. |
| loop | Repeats the audio continuously. |
| muted | Starts audio in muted mode. |
| preload | Loads the audio before playing. |
<audio controls autoplay loop muted> <source src="music.mp3" type="audio/mpeg"> </audio>
This example automatically starts the audio, keeps repeating it and starts in muted mode.
| Format | MIME Type | Browser Support |
|---|---|---|
| MP3 | audio/mpeg | ✅ All Modern Browsers |
| WAV | audio/wav | ✅ Most Browsers |
| OGG | audio/ogg | ✅ Chrome, Firefox, Edge |
The
<video>
element is used to display videos.
<video width="400" controls> <source src="movie.mp4" type="video/mp4"> Your browser does not support the video tag. </video>
<video width="400" controls> <source src="sample.mp4" type="video/mp4"> </video>
Output:
| Attribute | Description |
|---|---|
| controls | Displays video controls. |
| autoplay | Starts the video automatically. |
| loop | Repeats the video. |
| muted | Starts the video without sound. |
| poster | Displays an image before the video starts. |
| width | Sets the video width. |
| height | Sets the video height. |
You can provide multiple video formats so the browser can choose the supported one.
<video controls> <source src="movie.mp4" type="video/mp4"> <source src="movie.ogg" type="video/ogg"> Your browser does not support HTML video. </video>
<video width="500" controls poster="poster.jpg"> <source src="movie.mp4" type="video/mp4"> </video>
The poster attribute displays an image before the user plays the video.
You can embed a YouTube video using the
<iframe> element.
<iframe width="560" height="315" src="https://www.youtube.com/embed/VIDEO_ID" allowfullscreen> </iframe>
| Format | MIME Type | Browser Support |
|---|---|---|
| MP4 | video/mp4 | ✅ All Modern Browsers |
| WebM | video/webm | ✅ Chrome, Firefox, Edge |
| Ogg | video/ogg | ✅ Firefox, Opera |
| Audio | Video |
|---|---|
| Plays only sound. | Plays sound and visuals. |
| Uses <audio> tag. | Uses <video> tag. |
| Smaller file size. | Larger file size. |
| Suitable for music and podcasts. | Suitable for tutorials, movies and presentations. |
Which HTML element is used to display a video?