Home 〉 HTML Tutorials 〉 HTML Audio Tag Tutorial with Examples and Attributes | Learn HTML5 '<audio>' Element
HTML Audio Tag Tutorial with Examples and Attributes | Learn HTML5 '<audio>' Element !
Want to add audio to your website using simple HTML?
This detailed blog post on HTML audio tag explains its syntax, attributes, inline CSS styling, and how to embed audio files in web pages. It covers block-level behavior, complete examples, best practices, and related media elements. Learn how to use the '<audio>' tag efficiently with autoplay, controls, loop, and more. 👉 The complete blog post on the HTML audio tag is as follows.
The HTML '<audio>' tag allows you to embed sound content, such as music, sound effects, or voice recordings, directly into a webpage. Introduced in HTML5, the '<audio>' tag has become a standard way to handle audio playback without the need for external plugins like Flash.
With the help of various attributes, you can control how audio plays, including autoplay, looping, and preloading options. In this blog post, you will learn the syntax, usage, attributes, and best practices to use the '<audio>' tag effectively.
The '<audio>' tag is considered a block-level element because it occupies the full width of its parent container by default and can contain other nested elements like '<source>' and fallback text. It is not an empty element because it has an opening and a closing tag.
Syntax ✍
<audio controls>
<source src="audio-file.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
Here are the main attributes supported by the '<audio>' element:
Specifies the path to the audio file.
<!DOCTYPE html>
<html>
<body>
<audio src="/images/audio.mp3" controls></audio>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This plays the 'audio.mp3' file with audio controls.
Refer our blog post on 'file path' if you have your file in another directory.
Displays built-in audio controls like play, pause, and volume.
<!DOCTYPE html>
<html>
<body>
<audio src="/images/audio.mp3" controls></audio>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Enables UI controls for audio playback.
Plays audio automatically once the page loads.
<!DOCTYPE html>
<html>
<body>
<audio src="/images/audio.mp3" controls autoplay></audio>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Audio will play without user interaction (may be blocked by some browsers).
Makes the audio play repeatedly.
<!DOCTYPE html>
<html>
<body>
<audio src="/images/audio.mp3" loop controls></audio>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Audio restarts automatically after it ends.
Starts the audio in a muted state.
<!DOCTYPE html>
<html>
<body>
<audio src="/images/audio.mp3" muted controls></audio>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Useful when autoplaying audio in a muted state.
Hints the browser on how to load the audio file: 'auto', 'metadata', or 'none'.
<!DOCTYPE html>
<html>
<body>
<audio src="/images/audio.mp3" preload="metadata" controls></audio>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Loads only metadata (e.g., duration) before playing.
Here's an example of adding some inline styles to the '<audio>' tag:
<!DOCTYPE html>
<html>
<body>
<audio src="/images/audio.mp3" controls
style="width: 300px; padding: 5px; background-color: #f4f4f4; border: 1px solid #ccc; border-radius: 8px;">
</audio>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
The HTML '<audio>' tag makes it easy to add sound to your website without needing plugins. By using attributes like 'controls', 'autoplay', and 'loop', you can enhance the user experience. Inline CSS can further style the audio player to match your site's design.
Whether you're creating an online podcast, adding sound effects, or embedding music, the '<audio>' tag is a powerful and flexible tool for web development.
Yes, you can add multiple '<source>' elements to support different browsers.
Fallback text will appear, which you can define between opening and closing '<audio>' tags.
No. Modern browsers often block autoplay unless the audio is muted.
Not fully. You need to use CSS for better responsiveness and styling.