<audio>

An audio player: a podcast, a narration, a sound effect.

Display
inline (invisible without `controls`)
Closing tag
Required
Permitted content
<source> and <track> elements, followed by fallback content
On this page

Example

#
HTML
<p>Episode 12: How not to get stuck in tutorials</p>
<audio controls preload="none">
  <source src="/podcast/odcinek-12.mp3" type="audio/mpeg">
  <a href="/podcast/odcinek-12.mp3">Download the episode as MP3</a>
</audio>
Preview

The audio file is skipped in the preview, so the player shows only the controls.

Definition and usage

#

The <audio> element embeds an audio file in the page. You give the address in the src attribute, or in several <source> elements if you want to offer different formats. The safest choice is MP3, which every modern browser can play.

Without the controls attribute the player is invisible, and the sound can be started only by a script. With controls the browser shows its own bar with a play button, a timeline, and volume control.

Browsers usually block autoplaying sound until the user clicks something on the page, so autoplay often won’t work. That is a good thing: unexpected sound is one of the most annoying things a page can do. The content between the tags is shown only by browsers that don’t support <audio> at all.

Syntax

#
Syntax
<audio src="recording.mp3" controls></audio>

Attributes

#
  • src

    URL

    The address of the audio file. Instead of it you can put several <source> elements with different formats inside.
  • controls

    boolean attribute

    Shows the built-in player controls. Without it the element is completely invisible, as you can see in the second part of the example.
  • autoplay

    boolean attribute

    Tries to play the sound right after loading. Browsers usually block this until the user interacts with the page, so don’t rely on this attribute.
  • loop

    boolean attribute

    When the recording reaches the end, it starts again. Useful, for example, for background music in a game.
  • muted

    boolean attribute

    Starts the player with the sound muted. The user can turn it on in the controls.
  • preload

    none | metadata | auto

    A hint about how much data to download before playback. none downloads nothing, metadata only information about the file (for example its length), and auto allows downloading the whole file. The browser may ignore this hint.

The element also supports all global attributes, such as class, id, style and hidden.

Attributes in practice

#

src

URL

The address of the audio file. Instead of it you can put several <source> elements with different formats inside.

HTML
<audio src="/lekcje/wymowa-terminow.mp3" controls preload="none"></audio>
Preview

The audio file is skipped in the preview.

controls

boolean attribute

Shows the built-in player controls. Without it the element is completely invisible, as you can see in the second part of the example.

HTML
<p>With controls:</p>
<audio src="/lekcje/intro.mp3" controls preload="none"></audio>
<p>Without controls (the player is there, but you can’t see it):</p>
<audio src="/lekcje/intro.mp3" preload="none"></audio>
Preview

The audio file is skipped in the preview.

Default CSS

#

Most browsers display this element with these default styles:

CSS
audio:not([controls]) {
  display: none !important;
}

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Include a transcript of the recording, for example under the player or in a <details>. It helps deaf and hard of hearing people and everyone who prefers to read.
  • Set preload="none" or preload="metadata" for recordings the user may never play. The page then won’t download the whole file in advance.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.