The audio tag lets you add sound to your web page easily. It helps visitors listen to music, podcasts, or any audio without extra tools.
You want to play background music on a website.
You need to add a podcast episode for visitors to listen to.
You want to include sound effects for a game or interactive page.
You want to provide audio instructions or narration for accessibility.
You want visitors to control audio playback directly on the page.
Syntax
HTML
<audiosrc="path/to/audio.mp3"controls></audio>
The src attribute points to the audio file you want to play.
Adding controls shows play, pause, and volume buttons for users.
Examples
Plays the audio file with default controls visible.
HTML
<audiosrc="song.mp3"controls></audio>
Uses multiple sources for better browser support and shows a message if audio is not supported.
HTML
<audiocontrols>
<sourcesrc="audio.ogg"type="audio/ogg">
<sourcesrc="audio.mp3"type="audio/mpeg">
Your browser does not support the audio element.
</audio>
Plays audio automatically without sound (muted) when the page loads.
HTML
<audiosrc="sound.mp3"autoplaymuted></audio>
Sample Program
This page shows a heading and an audio player with controls. The audio plays a horse sound from an online source. If the browser can't play audio, it shows a message.
HTML
<!DOCTYPE html>
<htmllang="en">
<head>
<metacharset="UTF-8">
<metaname="viewport"content="width=device-width, initial-scale=1.0">
<title>Audio Tag Example</title>
</head>
<body>
<h1>Listen to this sound</h1>
<audiocontrols>
<sourcesrc="https://www.w3schools.com/html/horse.mp3"type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</body>
</html>
OutputSuccess
Important Notes
Always include the controls attribute so users can control playback.
Use multiple <source> tags with different audio formats for better browser support.
Remember to provide fallback text inside the audio tag for browsers that do not support it.
Summary
The <audio> tag adds sound to web pages easily.
Use controls to show playback buttons to users.
Multiple sources help your audio work on more browsers.
Practice
(1/5)
1. What is the main purpose of the <audio> tag in HTML?
easy
A. To create clickable buttons
B. To display images on a web page
C. To add sound or music to a web page
D. To add video content
Solution
Step 1: Understand the tag's function
The <audio> tag is designed to embed sound files in web pages.
Step 2: Compare with other media tags
Unlike <img> for images or <video> for videos, <audio> specifically handles audio content.
Final Answer:
To add sound or music to a web page -> Option C
Quick Check:
Audio tag = sound embedding [OK]
Hint: Audio tag is for sound, not images or video [OK]
Common Mistakes:
Confusing audio with video or images
Thinking audio tag creates buttons
Assuming audio tag is for text
2. Which attribute must you add to the <audio> tag to show playback controls like play and pause buttons?
easy
A. src
B. loop
C. autoplay
D. controls
Solution
Step 1: Identify attribute for controls
The controls attribute tells the browser to display play, pause, and volume buttons.
Step 2: Differentiate from other attributes
src sets the audio file, autoplay starts playing automatically, and loop repeats the audio.
3. What will happen when this HTML code runs in a modern browser?
<audio controls>
<source src="sound.mp3" type="audio/mpeg">
<source src="sound.ogg" type="audio/ogg">
Your browser does not support the audio element.
</audio>
medium
A. The audio player with controls will appear and play sound.mp3 or sound.ogg
B. Only sound.ogg will play because it is the last source
C. No audio will play because multiple sources confuse the browser
D. The text 'Your browser does not support the audio element.' will always show
Solution
Step 1: Understand multiple sources usage
The <source> tags provide alternative audio formats so browsers can pick the one they support.
Step 2: Check controls and fallback text
The controls attribute shows the player, and fallback text shows only if audio is unsupported.
Final Answer:
The audio player with controls will appear and play sound.mp3 or sound.ogg -> Option A
B. The src attribute should not be used with <source> tags inside <audio>
C. The type attribute is missing in the src attribute
D. The <source> tag must come before the src attribute
Solution
Step 1: Understand usage of src and source
When using <source> tags inside <audio>, do not use the src attribute on <audio> itself.
Step 2: Reason why mixing causes issues
Using both confuses the browser which source to play; only <source> tags or only src should be used.
Final Answer:
The src attribute should not be used with <source> tags inside <audio> -> Option B
Quick Check:
Use either src or source tags, not both [OK]
Hint: Use src or source tags, never both together [OK]
Common Mistakes:
Using src attribute with source tags
Thinking controls attribute is missing
Believing order of source and src matters
5. You want to add an audio player that automatically plays a sound once the page loads, loops it continuously, and hides controls. Which is the correct HTML code?
hard
A. <audio src="theme.mp3" autoplay loop></audio>
B. <audio src="theme.mp3" controls></audio>
C. <audio src="theme.mp3" autoplay loop controls></audio>
D. <audio src="theme.mp3" loop></audio>
Solution
Step 1: Identify attributes for autoplay and loop
The autoplay attribute starts playing automatically, and loop repeats the audio continuously.
Step 2: Check controls visibility
To hide controls, do not include the controls attribute.
Final Answer:
<audio src="theme.mp3" autoplay loop></audio> -> Option A