HTML provides the <audio>
and <video>
tags for embedding multimedia content directly into web pages. These tags support various media formats and offer attributes to control playback, display, and accessibility. This article demonstrates how to use these tags with examples.
The <audio>
tag allows you to embed audio files in HTML documents. Common audio formats include .mp3
, .wav
, and .ogg
. The <audio>
tag also includes several useful attributes, such as:
controls
: Displays audio controls for the user.autoplay
: Starts playing the audio automatically (not recommended due to user experience concerns).loop
: Replays the audio in a loop.muted
: Starts the audio in a muted state.Example of Using <audio> Tag:
<audio controls> <source src="audiofile.mp3" type="audio/mpeg"> <source src="audiofile.ogg" type="audio/ogg"> Your browser does not support the audio element. </audio>
In this example, the <audio>
tag includes two <source>
tags for different audio formats. If the browser doesn't support one format, it will try the other.
The <video>
tag is used to embed video files in HTML documents. Common video formats include .mp4
, .webm
, and .ogg
. Similar to the <audio>
tag, the <video>
tag supports several attributes:
controls
: Displays video controls, such as play, pause, and volume.autoplay
: Starts playing the video automatically (use cautiously as it may disrupt user experience).loop
: Replays the video in a loop.muted
: Starts the video muted.poster
: Displays an image before the video starts playing.Example of Using <video> Tag:
<video controls width="600" poster="poster.jpg"> <source src="videofile.mp4" type="video/mp4"> <source src="videofile.ogg" type="video/ogg"> Your browser does not support the video element. </video>
In this example, the controls
attribute enables video controls, and the poster
attribute specifies an image to display before the video plays. The browser will use videofile.mp4
or videofile.ogg
based on its compatibility.
Both <audio>
and <video>
tags support the autoplay
and loop
attributes. The autoplay
attribute starts playing the media automatically, while the loop
attribute allows the media to play continuously.
Example of Autoplay and Loop:
<audio autoplay loop> <source src="background-music.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> <video autoplay loop muted> <source src="background-video.mp4" type="video/mp4"> Your browser does not support the video element. </video>
Here, the audio will play in a loop continuously. For the video, autoplay
starts playback, loop
repeats it, and muted
prevents audio from playing on page load (a common requirement for autoplaying videos).
Below is an example that uses both the <audio>
and <video>
tags within the same page:
<h2>Audio Example</h2> <audio controls> <source src="song.mp3" type="audio/mpeg"> <source src="song.ogg" type="audio/ogg"> Your browser does not support the audio element. </audio> <h2>Video Example</h2> <video controls width="600" poster="thumbnail.jpg"> <source src="movie.mp4" type="video/mp4"> <source src="movie.webm" type="video/webm"> Your browser does not support the video element. </video>
The <audio>
and <video>
tags in HTML make it easy to embed multimedia content on a webpage. By using attributes like controls
, autoplay
, loop
, and muted
, you can control how the media behaves, enhancing the user experience.