1

Is it possible to restyle a input range to have different colors on the right and left side of the tracker just by CSS? This is what i have gotten so far.

input[type=range] {
   margin: 10px 0;
  margin: 50px;
  width: 500px;
  background: transparent;
   -webkit-appearance: none;
}

input[type=range]::-webkit-slider-runnable-track {
    
    height: 10px;
    
       background: white;
         border-radius: 5px;
  
}


input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    border: none;
    height: 25px;
    width: 25px;
    border-radius: 50%;
    background: white;
    border: 3px green solid;
    margin-top: -7px;
}

input[type=range]:focus {
    outline: none;
}


body {
  background-color: green;
}
<input type="range" min="0" max="10" />

https://jsfiddle.net/Lautaro/k179gwur/1/

Daarwin
  • 2,896
  • 7
  • 39
  • 69
  • I think the only browser that lets you do that is IE using `::-ms-fill-lower` and `::-ms-fill-upper`. See [this other SO question](http://stackoverflow.com/q/28283332/1028949) – jeffjenx May 11 '16 at 15:56
  • @Daarwin The support for such a feature is not given in such a way that it is reliable and consistent across the major browsers. There might be some browsers that support something similar but afaik there is not such a thing. I suggest using JS in this case. – F. Müller Dec 23 '20 at 14:55

2 Answers2

0

Yeah, you could use css gradient.

input[type=range]::-webkit-slider-runnable-track {
   background: linear-gradient(to right, #000000 0%,#000000 50%,#ffffff 50%,#ffffff 100%,#ffffff 100%);
}
VilleKoo
  • 2,827
  • 2
  • 14
  • 23
0

As far as I know it is not possible to do using only CSS.

Alternatively you can easily accomplish it with a tiny bit of javascript.
Use this: https://toughengineer.github.io/demo/slider-styler

for (let e of document.querySelectorAll('input[type="range"].slider-progress')) {
  e.style.setProperty('--value', e.value);
  e.style.setProperty('--min', e.min == '' ? '0' : e.min);
  e.style.setProperty('--max', e.max == '' ? '100' : e.max);
  e.addEventListener('input', () => e.style.setProperty('--value', e.value));
}
/*surrounding background color*/
body {
  background: green;
}

/*generated with Input range slider CSS style generator (version 20201223)
https://toughengineer.github.io/demo/slider-styler*/
input[type=range].styled-slider {
  height: 2.2em;
  -webkit-appearance: none;

  /*added this property to generated CSS to match the surrounding background color*/
  background: transparent;
  /*specify desired width*/
  width: 500px;
}

/*progress support*/
input[type=range].styled-slider.slider-progress {
  --range: calc(var(--max) - var(--min));
  --ratio: calc((var(--value) - var(--min)) / var(--range));
  --sx: calc(0.5 * 2em + var(--ratio) * (100% - 2em));
}

input[type=range].styled-slider:focus {
  outline: none;
}

/*webkit*/
input[type=range].styled-slider::-webkit-slider-thumb {
  width: 2em;
  height: 2em;
  border-radius: 1em;
  background: #007cf8;
  border: none;
  box-shadow: 0 0 2px black;
  margin-top: calc(max((1em - 1px - 1px) * 0.5,0px) - 2em * 0.5);
  -webkit-appearance: none;
}

input[type=range].styled-slider::-webkit-slider-runnable-track {
  height: 1em;
  border-radius: 0.5em;
  background: #efefef;
  border: 1px solid #b2b2b2;
  box-shadow: none;
}
input[type=range].styled-slider::-webkit-slider-thumb:hover {
  background: #0061c3;
}

input[type=range].styled-slider:hover::-webkit-slider-runnable-track {
  background: #e5e5e5;
  border-color: #9a9a9a;
}

input[type=range].styled-slider::-webkit-slider-thumb:active {
  background: #2f98f9;
}

input[type=range].styled-slider:active::-webkit-slider-runnable-track {
  background: #f5f5f5;
  border-color: #c1c1c1;
}

input[type=range].styled-slider.slider-progress::-webkit-slider-runnable-track {
  background: linear-gradient(#007cf8,#007cf8) 0/var(--sx) 100% no-repeat, #efefef;
}

input[type=range].styled-slider.slider-progress:hover::-webkit-slider-runnable-track {
  background: linear-gradient(#0061c3,#0061c3) 0/var(--sx) 100% no-repeat, #e5e5e5;
}

input[type=range].styled-slider.slider-progress:active::-webkit-slider-runnable-track {
  background: linear-gradient(#2f98f9,#2f98f9) 0/var(--sx) 100% no-repeat, #f5f5f5;
}

/*mozilla*/
input[type=range].styled-slider::-moz-range-thumb {
  width: 2em;
  height: 2em;
  border-radius: 1em;
  background: #007cf8;
  border: none;
  box-shadow: 0 0 2px black;
}

input[type=range].styled-slider::-moz-range-track {
  height: max(calc(1em - 1px - 1px),0px);
  border-radius: 0.5em;
  background: #efefef;
  border: 1px solid #b2b2b2;
  box-shadow: none;
}

input[type=range].styled-slider::-moz-range-thumb:hover {
  background: #0061c3;
}

input[type=range].styled-slider:hover::-moz-range-track {
  background: #e5e5e5;
  border-color: #9a9a9a;
}

input[type=range].styled-slider::-moz-range-thumb:active {
  background: #2f98f9;
}

input[type=range].styled-slider:active::-moz-range-track {
  background: #f5f5f5;
  border-color: #c1c1c1;
}

input[type=range].styled-slider.slider-progress::-moz-range-track {
  background: linear-gradient(#007cf8,#007cf8) 0/var(--sx) 100% no-repeat, #efefef;
}

input[type=range].styled-slider.slider-progress:hover::-moz-range-track {
  background: linear-gradient(#0061c3,#0061c3) 0/var(--sx) 100% no-repeat, #e5e5e5;
}

input[type=range].styled-slider.slider-progress:active::-moz-range-track {
  background: linear-gradient(#2f98f9,#2f98f9) 0/var(--sx) 100% no-repeat, #f5f5f5;
}

/*ms*/
input[type=range].styled-slider::-ms-fill-upper {
  background: transparent;
  border-color: transparent;
}

input[type=range].styled-slider::-ms-fill-lower {
  background: transparent;
  border-color: transparent;
}

input[type=range].styled-slider::-ms-thumb {
  width: 2em;
  height: 2em;
  border-radius: 1em;
  background: #007cf8;
  border: none;
  box-shadow: 0 0 2px black;
  margin-top: 0;
  box-sizing: border-box;
}

input[type=range].styled-slider::-ms-track {
  height: 1em;
  border-radius: 0.5em;
  background: #efefef;
  border: 1px solid #b2b2b2;
  box-shadow: none;
  box-sizing: border-box;
}

input[type=range].styled-slider::-ms-thumb:hover {
  background: #0061c3;
}

input[type=range].styled-slider:hover::-ms-track {
  background: #e5e5e5;
  border-color: #9a9a9a;
}

input[type=range].styled-slider::-ms-thumb:active {
  background: #2f98f9;
}

input[type=range].styled-slider:active::-ms-track {
  background: #f5f5f5;
  border-color: #c1c1c1;
}

input[type=range].styled-slider.slider-progress::-ms-fill-lower {
  height: max(calc(1em - 1px - 1px),0px);
  border-radius: 0.5em 0 0 0.5em;
  margin: -1px 0 -1px -1px;
  background: #007cf8;
  border: 1px solid #b2b2b2;
  border-right-width: 0;
}

input[type=range].styled-slider.slider-progress:hover::-ms-fill-lower {
  background: #0061c3;
  border-color: #9a9a9a;
}

input[type=range].styled-slider.slider-progress:active::-ms-fill-lower {
  background: #2f98f9;
  border-color: #c1c1c1;
}
<input type="range" class="styled-slider slider-progress" />
paul
  • 448
  • 4
  • 11