What is a range input?
A range input is a slider: you drag a handle along a bar to choose a value, such as volume, brightness or a price limit. You create one with a single HTML tag:
<label for="volume">Volume</label>
<input type="range" id="volume" min="0" max="100" value="40">
The min and max attributes set the lowest and highest values, value sets the starting position, and you can add step to control how far each move goes, such as step="5".
Every browser draws sliders differently, and the default look rarely matches your design. In this post we'll style them to fit your site. To design one visually instead, try our Style HTML Input Range generator.
The parts of a slider
| Part | What it is |
|---|---|
| Track | The long bar the handle slides along |
| Thumb | The handle you drag |
| Progress (or fill) | The part of the track before the thumb, often in a stronger colour |
The one-line fix: accent-color
If you just want the slider to match your brand colour, modern CSS has a one-line solution. The accent-color property colours the filled part of the track and the thumb:
input[type="range"] {
accent-color: #1d4ed8;
}
The browser keeps everything else, including keyboard support and focus styles, and you don't need to style each part separately. For many sites, this is all you need.
Step 1: resetting the slider
For a fully custom design, we first remove the browser's own styling with appearance: none:
.slider {
appearance: none;
width: 100%;
height: 24px;
margin: 0;
background: transparent;
cursor: pointer;
}
The height here is the size of the clickable area, not the visible bar. Making it taller than the track gives people a bigger target to grab, which helps a lot on touch screens.
Why we still need browser-specific selectors
CSS doesn't yet have standard selectors for the track and thumb, so we use vendor-prefixed pseudo-elements: special names that each browser engine provides for the parts of the slider.
| Part | Chrome, Edge, Safari, Opera | Firefox |
|---|---|---|
| Track | ::-webkit-slider-runnable-track | ::-moz-range-track |
| Thumb | ::-webkit-slider-thumb | ::-moz-range-thumb |
| Progress | No equivalent | ::-moz-range-progress |
What changed since the old version? The original also listed ::-ms-track, ::-ms-thumb and other -ms- selectors for Internet Explorer. Internet Explorer has been retired, so those are no longer needed, which removes a third of the code.
Step 2: styling the track
.slider::-webkit-slider-runnable-track {
height: 6px;
border-radius: 999px;
background: #dbeafe;
}
.slider::-moz-range-track {
height: 6px;
border-radius: 999px;
background: #dbeafe;
}
This gives a slim, pale blue bar with fully rounded ends.
Key rule: write separate rules for each browser, even though the styles are identical. If you combine them with a comma, such as .slider::-webkit-slider-runnable-track, .slider::-moz-range-track { ... }, each browser sees a selector it doesn't recognise and ignores the whole rule.
Step 3: styling the thumb
.slider::-webkit-slider-thumb {
appearance: none;
box-sizing: border-box;
width: 20px;
height: 20px;
margin-top: -7px;
border: 2px solid #1d4ed8;
border-radius: 50%;
background: #ffffff;
box-shadow: 0 1px 3px rgb(0 0 0 / 0.3);
}
.slider::-moz-range-thumb {
box-sizing: border-box;
width: 20px;
height: 20px;
border: 2px solid #1d4ed8;
border-radius: 50%;
background: #ffffff;
box-shadow: 0 1px 3px rgb(0 0 0 / 0.3);
}
appearance: noneon the WebKit thumb removes the browser's built-in handle, so our styles can take over.box-sizing: border-boxincludes the border in the 20px size, so the thumb is the same size in every browser.border-radius: 50%makes the thumb a circle.
Centring the thumb
In Chrome, Edge and Safari, the thumb sits at the top of the track rather than in the middle. The margin-top pulls it back into place. The formula is:
margin-top: (track height - thumb height) / 2
= (6px - 20px) / 2
= -7px
Firefox centres the thumb automatically, so its rule doesn't need a margin. If you change either size, recalculate the margin to match.
A little hover effect
.slider::-webkit-slider-thumb { transition: scale 0.15s ease; }
.slider::-moz-range-thumb { transition: scale 0.15s ease; }
.slider:hover::-webkit-slider-thumb { scale: 1.15; }
.slider:hover::-moz-range-thumb { scale: 1.15; }
Step 4: showing the filled part
A filled track, darker before the thumb and lighter after, makes it easy to see the value at a glance. Firefox has a pseudo-element for this:
.slider::-moz-range-progress {
height: 6px;
border-radius: 999px;
background: #1d4ed8;
}
Chrome, Edge and Safari don't have an equivalent, so we paint the fill onto the track with a gradient, using a custom property (a CSS variable) called --fill for the percentage:
.slider {
--fill: 50%;
}
.slider::-webkit-slider-runnable-track {
background: linear-gradient(to right, #1d4ed8 var(--fill), #dbeafe var(--fill));
}
The gradient switches sharply from dark blue to pale blue at exactly --fill. A few lines of JavaScript keep that value in step with the slider as it moves:
<script>
document.querySelectorAll('.slider').forEach(function (slider) {
function update() {
const percent = (slider.value - slider.min) / (slider.max - slider.min) * 100;
slider.style.setProperty('--fill', percent + '%');
}
slider.addEventListener('input', update);
update();
});
</script>
Drag the sliders below. The filled part follows the thumb, and the number beside each one updates too:
Without JavaScript? The slider still works perfectly; in Chrome, Edge and Safari the track simply shows the fill at its starting position. Firefox's ::-moz-range-progress needs no script at all.
Showing the value
The numbers beside the sliders use the <output> element, which is designed to display the result of a form control. Its for attribute links it to the slider:
<input type="range" id="volume" class="slider" min="0" max="100" value="60">
<output for="volume">60</output>
Update it in the same JavaScript function with output.value = slider.value.
Focus, labels and accessibility
Never just remove the focus outline
The original version of this post included input[type=range]:focus { outline: none; }. That hides the only sign of where keyboard users are, because sliders can be moved with the arrow keys. If you remove the default outline, replace it, for example with a ring around the thumb:
.slider:focus-visible {
outline: none;
}
.slider:focus-visible::-webkit-slider-thumb {
outline: 3px solid #f59e0b;
outline-offset: 2px;
}
.slider:focus-visible::-moz-range-thumb {
outline: 3px solid #f59e0b;
outline-offset: 2px;
}
Try clicking a slider above and then pressing the arrow keys, or press Tab to reach one: the thumb gets an amber ring.
Always add a label
A slider on its own doesn't say what it controls. Connect a <label> using the for and id attributes, so screen readers (software that reads pages aloud) announce something like "Volume, slider, 60".
If the number alone isn't meaningful, add aria-valuetext to describe it in words, such as aria-valuetext="60 percent" or aria-valuetext="Medium".
Disabled sliders
.slider:disabled {
opacity: 0.5;
cursor: not-allowed;
}
Correction: the original code included animate: 0.2s. There's no animate property in CSS, so browsers ignored it. To animate part of a slider, use transition, as in the thumb hover effect above.
The complete code
.slider {
--fill: 50%;
appearance: none;
width: 100%;
height: 24px;
margin: 0;
background: transparent;
cursor: pointer;
}
/* Track */
.slider::-webkit-slider-runnable-track {
height: 6px;
border-radius: 999px;
background: linear-gradient(to right, #1d4ed8 var(--fill), #dbeafe var(--fill));
}
.slider::-moz-range-track {
height: 6px;
border-radius: 999px;
background: #dbeafe;
}
.slider::-moz-range-progress {
height: 6px;
border-radius: 999px;
background: #1d4ed8;
}
/* Thumb */
.slider::-webkit-slider-thumb {
appearance: none;
box-sizing: border-box;
width: 20px;
height: 20px;
margin-top: -7px;
border: 2px solid #1d4ed8;
border-radius: 50%;
background: #ffffff;
box-shadow: 0 1px 3px rgb(0 0 0 / 0.3);
transition: scale 0.15s ease;
}
.slider::-moz-range-thumb {
box-sizing: border-box;
width: 20px;
height: 20px;
border: 2px solid #1d4ed8;
border-radius: 50%;
background: #ffffff;
box-shadow: 0 1px 3px rgb(0 0 0 / 0.3);
transition: scale 0.15s ease;
}
.slider:hover::-webkit-slider-thumb { scale: 1.15; }
.slider:hover::-moz-range-thumb { scale: 1.15; }
/* Focus */
.slider:focus-visible { outline: none; }
.slider:focus-visible::-webkit-slider-thumb { outline: 3px solid #f59e0b; outline-offset: 2px; }
.slider:focus-visible::-moz-range-thumb { outline: 3px solid #f59e0b; outline-offset: 2px; }
/* Disabled */
.slider:disabled { opacity: 0.5; cursor: not-allowed; }
@media (prefers-reduced-motion: reduce) {
.slider::-webkit-slider-thumb,
.slider::-moz-range-thumb { transition: none; }
}
Want to skip the typing? Our Style HTML Input Range generator lets you design the track and thumb visually and copy the finished, modern code. If you create a design you're proud of, send us the code and we may add it to the preset designs.
Wrapping up
For a quick brand match, accent-color styles a slider in one line. For a fully custom look, reset it with appearance: none, style the track and thumb with each browser's pseudo-elements in separate rules, centre the thumb with a little margin maths, and add a gradient fill. Keep a visible focus style and a label, and your slider will look great and work for everyone.