How to create: Range slider

Learn how to create custom range sliders using CSS and JavaScript.

Default:

Square:

Dot:

Image:

Value:

Try It Yourself

Create a range slider

First step - Add HTML:

<div class="slidecontainer">
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
</div>

Second step - Add CSS:

.slidecontainer {
  width: 100%; /* Width of the external container */
}
/* The slider itself */
.slider {
  -webkit-appearance: none;  /* Cover the default CSS style */
  appearance: none;
  width: 100%; /* Full width */
  height: 25px; /* Specified height */
  background: #d3d3d3; /* Grey background */
  outline: none; /* Remove outline */
  opacity: 0.7; /* Set transparency (used for the mouse hover effect) */
  -webkit-transition: .2s; /* 0.2s transition effect */
  transition: opacity .2s;
}
/* Mouse hover effect */
.slider:hover {
  opacity: 1; /* Fully displayed when the mouse hovers */
}
/* Slider handle (use -webkit- for Chrome, Opera, Safari, Edge and -moz- for Firefox to override the default appearance) */
.slider::-webkit-slider-thumb {
  -webkit-appearance: none; /* Cover the default appearance */
  appearance: none;
  width: 25px; /* Set a specific slider handle width */
  height: 25px; /* Slider handle height */
  background: #04AA6D; /* Green background */
  cursor: pointer; /* Cursor when hovering */
}
.slider::-moz-range-thumb {
  width: 25px; /* Set a specific slider handle width */
  height: 25px; /* Slider handle height */
  background: #04AA6D; /* Green background */
  cursor: pointer; /* Cursor when hovering */
}

Try It Yourself

Step 3 - Add JavaScript:

Create a dynamic range slider with JavaScript to display the current value:

var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = slider.value; // Display the default slider value
// Update the current slider value (each time the slider thumb is dragged)
slider.oninput = function() {
  output.innerHTML = this.value;
}

Try It Yourself

Circular slider

To create a circular slider handle, use border-radius property.

Tip:If you want uneven heights (15 pixels and 25 pixels in this example), set the slider height to a different value than the slider thumb:

Instance

.slider {
  -webkit-appearance: none;
  width: 100%;
  height: 15px;
  border-radius: 5px;  
  background: #d3d3d3;
  outline: none;
  opacity: 0.7;
  -webkit-transition: .2s;
  transition: opacity .2s;
}
.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 25px;
  height: 25px;
  border-radius: 50%; 
  background: #04AA6D;
  cursor: pointer;
}
.slider::-moz-range-thumb {
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: #04AA6D;
  cursor: pointer;
}

Try It Yourself

Slider icon/image

To create a slider handle with an icon/image, use background Insert image URL by using the

Instance

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 23px;
  height: 24px;
  border: 0;
  background: url('contrasticon.png');
  cursor: pointer;
}
.slider::-moz-range-thumb {
  width: 23px;
  height: 25px;
  border: 0;
  background: url('contrasticon.png');
  cursor: pointer;
}

Try It Yourself