Custom Range Slider with HTML, CSS & JavaScript
01/21/2021
Demo
Code
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Custom Range Slider</title>
<link rel="stylesheet" type="text/css" href="https://demo.plantpot.works/assets/css/normalize.css">
<link rel="stylesheet" href="https://use.typekit.net/opg3wle.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="container">
<form class="form">
<input id="slider" class="slider" type="range" min="0" max="100" value="0">
<div id="value" class="value"></div>
</form>
<h1>Custom Range Slider</h1>
</div>
</body>
</html>
CSS
@charset "utf-8";
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-size: 16px;
}
body {
background-color: #fff7b0;
font-family: futura-pt, sans-serif;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
#container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
padding: 10px;
}
.form {
display: grid;
grid-template-columns: 80% 19%;
grid-column-gap: 1%;
justify-content: center;
align-items: center;
position: relative;
width: 100%;
max-width: 400px;
margin-left: 30px;
}
.slider {
-webkit-appearance: none;
height: 6px;
border: none;
border-radius: 3px;
background-color: #cdad70;
outline: none;
cursor: pointer;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
background: #E00650;
width: 18px;
height: 18px;
border-radius: 50%;
border: none;
}
.value {
color: #e00650;
font-weight: 500;
font-size: 2rem;
text-align: center;
}
h1 {
margin: 30px 0 0;
color: #e00650;
font-size: 3rem;
letter-spacing: 1px;
text-align: center;
}
@media screen and (max-width: 480px) {
.form {
width: 90%;
margin-left: 20px;
}
.value {
font-size: 1.25rem;
}
h1 {
margin: 20px 0 0;
font-size: 2rem;
}
}
JavaScript
var slider = document.getElementById("slider");
var val = document.getElementById("value");
val.innerHTML = slider.value;
slider.oninput = function() {
val.innerHTML = this.value;
}