4

I have a simple HTML range input component and I would like to divide the track to three different parts. I have a range of 0 to 75 in the component. I would like to style 0 to 25 as green, 26 to 50 as yellow and 51 to 75 as red irrespective of the input value, ie., the colors are constant. Is it possible to it? Here is the working jsfiddle

var p = document.getElementById("price"),
    res = document.getElementById("result");

p.addEventListener("input", function() {
    res.innerHTML =  p.value;
}, false); 
<div style="margin-top: 1em">
    <h2>Price</h2>
    0<input id="price" type="range" min="0" max="75" value="" />75
</div>

<p id="result"></p>

enter image description here

Brr Switch
  • 974
  • 1
  • 9
  • 21

1 Answers1

5

With a linear-gradient background

body {
text-align:center;
}  

#range::-webkit-slider-runnable-track {
  width: 300px;
  height: 10px;
  background: linear-gradient(to right, green, green 25%, yellow 25%, yellow 50%, red 51%);
  border: none;
  border-radius: 3px;
}

#range::-moz-range-track {
  width: 300px;
  height: 10px;
  background: linear-gradient(to right, green, green 25%, yellow 25%, yellow 50%, red 51%);
  border: none;
  border-radius: 3px;
}
<input id="range" type="range">
Paulie_D
  • 107,962
  • 13
  • 142
  • 161