Overview
The HTML <audio> tag is used to place a playable audio file on the page.
The <audio> tag contains one or more <source> tags specifying different audio types. The browser will choose the first source it supports.
<audio>
<source src="song-name.ogg" type="audio/ogg">
<source src="song-name.mp3" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>
Note
It’s good practice to include a line of text inside the <audio> tag for those browsers that do not support audio.
To display audio controls (play, pause, volume, etc.), place the controls attribute inside the <audio> tag.
<audio controls>
</audio>
The video can also be set to start playing on page load (not recommended), by placing the autoplay attribute inside the <audio> tag.
<audio controls autoplay>
</audio>
There are many more attributes that can be used within the <audio&rt; tag.
Attribute | Value | Description |
---|---|---|
autoplay | n/a - just the attribute is needed | Specifies that the audio will start playing as soon as it's ready |
controls | n/a - just the attribute is needed | Specifies that audio controls should be displayed (play, pause, etc.) |
loop | n/a - just the attribute is needed | Specifies that the audio will start over again once finished |
muted | n/a - just the attribute is needed | Specifies that the audio output should be muted |
preload | auto metadata none | Specifies if and how the author thinks the audio should be loaded when the page loads |
src | URL | Specifies the URL of the audio file |
HTML Notes:
- In our HTML section the term “tag” and “element” are often used interchangeably to refer to both the tag used to create a page element and the element created by the tag (<p> tag = <p> element = paragraph on the page)
- HTML5 is not case sensitive; so <P> is the same as <p>, <H1> is the same as <h1>
- Global attributes can be used with all HTML tags and are therefore not mentioned on every tag page
- To write clean, readable HTML code, it is best to use indentation whereas elements within elements are indented (tabbed or spaces) to create something that looks like a project outline
- The browser will automatically remove any extra spaces and lines in your HTML code when the page is displayed
- Double quotes or single quotes can be used around HTML attribute values, but when the attribute value itself contains one form of quote, it will be necessary to use the other around the attribute
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.