JSFiddle - React, Tailwind, and code Playground
by Yair Even Or
HTML
<div class="range-slider" style='--min:0; --max:200; --step:1; --value:75; --text-value:"75";'>
<input type="range" min="0" max="200" step="1" value="75" oninput="this.parentNode.style.setProperty('--value',this.value); this.parentNode.style.setProperty('--text-value', JSON.stringify(this.value))">
<output></output>
<div class='range-slider__progress'></div>
</div>
SCSS
body > .range-slider {
width: 90%;
--maxTicksAllowed: 200;
}
body{
display: grid;
place-items: center;
height: 100vh;
}
.range-slider {
--primary-color: #0366d6;
--value-offset-y: var(--ticks-gap);
--value-active-color: white;
--value-background: transparent;
--value-background-hover: var(--primary-color);
--value-font: 700 12px/1 Arial;
--fill-color: var(--primary-color);
--progress-background: #eee;
--progress-radius: 20px;
--track-height: calc(var(--thumb-size) / 2);
--min-max-font: 12px Arial;
--min-max-opacity: 0.5;
--min-max-x-offset: 10%; // 50% to center
--thumb-size: 22px;
--thumb-color: white;
--thumb-shadow: 0 0 3px rgba(0, 0, 0, 0.4), 0 0 1px rgba(0, 0, 0, 0.5) inset,
0 0 0 99px var(--thumb-color) inset;
--thumb-shadow-active: 0 0 0 calc(var(--thumb-size) / 4) inset
var(--thumb-color),
0 0 0 99px var(--primary-color) inset, 0 0 3px rgba(0, 0, 0, 0.4);
--thumb-shadow-hover: var(--thumb-shadow);
--ticks-thickness: 1px;
--ticks-height: 5px;
--ticks-gap: var(
--ticks-height,
0
); // vertical space between the ticks and the progress bar
--ticks-color: silver;
// ⚠️ BELOW VARIABLES SHOULD NOT BE CHANGED
--step: 1;
--ticks-count: Calc(var(--max) - var(--min)) / var(--step);
--maxTicksAllowed: 30;
--too-many-ticks: Min(1, Max(var(--ticks-count) - var(--maxTicksAllowed), 0));
--x-step: Max(
var(--step),
var(--too-many-ticks) * (var(--max) - var(--min))
); // manipulate the number of steps if too many ticks exist, so there would only be 2
--tickInterval: 100/ ((var(--max) - var(--min)) / var(--step)) * var(--tickEvery, 1);
--tickIntervalPerc: calc(
(100% - var(--thumb-size)) / ((var(--max) - var(--min)) / var(--x-step)) *
var(--tickEvery, 1)
);
--value-a: Clamp(
var(--min),
var(--value, 0),
var(--max)
); // default value ("--value" is used in single-range markup)
--value-b: var(--value, 0); // default value
...