Introduction to <audio>

The <audio>HTML element is a versatile and essential tool for embedding sound content in web documents. Whether you're looking to add <background music>, <podcasts>, or sound effects to your webpage, the <audio>tag provides a straightforward method for doing so. This element enables web developers to directly incorporate audio files into their webpages, creating a richer, more interactive user experience.

Copy to clipboard
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>title of <audio> tag</title>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<div class="content">
<h1>Listen to Our Latest Podcast</h1>
<p>Enjoy our latest episode filled with insightful discussions, special guest appearances, and much more. Click play to start listening now!</p>
<div class="audio-player-container">
<div class="audio-player">
<audio id="audio" preload="metadata">
<source src="/public_html/ID/epic-cinematic-beautiful-dubstep-141372.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<div class="controls">
<button id="play-pause" class="play">
<span class="material-icons">play_arrow</span>
</button>
<div class="progress-bar-container">
<input type="range" id="progress-bar" value="0" step="1" min="0" max="100">
</div>
<div class="time">
<span id="current-time">00:00</span> / <span id="duration">00:00</span>
</div>
<button id="mute">
<span class="material-icons">volume_up</span>
</button>
<input type="range" id="volume-bar" value="100" step="1" min="0" max="100">
</div>
</div>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
Copy to clipboard
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
background-color: #181818;
font-family: 'Roboto', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
overflow: hidden;
background: linear-gradient(135deg, #1e1e1e 0%, #282828 100%);
}

.container {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
}

.content {
max-width: 500px;
text-align: center;
color: #fff;
padding: 20px;
background-color: rgba(34, 34, 34, 0.85);
border-radius: 15px;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5);
}

h1 {
font-size: 24px;
margin-bottom: 10px;
color: #ff416c;
font-weight: 700;
}

p {
font-size: 16px;
line-height: 1.5;
margin-bottom: 20px;
color: #ccc;
}

.audio-player {
max-width: 400px;
background-color: #222;
border-radius: 20px;
padding: 30px 20px;
box-shadow: 0 15px 25px rgba(0, 0, 0, 0.5);
text-align: center;
position: relative;
transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.audio-player:hover {
transform: translateY(-10px);
box-shadow: 0 30px 40px rgba(0, 0, 0, 0.7);
}

.controls {
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
gap: 20px;
margin-top: 20px;
}

button {
background: linear-gradient(45deg, #e2005e, #006af5);
color: white;
border: none;
padding: 15px;
border-radius: 50%;
cursor: pointer;
font-size: 20px;
display: flex;
align-items: center;
justify-content: center;
transition: background 0.4s ease, transform 0.3s ease, box-shadow 0.3s ease;
}

button:hover {
transform: scale(1.1);
box-shadow: 0 5px 15px rgba(255, 75, 43, 0.6);
}

.material-icons {
font-size: 28px;
}

.progress-bar-container {
flex-grow: 2;
display: flex;
align-items: center;
}

input[type="range"] {
width: 100%;
-webkit-appearance: none;
background: #333;
height: 6px;
border-radius: 5px;
outline: none;
margin: 0 10px;
cursor: pointer;
transition: background 0.3s ease;
}

input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
width: 16px;
height: 16px;
background: radial-gradient(circle, #ff416c 30%, #ff4b2b);
border-radius: 50%;
cursor: pointer;
transition: transform 0.3s ease, background-color 0.3s ease;
}

input[type="range"]::-moz-range-thumb {
width: 16px;
height: 16px;
background: radial-gradient(circle, #ff416c 30%, #ff4b2b);
border-radius: 50%;
cursor: pointer;
transition: transform 0.3s ease, background-color 0.3s ease;
}

input[type="range"]::-ms-thumb {
width: 16px;
height: 16px;
background: radial-gradient(circle, #ff416c 30%, #ff4b2b);
border-radius: 50%;
cursor: pointer;
transition: transform 0.3s ease, background-color 0.3s ease;
}

input[type="range"]::-webkit-slider-thumb:hover, input[type="range"]::-moz-range-thumb:hover, input[type="range"]::-ms-thumb:hover {
transform: scale(1.2);
background-color: #ff4b2b;
}

input[type="range"]:focus {
background: #444;
}

.time {
font-size: 14px;
color: #ccc;
font-weight: 500;
}

#volume-bar {
width: 100px;
}

@keyframes pulse {
0% {
box-shadow: 0 0 0 0 rgba(255, 75, 43, 0.4);
}
70% {
box-shadow: 0 0 0 10px rgba(255, 75, 43, 0);
}
100% {
box-shadow: 0 0 0 0 rgba(255, 75, 43, 0);
}
}

button:active {
animation: pulse 0.7s;
}

.audio-player:before {
content: '';
position: absolute;
top: -2px;
left: 50%;
width: 90%;
height: 5px;
background: linear-gradient(45deg, #5272ff, #8215ff);
border-radius: 10px;
transform: translateX(-50%);
}

Copy to clipboard
const audio = document.getElementById('audio');
const playPauseButton = document.getElementById('play-pause');
const progressBar = document.getElementById('progress-bar');
const currentTimeSpan = document.getElementById('current-time');
const durationSpan = document.getElementById('duration');
const muteButton = document.getElementById('mute');
const volumeBar = document.getElementById('volume-bar');

playPauseButton.addEventListener('click', () => {
  if (audio.paused) {
    audio.play();
    playPauseButton.querySelector('.material-icons').textContent = 'pause';
  } else {
    audio.pause();
    playPauseButton.querySelector('.material-icons').textContent = 'play_arrow';
  }
});

audio.addEventListener('timeupdate', () => {
  const currentTime = audio.currentTime;
  const duration = audio.duration;
  progressBar.value = (currentTime / duration) * 100;

  const currentMinutes = Math.floor(currentTime / 60);
  const currentSeconds = Math.floor(currentTime % 60).toString().padStart(2, '0');
  currentTimeSpan.textContent = `${currentMinutes}:${currentSeconds}`;

  const durationMinutes = Math.floor(duration / 60);
  const durationSeconds = Math.floor(duration % 60).toString().padStart(2, '0');
  durationSpan.textContent = `${durationMinutes}:${durationSeconds}`;
});

progressBar.addEventListener('input', () => {
  audio.currentTime = (progressBar.value / 100) * audio.duration;
});

muteButton.addEventListener('click', () => {
  audio.muted = !audio.muted;
  muteButton.querySelector('.material-icons').textContent = audio.muted ? 'volume_off' : 'volume_up';
});

volumeBar.addEventListener('input', () => {
  audio.volume = volumeBar.value / 100;
});
audio.addEventListener('ended', () => {
  playPauseButton.querySelector('.material-icons').textContent = 'play_arrow';
  progressBar.value = 0;
  currentTimeSpan.textContent = '00:00';
});

Global Attributes

AttributesDescription
accesskeySpecifies a shortcut key to activate or focus an element.
autocapitalizeControls whether and how text input is automatically capitalized.
classSpecifies one or more class names for the element, allowing CSS styling and JavaScript manipulation.
contenteditableSpecifies whether the content of an element is editable or not.
contextmenuSpecifies a context menu for the element.
dirDefines the text direction. Possible values are "ltr" (left-to-right), "rtl" (right-to-left), and "auto".
draggableSpecifies whether an element is draggable. Possible values are "true" or "false".
enterkeyhintSpecifies the action label or icon to be shown for the enter key on virtual keyboards.
hiddenIndicates that the element is not yet, or is no longer, relevant. The browser does not display elements that have the `hidden` attribute set.
idSpecifies a unique identifier for the element.
inputmodeProvides a hint to browsers for which virtual keyboard configuration to use when editing this element or its descendants.
isAllows you to specify the type of custom element.
langSpecifies the language of the element's content.
nonceA cryptographic nonce ("number used once") that can be used by Content Security Policy (CSP) to determine whether or not a given fetch will be allowed to proceed.
partSpecifies the element’s part in the shadow DOM.
slotAssigns a slot in a shadow DOM shadow tree.
spellcheckIndicates whether the element is subject to spell checking.
styleProvides inline CSS styling for the element.
tabindexSpecifies the tab order of the element.
titleAdds extra information about the element, displayed as a tooltip when hovering over the element.
translateSpecifies whether the content of the element should be translated. Possible values are "yes" or "no".

Audio Format and Browser Support

BrowserMP3WAVOGG
Edge / IEYESYES*YES*
ChromeYESYESYES
FirefoxYESYESYES
SafariYESYESNO
OperaYESYESYES
HTML AttributesGlobal AttributesEvent Attributes
To view the full list
To view the full list
To view the full list
elementsChrome BrowsersMicrosoft Edge BrowserFirefox BrowsersSafari BrowserOpera Browser
<audio>4.09.03.54.011.5