The audio tag
We made a short doorbell-style chime for this page, about three seconds long. Press play:
src is the address of the sound file, the same way an image has one. controls asks the browser
for its own player: play and pause, the time, a progress bar and the volume. Each browser draws it a bit differently,
and you don’t have to build any of it. The closing </audio> is required. The CSS line is for small
phones: Chrome’s player is 300px wide, and max-width: 100% lets it shrink to fit.
The examples run in a locked box: scripts are off, nothing is sent anywhere, and your edits go away when you reload. To keep one, copy it into the HTML Editor.
No controls, no player
This is the most common “my audio doesn’t work” question. An audio tag without controls takes up no
space at all. Browsers hide it, because there’s nothing to show:
The second audio tag is there, between the two middle lines of text. It just has no player. An invisible audio tag only makes sense when your own JavaScript starts the sound, like the custom play button further down.
MP3, Ogg, WAV: which format?
MP3 plays in every current browser, so it’s the safe choice. The others, briefly:
- AAC (usually a
.m4afile) also plays everywhere, and often sounds a little better than MP3 at the same size. - Ogg (Vorbis or Opus inside) plays in Chrome, Edge and Firefox, and in newer Safari versions. Opus is very good for speech.
- WAV plays everywhere too, but it isn’t compressed. One minute of CD-quality stereo is about 10 MB, against about 1 MB for a 128 kbps MP3.
To offer two formats, swap src for <source> tags inside the audio tag. The browser
plays the first one it can, and the type lets it skip the others without downloading them:
Chrome picks the Ogg file here, and a browser without Ogg support moves on to the MP3. The type for MP3 is
audio/mpeg, not audio/mp3. For most sites, one MP3 is enough. Two formats are
worth it when you have a lot of audio and want smaller files for most visitors.
Autoplay and background music
Lots of people want music to start when the page opens. Here’s the code they try:
<!-- What people try. With sound, browsers block it. -->
<audio src="background-music.mp3" autoplay loop></audio>
In most cases nothing happens. Chrome, Firefox and Safari block sound that starts before the visitor has done
anything on the page. Chrome makes exceptions, for example after someone has clicked on your page, or on a site they
use a lot, but a first-time visitor almost never hears it. Adding muted lets autoplay through, but a
muted song isn’t much use.
It’s also a matter of manners. Sound out of nowhere startles people, and for someone using a screen reader it talks over their reader. The accessibility guidelines (WCAG, “Audio Control”) say any sound that plays by itself for more than 3 seconds needs a way to pause or stop it, or a volume control just for that sound. So the better pattern is a visible player or a play button, and the visitor decides.
Loop, muted and preload
Three more attributes cover most of what you’ll need. Press play on the first player and it keeps going round:
loopstarts the sound again when it ends. Good for a short ambient sound, tiring for a song.mutedstarts with the volume off. The player shows a crossed-out speaker, and people can turn it on.-
preload="none"loads nothing until someone presses play, which saves data on a page with many players.preload="metadata"loads only the length, andpreload="auto"lets the browser fetch the whole file early if it wants to.
Style the audio player
The browser’s player is a built-in piece of the browser, so CSS can’t reach much of it. You can change its size and
where it sits. Chrome draws it 300px wide by default, and width: 100% stretches it to fill its box. Here
it sits in a dark card:
color-scheme: dark gives you a dark player in Chrome. Other browsers may keep their light one, so check
it where your visitors are. For anything more, like your own colors, a round play button or a waveform, you hide the
built-in player and make your own controls with JavaScript.
Your own play button
A custom button needs a few lines of JavaScript: one click plays the sound, the next one pauses it, and the button’s
text follows along. The audio tag has no controls, so it stays hidden:
<button type="button" id="play-btn">Play the chime</button>
<audio id="chime" src="chime.mp3"></audio>
<script>
const audio = document.getElementById('chime');
const btn = document.getElementById('play-btn');
btn.addEventListener('click', () => {
if (audio.paused) {
audio.play();
} else {
audio.pause();
}
});
audio.addEventListener('play', () => { btn.textContent = 'Pause'; });
// Fires on pause and when the sound ends, too.
audio.addEventListener('pause', () => { btn.textContent = 'Play the chime'; });
</script>
Because the sound starts from a click, the browser allows it. A real <button> works with the
keyboard and screen readers without any extra work, and the text change tells people what the next click does. The
script can’t run in the example boxes on this page. Paste it into the HTML Editor and
change chime.mp3 to https://htmlviewer.tech/samples/chime.mp3 to hear it.
A download link and fallback text
Text inside the audio tag is only shown by a browser that can’t play audio at all, which is very rare today. A download link next to the player is more useful: people can save a podcast episode for the train.
The download attribute asks the browser to save the file instead of opening it. It only works for files
on your own site; for a file on another site, the browser opens it as usual. (Downloads are switched off in the
example boxes here, so try the link on your own page.)
Transcripts for spoken audio
People who can’t hear the audio need the words in another form. For a video you’d add captions, but browsers don’t
show captions for an audio tag, since there’s no picture to draw them on. Use a transcript instead: the same words as
text, right under the player. A <details> box keeps a long one out of the way:
Click “Read the transcript” to open it. For a podcast, put in who’s speaking and describe sounds that matter, in square brackets. A transcript also helps search engines and anyone who’d rather skim than listen. If you really want captions on the screen, use a video tag with a track and a still image. The accordion tutorial has more on details and summary.
SoundCloud and Spotify players
If your audio already lives on SoundCloud, Spotify or a podcast host, you don’t need the audio tag. Those sites give
you an <iframe> embed code (look for Share, then Embed). It looks roughly like this, with the id
from your own share link:
<iframe
src="https://open.spotify.com/embed/episode/EPISODE_ID"
title="Coffee Talk, episode 1"
width="100%" height="152" loading="lazy"
allow="encrypted-media"
style="border: 0; border-radius: 12px;"></iframe> Copy the exact code the site gives you, because each service has its own address and options. Their player loads from their site, with their look and their cookies, so mention it in your privacy policy. The examples on this page don’t load players from other sites, so there’s no live one here.
Audio not playing? Check these
- You can’t see a player. Add
controls. Without it, the audio tag is invisible. - It should start on its own. Autoplay with sound is blocked. Let people press play.
- The path is wrong.
Music/Song.MP3andmusic/song.mp3are different files on most servers. Paste the file’s address into a new tab: if it doesn’t play there, the page can’t play it either. The image tutorial explains file paths. - The format. A
.wmaor.aifffile won’t play in most browsers. Convert it to MP3. - The volume. It sounds silly, but check that the player, the browser tab and the device aren’t muted.
A
mutedattribute left in the code does it too.
Sound and pictures together? The HTML video tutorial uses the same attributes with
the <video> tag, plus YouTube embeds.
Questions people ask
How do I add audio to an HTML page?
Use the audio tag with the address of an MP3 file and the controls attribute: <audio src="chime.mp3" controls></audio>. The browser shows a small player with a play button, a progress bar and the volume.
Why can’t I see my audio player?
The controls attribute is missing. An audio tag without controls takes up no space and shows nothing, even though it’s on the page. Write <audio src="song.mp3" controls></audio>, or start the sound from your own button with JavaScript.
How do I autoplay audio or add background music to a website?
You mostly can’t. Chrome, Firefox and Safari block sound that starts before the visitor has done anything on the page, so <audio autoplay> usually stays silent. Chrome makes exceptions for sites people use a lot or after a click, but first-time visitors rarely hear it. Show a player or a play button and let people choose.
Which audio format works in all browsers?
MP3 plays in every current browser, and so does AAC (.m4a). Ogg (Vorbis or Opus) plays in Chrome, Edge, Firefox and newer Safari versions. WAV works everywhere but isn’t compressed, so a minute of it is about 10 MB. One MP3 file is enough for most sites.
How do I loop audio in HTML?
Add the loop attribute: <audio src="rain.mp3" controls loop></audio>. When the sound ends, it starts again from the beginning until someone presses pause.
Can I change how the audio player looks?
Only a little. CSS can set its width (audio { width: 100%; }) and position, and in Chrome color-scheme: dark gives a dark player. For your own colors and buttons, leave out controls and build the controls with a <button> and a few lines of JavaScript that call audio.play() and audio.pause().
How do I make a play button for a sound?
Add a hidden audio tag and a button: <button id="play-btn">Play</button> <audio id="chime" src="chime.mp3"></audio>. In JavaScript, on click call audio.play() if audio.paused is true, otherwise audio.pause(). Because the sound starts from a click, browsers allow it.
How do I add a download link for an audio file?
Put a normal link next to the player and add the download attribute: <a href="episode-1.mp3" download>Download the episode</a>. The download attribute only works for files on your own site; for files elsewhere the browser opens them instead.
Can I add captions to an audio tag?
Browsers don’t show captions for audio, because there’s no picture area to draw them on. Add a transcript instead: the words as text under the player, for example inside a <details> box. If you need captions on screen, use a video tag with a still image and a <track> file.