Range Slider
A slider with a filled track, a value bubble and keyboard support.
AI prompt
Build a custom-styled range slider on top of a native input type="range" (min 0, max 100, value 40) so keyboard, touch and screen-reader support are preserved. Style the track as 6px tall with the portion before the thumb filled with the accent colour by updating a CSS variable --p (percentage) on input. Style the thumb as a 24px white circle with a soft shadow that grows to 28px while dragging. Show a value bubble above the thumb (rounded rectangle with the number) positioned using --p and clamped inside the container; it fades in during pointer press or keyboard focus and out afterwards in 120ms. Also show the numeric value to the right of a label "Volume" for accessibility and set aria-valuetext with a unit. Disable the growth animation under prefers-reduced-motion.About this pattern
Definition
A control for choosing a number within a range by dragging a thumb along a track. The part of the track behind the thumb is filled, and a bubble above the thumb shows the current value while it is being used.
Purpose & UX principles
- Best for approximate values where the exact number matters less than the feel (volume, brightness, price range).
- When precision matters, pair the slider with a number input.
- Always expose the value as text, not only as a position.
Interaction states
- Idle, hover (thumb halo), dragging (bubble visible, thumb grows), focus-visible, disabled.
- Arrow keys step by 1; PageUp and PageDown step by 10 (native
input type=rangebehaviour).
Motion specification
- Thumb growth and bubble: 120ms ease-out.
- The fill follows the value directly without easing to feel responsive.
- Reduced motion: no growth animation.
Responsive behaviour
- Thumb 24px visual with a 44px touch area; the track is full width.
- Keep the bubble inside the container at the extremes.
Code
<div class="rs"><div class="top"><label for="r">Volume</label><output id="o">40%</output></div>
<div class="wrap"><span class="bub" id="b">40</span><input type="range" id="r" min="0" max="100" value="40"></div></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.rs{width:min(340px,100%);padding:20px;border:1px solid var(--line);border-radius:18px;background:var(--surface)}
.top{display:flex;justify-content:space-between;margin-bottom:34px;font-size:15px}
output{color:var(--accent);font-weight:700;font-variant-numeric:tabular-nums}
.wrap{position:relative;--p:40}
input[type=range]{-webkit-appearance:none;appearance:none;width:100%;height:24px;background:transparent;cursor:pointer;margin:0}
input[type=range]::-webkit-slider-runnable-track{height:6px;border-radius:3px;background:linear-gradient(to right,var(--accent) calc(var(--p)*1%),var(--surface-2) calc(var(--p)*1%))}
input[type=range]::-moz-range-track{height:6px;border-radius:3px;background:var(--surface-2)}
input[type=range]::-moz-range-progress{height:6px;border-radius:3px;background:var(--accent)}
input[type=range]::-webkit-slider-thumb{-webkit-appearance:none;width:24px;height:24px;margin-top:-9px;border-radius:50%;background:#fff;box-shadow:0 1px 5px rgba(0,0,0,.3);transition:transform .12s ease-out}
input[type=range]::-moz-range-thumb{width:24px;height:24px;border:0;border-radius:50%;background:#fff;box-shadow:0 1px 5px rgba(0,0,0,.3)}
input[type=range]:active::-webkit-slider-thumb{transform:scale(1.17)}
input[type=range]:focus-visible{outline:2px solid var(--accent);outline-offset:6px;border-radius:12px}
.bub{position:absolute;bottom:34px;left:calc(12px + (100% - 24px) * var(--p) / 100);transform:translateX(-50%) translateY(4px);background:var(--text);color:var(--bg);font-size:12px;font-weight:700;padding:3px 9px;border-radius:8px;opacity:0;pointer-events:none;transition:opacity .12s,transform .12s;font-variant-numeric:tabular-nums}
.wrap:focus-within .bub,.wrap.drag .bub{opacity:1;transform:translateX(-50%)}
@media (prefers-reduced-motion:reduce){input[type=range]::-webkit-slider-thumb,.bub{transition:none}}const r=document.getElementById('r'),b=document.getElementById('b'),o=document.getElementById('o'),w=r.parentElement;
function upd(){const p=(r.value-r.min)/(r.max-r.min)*100;w.style.setProperty('--p',p);b.textContent=r.value;o.textContent=r.value+'%';r.setAttribute('aria-valuetext',r.value+' percent');}
r.addEventListener('input',upd);
r.addEventListener('pointerdown',()=>w.classList.add('drag'));
addEventListener('pointerup',()=>w.classList.remove('drag'));upd();One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Range Slider — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.rs{width:min(340px,100%);padding:20px;border:1px solid var(--line);border-radius:18px;background:var(--surface)}
.top{display:flex;justify-content:space-between;margin-bottom:34px;font-size:15px}
output{color:var(--accent);font-weight:700;font-variant-numeric:tabular-nums}
.wrap{position:relative;--p:40}
input[type=range]{-webkit-appearance:none;appearance:none;width:100%;height:24px;background:transparent;cursor:pointer;margin:0}
input[type=range]::-webkit-slider-runnable-track{height:6px;border-radius:3px;background:linear-gradient(to right,var(--accent) calc(var(--p)*1%),var(--surface-2) calc(var(--p)*1%))}
input[type=range]::-moz-range-track{height:6px;border-radius:3px;background:var(--surface-2)}
input[type=range]::-moz-range-progress{height:6px;border-radius:3px;background:var(--accent)}
input[type=range]::-webkit-slider-thumb{-webkit-appearance:none;width:24px;height:24px;margin-top:-9px;border-radius:50%;background:#fff;box-shadow:0 1px 5px rgba(0,0,0,.3);transition:transform .12s ease-out}
input[type=range]::-moz-range-thumb{width:24px;height:24px;border:0;border-radius:50%;background:#fff;box-shadow:0 1px 5px rgba(0,0,0,.3)}
input[type=range]:active::-webkit-slider-thumb{transform:scale(1.17)}
input[type=range]:focus-visible{outline:2px solid var(--accent);outline-offset:6px;border-radius:12px}
.bub{position:absolute;bottom:34px;left:calc(12px + (100% - 24px) * var(--p) / 100);transform:translateX(-50%) translateY(4px);background:var(--text);color:var(--bg);font-size:12px;font-weight:700;padding:3px 9px;border-radius:8px;opacity:0;pointer-events:none;transition:opacity .12s,transform .12s;font-variant-numeric:tabular-nums}
.wrap:focus-within .bub,.wrap.drag .bub{opacity:1;transform:translateX(-50%)}
@media (prefers-reduced-motion:reduce){input[type=range]::-webkit-slider-thumb,.bub{transition:none}}
</style></head>
<body><div class="stage">
<div class="rs"><div class="top"><label for="r">Volume</label><output id="o">40%</output></div>
<div class="wrap"><span class="bub" id="b">40</span><input type="range" id="r" min="0" max="100" value="40"></div></div>
</div>
<script>
const r=document.getElementById('r'),b=document.getElementById('b'),o=document.getElementById('o'),w=r.parentElement;
function upd(){const p=(r.value-r.min)/(r.max-r.min)*100;w.style.setProperty('--p',p);b.textContent=r.value;o.textContent=r.value+'%';r.setAttribute('aria-valuetext',r.value+' percent');}
r.addEventListener('input',upd);
r.addEventListener('pointerdown',()=>w.classList.add('drag'));
addEventListener('pointerup',()=>w.classList.remove('drag'));upd();
</script>
</body></html>