The audio element is used to embed sound content in web pages. It allows you to include audio files directly into your HTML documents and provides several attributes to control audio playback.
The audio element comes with several attributes to help you control the playback and appearance of your audio content:
Attribute | Description | Example |
---|---|---|
src | Specifies the URL of the audio file. |
Try yourself
|
controls | Specifies that audio controls should be displayed (such as play/pause buttons). |
Try yourself
|
autoplay | Specifies that the audio will start playing as soon as it is ready. |
Try yourself
|
loop | Specifies that the audio will start over again, every time it is finished. |
Try yourself
|
Below are examples of how you can use the audio element in your HTML documents:
Example: Basic Audio
<audio controls>
<source src="/media/audio/sample.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
This example shows an audio element with basic controls for play, pause, and volume adjustment.
<audio src="/media/audio/sample.mp3" autoplay></audio>
This example shows an audio element that starts playing automatically when the page loads.
Here are some important notes and best practices for using the audio element: