Design a custom range slider for controlling audio volume using HTML, JS, and CSS.
- User
Design a custom range slider for controlling audio volume using HTML, JS, and CSS.
- Assistant
Creating a volume control
I'm working on a solution for a volume control with HTML, CSS, and JavaScript. The HTML structure includes a mute/unmute button with an SVG speaker and a slider for volume levels. The JavaScript manages audio playback and sets the volume based on the slider's input. The background of the slider updates dynamically to reflect the volume. I also need to ensure the CSS is polished for cross-browser compatibility while keeping it concise. I'll format the response clearly with labeled code blocks! Here’s a simple, self‑contained example of a custom volume slider that controls an
<audio>element.You can put this in a single
index.htmlfile and open it in your browser.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Custom Volume Slider</title> <style> body { font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background: #0f172a; color: #e5e7eb; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; margin: 0; } .player { background: #020617; border-radius: 12px; padding: 16px 20px; box-shadow: 0 18px 45px rgba(0, 0, 0, 0.55); min-width: 320px; } .volume-control { display: flex; align-items: center; gap: 10px; margin-top: 12px; } /* Volume icon button */ .volume-button { border: none; background: transparent; color: #e5e7eb; width: 28px; height: 28px; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; padding: 0; } .volume-button svg { width: 20px; height: 20px; } .volume-button:focus-visible { outline: 2px solid #22c55e; outline-offset: 2px; border-radius: 999px; } /* Range slider base */ .volume-slider { -webkit-appearance: none; appearance: none; width: 180px; height: 6px; border-radius: 999px; background: linear-gradient( to right, #22c55e 0%, #22c55e 75%, #1f2937 75%, #1f2937 100% ); cursor: pointer; outline: none; } /* Remove default focus ring in some browsers and style our own */ .volume-slider:focus-visible { box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.6); } /* WebKit Track */ .volume-slider::-webkit-slider-runnable-track { height: 6px; border-radius: 999px; background: transparent; /* we use the background on the input itself */ } /* WebKit Thumb */ .volume-slider::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 16px; height: 16px; border-radius: 50%; background: #ffffff; border: 2px solid #22c55e; margin-top: -5px; /* center thumb on 6px track */ box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.25); transition: transform 0.12s ease, box-shadow 0.12s ease; } .volume-slider::-webkit-slider-thumb:hover { transform: scale(1.05); box-shadow: 0 0 0 6px rgba(34, 197, 94, 0.35); } /* Firefox Track */ .volume-slider::-moz-range-track { height: 6px; border-radius: 999px; background: transparent; } /* Firefox Thumb */ .volume-slider::-moz-range-thumb { width: 16px; height: 16px; border-radius: 50%; background: #ffffff; border: 2px solid #22c55e; box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.25); transition: transform 0.12s ease, box-shadow 0.12s ease; } .volume-slider::-moz-range-thumb:hover { transform: scale(1.05); box-shadow: 0 0 0 6px rgba(34, 197, 94, 0.35); } /* Firefox: remove extra focus border */ .volume-slider::-moz-focus-outer { border: 0; } /* IE/Edge legacy (optional) */ .volume-slider::-ms-track { height: 6px; border-radius: 999px; background: transparent; border-color: transparent; color: transparent; } .volume-slider::-ms-thumb { width: 16px; height: 16px; border-radius: 50%; background: #ffffff; border: 2px solid #22c55e; box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.25); } .volume-slider::-ms-fill-lower { background: #22c55e; border-radius: 999px; } .volume-slider::-ms-fill-upper { background: #1f2937; border-radius: 999px; } </style> </head> <body> <div class="player"> <audio id="audio" src="https://www.kozco.com/tech/LRMonoPhase4.mp3" controls ></audio> <div class="volume-control"> <!-- Volume / mute button --> <button class="volume-button" id="muteButton" aria-label="Mute"> <!-- Simple speaker icon (SVG) --> <svg id="volumeIcon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M11 5L6 9H3v6h3l5 4V5z"></path> <path d="M15.54 8.46A5 5 0 0 1 17 12a5 5 0 0 1-1.46 3.54"></path> </svg> </button> <!-- Custom range slider --> <input id="volumeSlider" class="volume-slider" type="range" min="0" max="100" value="75" step="1" aria-label="Volume" /> </div> </div> <script> const audio = document.getElementById('audio'); const slider = document.getElementById('volumeSlider'); const muteButton = document.getElementById('muteButton'); const volumeIcon = document.getElementById('volumeIcon'); // Keep track of last non-zero volume for mute/unmute let lastVolume = Number(slider.value) || 75; // Set initial audio volume (0–1) from slider (0–100) audio.volume = slider.value / 100; function updateSliderBackground(value) { // value: 0–100 const percent = value; slider.style.background = ` linear-gradient( to right, #22c55e 0%, #22c55e ${percent}%, #1f2937 ${percent}%, #1f2937 100% ) `; } function updateIcon(volume) { // Simple switch between loud and muted icon if (volume === 0 || audio.muted) { volumeIcon.setAttribute( 'd', '' // we'll rebuild full SVG paths instead ); volumeIcon.innerHTML = ` <path d="M11 5L6 9H3v6h3l5 4V5z"></path> <line x1="19" y1="9" x2="15" y2="13"></line> <line x1="15" y1="9" x2="19" y2="13"></line> `; muteButton.setAttribute('aria-label', 'Unmute'); } else { volumeIcon.innerHTML = ` <path d="M11 5L6 9H3v6h3l5 4V5z"></path> <path d="M15.54 8.46A5 5 0 0 1 17 12a5 5 0 0 1-1.46 3.54"></path> `; muteButton.setAttribute('aria-label', 'Mute'); } } // Initialize visual state updateSliderBackground(slider.value); updateIcon(audio.volume); // Handle slider move slider.addEventListener('input', (e) => { const value = Number(e.target.value); // 0–100 audio.volume = value / 100; audio.muted = value === 0; if (value !== 0) { lastVolume = value; } updateSliderBackground(value); updateIcon(audio.volume); }); // Mute/unmute on button click muteButton.addEventListener('click', () => { if (audio.muted || audio.volume === 0) { // Unmute to last volume const newVal = lastVolume || 75; audio.muted = false; audio.volume = newVal / 100; slider.value = newVal; updateSliderBackground(newVal); } else { // Mute and store current volume lastVolume = Number(slider.value) || 75; audio.muted = true; slider.value = 0; updateSliderBackground(0); } updateIcon(audio.volume); }); </script> </body> </html>You can customize:
- Colors (
#22c55e,#1f2937) for the filled/unfilled track. - Thumb size and shape in the
::-webkit-slider-thumb/::-moz-range-thumbrules. - The SVG icon paths for different volume levels.
- Colors (
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.