Creating A Range Slider In HTML using JavaScript

On web pages, range sliders are used to let users specify a numeric value within a defined range. Instead of typing exact numbers, users can simply drag a slider control to select their desired value. This makes range sliders perfect for settings where precision isn't critical, such as volume controls, brightness adjustments, or filtering options.

HTML provides the <input type="range"> element for creating basic sliders, but with JavaScript and CSS, we can enhance them to create more interactive and visually appealing controls.

HTML input type="range"

The <input type="range"> creates a slider control for selecting numeric values. By default, it ranges from 0 to 100, but you can customize this behavior using these attributes:

  • max ? Sets the maximum allowed value

  • min ? Sets the minimum allowed value

  • step ? Defines the increment/decrement intervals

  • value ? Sets the initial default value

Creating a Round Range Slider

Let's create a customized round range slider with real-time value display using CSS styling and JavaScript:

<!DOCTYPE html>
<html>
<head>
   <style>
      .tutorial {
         -webkit-appearance: none;
         width: 100%;
         height: 10px;
         background: #ABEBC6;
         opacity: 0.8;
         border-radius: 5px;
      }
      .tutorial:hover {
         opacity: 1;
      }
      .tutorial::-webkit-slider-thumb {
         -webkit-appearance: none;
         width: 20px;
         height: 20px;
         border-radius: 50%;
         background: #DE3163;
         cursor: pointer;
      }
   </style>
</head>
<body style="text-align:center">
   <h2 style="font-family:verdana">Round Range Slider</h2>
   <div class="slidecontainer">
      <input type="range" min="0" max="100" value="10" class="tutorial" id="tp">
      <p style="color:#6C3483">Current Value: <span id="work"></span></p>
   </div>
   
   <script>
      var slider = document.getElementById("tp");
      var output = document.getElementById("work");
      
      // Display initial value
      output.innerHTML = slider.value;
      
      // Update value when slider moves
      slider.oninput = function() {
         output.innerHTML = this.value;
      }
   </script>
</body>
</html>

This creates a smooth, rounded slider with a circular thumb that displays the current value in real-time as users drag it.

Creating an Image Range Slider

For a more creative approach, you can use an image as the slider thumb. Here's how to create an image-based range slider:

<!DOCTYPE html>
<html>
<head>
   <style>
      .image-slider {
         -webkit-appearance: none;
         width: 100%;
         height: 50px;
         background: linear-gradient(to right, #DE3163, #FF6B6B);
         opacity: 0.7;
         border-radius: 25px;
      }
      .image-slider:hover {
         opacity: 1;
      }
      .image-slider::-webkit-slider-thumb {
         -webkit-appearance: none;
         width: 60px;
         height: 60px;
         background: url('/images/slider-thumb.png') no-repeat center;
         background-size: contain;
         cursor: pointer;
         border-radius: 50%;
      }
   </style>
</head>
<body>
   <h2 style="font-family:verdana; text-align:center">Image Range Slider</h2>
   <div class="slidecontainer" style="margin: 50px auto; width: 80%;">
      <input type="range" min="5" max="100" value="15" class="image-slider" id="imageSlider">
      <p style="text-align:center; font-size:18px; margin-top:20px">
         Current Value: <span id="imageValue"></span>
      </p>
   </div>
   
   <script>
      var imageSlider = document.getElementById("imageSlider");
      var imageValue = document.getElementById("imageValue");
      
      // Initialize display
      imageValue.innerHTML = imageSlider.value;
      
      // Update value on slider movement
      imageSlider.oninput = function() {
         imageValue.innerHTML = this.value;
         console.log("Slider value changed to:", this.value);
      }
   </script>
</body>
</html>

This example creates a more visually appealing slider with gradient background and custom image thumb. The JavaScript provides real-time feedback and console logging for debugging.

Key JavaScript Functions

The essential JavaScript functionality for range sliders includes:

  • oninput event ? Triggers when the slider value changes

  • value property ? Gets/sets the current slider value

  • innerHTML ? Updates the display text with current value

Browser Compatibility

Feature Chrome Firefox Safari
Basic range input ? ? ?
-webkit-slider-thumb ? ? ?
-moz-range-thumb ? ? ?

Conclusion

JavaScript-enhanced range sliders provide an intuitive way for users to select values within a defined range. By combining HTML5 range inputs with CSS styling and JavaScript event handling, you can create engaging, responsive slider controls that enhance user experience on your web pages.

Updated on: 2026-03-15T23:19:01+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements