The HTML audio element embeds playable audio tracks—such as podcast episodes, pronunciation clips, or music previews—directly into a web page. Browsers provide accessible keyboard-friendly playback controls out of the box when you add the controls attribute.
Just like video, placing multiple source tags inside audio lets every browser pick the first audio codec it recognizes. MP3 (audio/mpeg) works across virtually all modern devices, while Ogg Vorbis (audio/ogg) or Opus (audio/ogg; codecs=opus) provides compact open-source compression.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML Audio Player Example</title>
<style>
.audio-card { max-width: 440px; padding: 16px; border: 1px solid #cbd5e1; border-radius: 8px; font-family: system-ui, sans-serif; }
audio { width: 100%; margin-top: 8px; }
</style>
</head>
<body>
<figure class="audio-card">
<figcaption>Episode 12: Clean HTML Semantics</figcaption>
<audio controls preload="metadata">
<source src="/media/sample-audio.mp3" type="audio/mpeg">
<source src="/media/sample-audio.ogg" type="audio/ogg">
Your browser does not support native HTML5 audio.
</audio>
</figure>
</body>
</html>
Set preload="none" on pages containing a list of twenty audio clips so mobile visitors do not download megabytes of unused audio on initial page load. Use preload="metadata" when showing track length before playback starts.
Avoid autoplay on audio elements altogether—modern browsers block unmuted autoplay until the visitor interacts with the page, and unexpected sound irritates users.
controls on visible players: Without controls (or custom JavaScript buttons using .play() and .pause()), the audio player stays completely invisible..wav files: Always encode web audio as compressed MP3, AAC, or Opus to cut file size by 80%–90%.Yes. Leave off the controls attribute and control the audio element via the JavaScript HTMLMediaElement methods .play(), .pause(), and currentTime.
MP3 (type="audio/mpeg") works natively across all modern desktop and mobile browsers.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.
You've completed this section! Take a quick 5-question quiz to check your understanding.