About this snippet

Embed music on your page with this glassmorphism audio widget. We designed a frosted background and custom play controls, so it looks far better than default players yet remains simple.

Size
3.4 KB
Uses
HTML, CSS, JavaScript
Loads
placehold.co, codeshack.io
Views
129
<div class="audio-widget">
  <div class="audio-info">
    <img src="https://placehold.co/60x60/3b82f6/ffffff?text=Art" alt="Album Art" class="album-art">
    <div class="track-details">
      <h3>Late Night Vibes</h3>
      <p>Lofi Producer</p>
    </div>
  </div>
  
  <div class="audio-controls">
    <button class="play-circle" id="play-btn">
      <svg id="play-icon" viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>
      <svg id="pause-icon" viewBox="0 0 24 24" width="20" height="20" fill="currentColor" style="display: none;"><rect x="6" y="4" width="4" height="16"></rect><rect x="14" y="4" width="4" height="16"></rect></svg>
    </button>
    
    <div class="progress-container">
      <div class="progress-bar" id="progress-bar"></div>
    </div>
  </div>
  
  <audio id="audio-element" src="https://codeshack.io/web/example.mp4"></audio>
</div>
*, *::before, *::after {
  box-sizing: border-box;
}

body {
  font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  background: url("https://placehold.co/1920x1080/4c1d95/1e1b4b?text=Gradient") center/cover;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  padding: 20px;
}

.audio-widget {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  padding: 20px;
  border-radius: 20px;
  width: 100%;
  max-width: 350px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
  color: #ffffff;
}

.audio-info {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
}

.album-art {
  border-radius: 12px;
  box-shadow: 0 4px 10px rgba(0,0,0,0.3);
}

.track-details h3 {
  margin: 0 0 4px 0;
  font-size: 1.125rem;
}

.track-details p {
  margin: 0;
  color: #cbd5e1;
  font-size: 0.875rem;
}

.audio-controls {
  display: flex;
  align-items: center;
  gap: 16px;
}

.play-circle {
  background: #ffffff;
  color: #4c1d95;
  border: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.2s;
}

.play-circle:active {
  transform: scale(0.95);
}

.progress-container {
  flex-grow: 1;
  height: 6px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: #ffffff;
  border-radius: 3px;
}
const audio = document.getElementById("audio-element");
const playBtn = document.getElementById("play-btn");
const playIcon = document.getElementById("play-icon");
const pauseIcon = document.getElementById("pause-icon");
const progressBar = document.getElementById("progress-bar");

playBtn.addEventListener("click", function() {
  if (audio.paused) {
    audio.play();
    playIcon.style.display = "none";
    pauseIcon.style.display = "block";
  } else {
    audio.pause();
    playIcon.style.display = "block";
    pauseIcon.style.display = "none";
  }
});

audio.addEventListener("timeupdate", function() {
  if (!isNaN(audio.duration)) {
    const percentage = (audio.currentTime / audio.duration) * 100;
    progressBar.style.width = percentage + "%";
  }
});

// Reset when finished
audio.addEventListener("ended", function() {
  playIcon.style.display = "block";
  pauseIcon.style.display = "none";
  progressBar.style.width = "0%";
});
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save