I’m trying to customize the default range slider for webkit to something that suits me better. I found this website which allowed me to come up with the following:
input[type="range"]::-webkit-slider-runnable-track
{
height: 5px;
background: pink;
border-radius: 16px;
}
input[type="range"]::-webkit-slider-thumb
{
height: 20px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<p>Audio settings:</p>
<div>
<input type="range" id="volume" name="volume" min="0" max="11" />
<label for="volume">Volume</label>
</div>
<div>
<input type="range" id="cowbell" name="cowbell" min="0" max="100"
value="90" step="10" />
<label for="cowbell">Cowbell</label>
</div>
</body>
</html>
However, as you can probably observe the thumb isn’t centered on the track. So I did some searching and found this page which claims that the following formula should center the thumb on the track:
margin-top = (track height in pixels / 2) – (thumb height in pixels /2)
I tried this, but the thumb is still not centered on the track:
input[type="range"]::-webkit-slider-runnable-track
{
height: 5px;
background: pink;
border-radius: 16px;
}
input[type="range"]::-webkit-slider-thumb
{
margin-top: calc((5px / 2) - (20px / 2));
height: 20px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<p>Audio settings:</p>
<div>
<input type="range" id="volume" name="volume" min="0" max="11" />
<label for="volume">Volume</label>
</div>
<div>
<input type="range" id="cowbell" name="cowbell" min="0" max="100"
value="90" step="10" />
<label for="cowbell">Cowbell</label>
</div>
</body>
</html>
As you can probably observe, I think the formula is wrong and does not center the thumb on the track.
Is there a formula for this? I’d like to be able to play around with the sizes until I find one that suits me best. Some explanation of why the formula works would be helpful also.
2
Answers
}
Just update this css code
Don’t play with the height, adjust only the background property
If you want more advanced styling that works across browsers, check this: https://www.sitepoint.com/css-custom-range-slider/