HTML5 Reference

HTML <audio> tag

Definition

The <audio> tag is used to is used to embed audio content into a HTML document.

The <audio> tag can be used in conbination with the <source> element to provide a list of audios where the first valid one is played, or standalone.

The <audio> tag was introduced in HTML5.

Examples



<audio controls>
  <source src="../../pages/bensound-summer.mp3" type="audio/ogg">
  <source src="../../pages/bensound-summer.mp3" type="audio/mpeg">
</audio>

Royalty Free Music from Bensound

Attributes

The following attributes can be used with the <audio> tag.


<audio> Specific Attributes
Attribute Value Description Example
autoplay empty string or autoplaySpecifies the audio will play as soon as it is ready.

<audio autoplay>Audio content</audio>
<audio autoplay="autoplay">Audio content</audio>
controls empty string or controlsSpecifies whether to display audio controls such as stop/play/pause.

<audio controls>Audio content</audio>
<audio controls="controls">;Audio content</audio>
loop empty string or loopSpecifies whether to continue playing the audio in an endless loop.

<audio loop>Audio content</audio>
<audio loop="loop">;Audio content</audio>
muted empty string or mutedSpecifies whether the audio is muted.

<audio muted>Audio content</audio>
<audio muted="muted">;Audio content</audio>
preloadauto

metadata

none
Specifies if the audio should be preloaded and if so, how.

<audio preload="auto">;Audio content</audio>
srcurl

Required when source tag nested in audio
Specifies the URL location of an audio file.

<audio src="../../pages/bensound-summer.mp3">;Audio content</audio>
<audio> Global Attributes
Attribute Description Example
Common
classSpecifies a classname for the element allowing you to apply the style of the predefined class to the content.

<audio class="name">Audio content</audio>
idSpecifies a unique id for the element allowing you to apply the style of the predefined id to the content.

<audio id="name">Audio content</audio>
styleSpecifies an inline style for the element allowing you to apply the style to the contentt.

<audio style="color:red;">Audio content</audio>
titleSpecifies an inline style for the element allowing you to apply the style to the content.

<audio title="Content info">Audio content</audio>
Keyboard
accesskeySpecifies a keyboard shortcut to associate with the element.

<audio accesskey="a">Audio content</audio>
tabindexSpecifies a tab order for the element.

<audio tabindex="1">Audio content</audio>
Language
dirSpecifies the directional flow of the content.

<!-- The text will flow from left to right -->
<audio dir="ltr">Audio content</audio>
<!-- The text will flow from right to left -->
<audio dir="rtl">Audio content</audio>
langSpecifies a language code for the content of the element.

<audio lang="en">Audio content</audio>
spellcheckSpecifies an inline style for the element allowing you to apply the style to the contentt.

<!-- Valid values true and false. -->
<!-- Default inherited/browser specific. -->
<audio spellcheck="true">Audio content</audio> 
translateSpecifies an inline style for the element allowing you to apply the style to the content.

<!-- Valid values yes and no. -->
<!-- Default yes. -->
<audio translate="no">Audio content</audio> 
Miscellaneous
contenteditableSpecifies whether the content of the element is editable.

<!-- Valid values true and false. -->
<!-- Default inherited. -->
<audio contenteditable="true">Audio content</audio> 
draggableSpecifies whether the element is draggable.

<!-- Valid values true and false. -->
<!-- Default browser specific. -->
<audio draggable="true">Audio content</audio>
hiddenSpecifies whether the element is not yet, or no longer, relevant.

<!-- Valid values an empty string or hidden -->
<audio hidden>Audio content</audio> 
<audio hidden="hidden">Audio content</audio> 
<audio> Event Attributes
Attribute Description Example
Document Element
oncopyThe script to be run when the user copies the content of an element.

 <!-- Executes go() function -->
<audio oncopy="go()">Audio content</audio> 
oncutThe script to be run when the user cuts the content of an element.

 <!-- Executes go() function -->
<audio oncut="go()">Audio content</audio>
onpasteThe script to be run when the user pastes some content into an element.

 <!-- Executes go() function -->
<audio onpaste="go()">Audio content</audio>
Drag and Drop
ondragThe script to be run when an element is dragged.

 <!-- Executes go() function -->
<audio ondrag="go()">Audio content</audio>
ondragendThe script to be run when an element has stopped being dragged.

 <!-- Executes go() function -->
<audio ondragend="go()">Audio content</audio>
ondragenterThe script to be run when an element has been dragged to a valid drop target.

 <!-- Executes go() function -->
<audio ondragenter="go()">Audio content</audio>
ondragleaveThe script to be run when an element leaves a valid drop target.

 <!-- Executes go() function -->
<audio ondragleave="go()">Audio content</audio>
ondragoverThe script to be run when an element is being dragged over a valid drop target.

 <!-- Executes go() function -->
<audio ondragover="go()">Audio content</audio>
ondragstartThe script to be run at the start of a drag operation.

 <!-- Executes go() function -->
<audio ondragstart="go()">Audio content</audio>
ondropThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<audio ondrop="go()">Audio content</audio>
Form
onblurThe script to be run when the element loses focus.

 <!-- Executes go() function -->
<audio onblur="go()">Audio content</audio>
onchangeThe script to be run when object changed and attempt to leave field.

 <!-- Executes go() function -->
<audio onchange="go()">Audio content</audio>
oncontextmenuThe script to be run when a context menu is triggered.

 <!-- Executes go() function -->
<audio oncontextmenu="go()">Audio content</audio>
onfocusThe script to be run when the element gets focus.

 <!-- Executes go() function -->
<audio onfocus="go()">Audio content</audio>
oninputThe script to be run when an element gets user input.

 <!-- Executes go() function -->
<audio oninput="go()">Audio content</audio>
oninvalidThe script to be run when an element is invalid.

 <!-- Executes go() function -->
<audio oninvalid="go()">Audio content</audio>
onresetThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<audio onreset="go()">Audio content</audio>
onselectThe script to be run when some or all of the contents of an object are selected.

 <!-- Executes go() function -->
<audio onselect="go()">Audio content</audio>
onsubmitThe script to be run when a form is submitted.

 <!-- Executes go() function -->
<audio onsubmit="go()">Audio content</audio>
Keyboard
onkeydownThe script to be run when an element is in focus and keyboard key is pressed down.

 <!-- Executes go() function -->
<audio onkeydown="go()">Audio content</audio>
onkeypressThe script to be run when an element is in focus and keyboard key is pressed down and released.

 <!-- Executes go() function -->
<audio onkeypress="go()">Audio content</audio>
onkeyupThe script to be run when an element is in focus and keyboard key is released.

 <!-- Executes go() function -->
<audio onkeyup="go()">Audio content</audio>
Media
onabortThe script code to be run on abort.

 <!-- Executes go() function -->
<audio onabort="go()">Audio content</audio>
oncanplayThe script to be run when a file has buffered enough so it is ready to start playing.

 <!-- Executes go() function -->
<audio oncanplay="go()">Audio content</audio>
oncanplaythroughThe script to be run when a file can be played all the way to the end without further need of buffering.

 <!-- Executes go() function -->
<audio oncanplaythrough="go()">Audio content</audio>
oncuechangeThe script to be run when the cue changes when using the track element.

 <!-- Executes go() function -->
<audio oncuechange="go()">Audio content</audio>
ondurationchangeThe script to be run when the length of the media is changed.

 <!-- Executes go() function -->
<audio ondurationchange="go()">Audio content</audio>
onemptiedThe script to be run when a media resource element suddenly becomes empty, usually due to an error.

 <!-- Executes go() function -->
<audio onemptied="go()">Audio content</audio>
onendedThe script to be run when the media has reach the end.

 <!-- Executes go() function -->
<audio onended="go()">Audio content</audio>
onloadeddataThe script to be run when media data is loaded and playback can start.

 <!-- Executes go() function -->
<audio onloadeddata="go()">Audio content</audio>
onloadedmetadataThe script to be run when metadata has been loaded.

 <!-- Executes go() function -->
<audio onloadedmetadata="go()">Audio content</audio>
onloadstartThe script to be run whenthe media resource has started loading.

 <!-- Executes go() function -->
<audio onloadstart="go()">Audio content</audio>
onpauseThe script to be run when the media resource has been paused.

 <!-- Executes go() function -->
<audio onpause="go()">Audio content</audio>
onplayThe script to be run when the media resource starts playback.

 <!-- Executes go() function -->
<audio onplay="go()">Audio content</audio>
onplayingThe script to be run when when playback has already begun.

 <!-- Executes go() function -->
<audio onplaying="go()">Audio content</audio>
onprogressThe script to be run when the browser is fetching the media data.

 <!-- Executes go() function -->
<audio onprogress="go()">Audio content</audio>
onratechangeThe script to be run when the playback rate changes.

 <!-- Executes go() function -->
<audio onratechange="go()">Audio content</audio>
onseekedThe script to be run when the seeking attribute is set to false indicating that seeking has finished.

 <!-- Executes go() function -->
<audio onseeked="go()">Audio content</audio>
onseekingThe script to be run when the seeking attribute is set to true indicating that seeking is currently active.

 <!-- Executes go() function -->
<audio onseeking="go()">Audio content</audio>
onstalledThe script to be run when the browser is unable to continue fetching media data.

 <!-- Executes go() function -->
<audio onstalled="go()">Audio content</audio>
onsuspendThe script to be run when media data has stopped before being completely loaded.

 <!-- Executes go() function -->
<audio onsuspend="go()">Audio content</audio>
ontimeupdateThe script to be run when the media resources current playback position has changed.

 <!-- Executes go() function -->
<audio ontimeupdate="go()">Audio content</audio>
onvolumechangeThe script to be run when the volume has changed or been muted.

 <!-- Executes go() function -->
<audio onvolumechange="go()">Audio content</audio>
onwaitingThe script to be run when the media resource has paused but is expected to resume.

 <!-- Executes go() function -->
<audio onwaiting="go()">Audio content</audio>
Mouse
onclickThe script to be run when when a mouse is clicked on an element.

 <!-- Executes go() function -->
<audio onclick="go()">Audio content</audio>
ondblclickThe script to be run when a mouse is double clicked on an element.

 <!-- Executes go() function -->
<audio ondblclick="go()">Audio content</audio>
onmousedownThe script to be run when he mouse button is pressed down while the cursor is over an element.

 <!-- Executes go() function -->
<audio onmousedown="go()">Audio content</audio>
onmousemoveThe script to be run when the mouse button is moved.

 <!-- Executes go() function -->
<audio onmousemove="go()">Audio content</audio>
onmouseoutThe script to be run when the mouse cursor moves off an element.

 <!-- Executes go() function -->
<audio onmouseout="go()">Audio content</audio>>
onmouseoverThe script to be run when the mouse cursor moves over an element.

 <!-- Executes go() function -->
<audio onmouseover="go()">Audio content</audio>
onmouseupThe script to be run when the mouse button is released while the cursor is over the element.

 <!-- Executes go() function -->
<audio onmouseup="go()">Audio content</audio>
onwheelThe script to be run when the mouse wheel rolls up or down over an element.

 <!-- Executes go() function -->
<audio onwheel="go()">Audio content</audio>
Scroll
onscrollThe script code to be run when the scrollbar of an element is being scrolled.

 <!-- Executes go() function -->
<audio onscroll="go()">Audio content</audio> 
Window - NONE

Relevant HTML Tutorials

HTML Media