Codectionary / Developer documentation / HTML

<audio>

The <audio> element embeds sound content — music, podcasts, or sound effects — directly in a page. Like <video>, it supports multiple <source> elements for format fallback and built-in playback controls, without needing Flash or other plugins.

Syntax

<audio src="song.mp3" controls></audio>

Examples

Basic Audio Player

An audio player with standard controls.

<audio controls>
  <source src="podcast.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>

Multiple Format Fallback

Providing both MP3 and OGG formats for compatibility.

<audio controls>
  <source src="track.ogg" type="audio/ogg">
  <source src="track.mp3" type="audio/mpeg">
</audio>

Looping Sound Effect

A muted-by-default, loopable audio clip.

<audio id="notification" src="ding.mp3" preload="auto"></audio>
<button onclick="document.getElementById('notification').play()">Play Sound</button>

Attributes

  • controls (boolean): Shows the browser's built-in playback controls
  • autoplay (boolean): Starts playback automatically (subject to browser autoplay restrictions)
  • loop (boolean): Restarts playback automatically when it ends
  • preload (none | metadata | auto): Hints how much of the file to preload before playback

Best practices

  • Always show controls unless the audio is purely decorative (like a UI sound effect triggered by JavaScript)
  • Provide MP3 and OGG sources together for the widest browser compatibility
  • Be mindful of autoplay — most browsers block auto-playing audio with sound unless the user has interacted with the page
  • Use preload="none" for pages with many audio elements to avoid unnecessary bandwidth usage

At a glance

Purpose
Structure and meaning for web content
File extension
.html · .htm
Runs in
Web browsers
Usually used with
CSS and JavaScript

Specifications & further reading

Related HTML documentation