Table of Contents
Use oninput() event
HTML file(index.html)
CSS file (style.css)
JavaScript file (index.js)
Example
As you can see in the program above, we use HTML, CSS, and JavaScript to create a custom range slider.

" >As you can see in the program above, we use HTML, CSS, and JavaScript to create a custom range slider.

Use onclick() event
In the above program, we used HTML, CSS and JavaScript, in HTML we create an input field that accepts type as range, then create an HTML button (calculate range), then we create a span Label to display range values.

" >In the above program, we used HTML, CSS and JavaScript, in HTML we create an input field that accepts type as range, then create an HTML button (calculate range), then we create a span Label to display range values.

Home Web Front-end CSS Tutorial How to create a custom range slider using CSS and JavaScript?

How to create a custom range slider using CSS and JavaScript?

Aug 25, 2023 pm 04:45 PM

如何使用 CSS 和 JavaScript 创建自定义范围滑块?

Range Slider is an input field in HTML that accepts the 'range' type. It is used to select values ​​within a specific range given, we can pass the range within the input field as shown in the following code snippet

<input type = “range” min = “0” max = “100”/>
Copy after login

As you can see in the above snippet, the type is equal to the range and we provide min="0" and max="100" values ​​which will be the fields range.

Custom range slider helps customize the field range as per your needs.

In the following article, let us learn how to create a custom range slider using CSS and JavaScript.

Let’s create separate files for each language -

Use oninput() event

The oninput event is an HTML event used to perform an action immediately when the user enters a value in an input field. Here is the code snippet that uses this event -

<input type = ‘’ oninput = ‘event name()’>
Copy after login

The following is an explanation of the code below -

HTML file(index.html)

This is an HTML file and must be saved with the .html extension. In this file we will create an input range field which will be our custom range slider and inside the input field we will set the range. And create a span tag to display the custom range slider value.

The following is the HTML code

index.html

<!DOCTYPE html>
<html lang="en">
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Custom range Sliders</title>
</head>
<body>
   <h1>Custom Range Sliders with HTML, CSS and JavaScript</h1>
   <div class="sliders">
      <p>Custom Range Slider: </p>
      <input type="range" min = "1" max = "100" oninput="Range()" id = 'slider' title = 'range'><br>
      <span id = 'res'></span>
   </div>
</body>
</html>
Copy after login

CSS file (style.css)

This is a CSS file created with the .css extension. Using CSS, we will manage the styling of our HTML pages.

The following is a code snippet that connects CSS files and HTML files -

<link rel="stylesheet" href="index.css">
Copy after login

The following is the CSS code -

style.css

span{
   position: relative;
   top: 20px;
   left: 20px;
   font-size: 30px;
   font-weight: 700;
}

p{
   position: relative;
   left: 10px;
   font-size: 20px;
}

input[type='range']
{
   -webkit-appearance: none;
   width: 400px;
   height: 30px;
   background-color: black;
   border-radius: 60px;
}

#slider::-webkit-slider-thumb{
   -webkit-appearance: none;
   width: 50px;
   height: 50px;
   border-radius: 40px;
   appearance: none;
   cursor: pointer;
   background-color: blue;
}
Copy after login

JavaScript file (index.js)

This is a JavaScript file that must be saved with a .js extension. In JavaScript, we will write a program to get the input range value and display it to the user using the innerHTML attribute.

The following is a code snippet that connects a JavaScript file to an HTML file -

<script src = ‘index.html’>
Copy after login

NOTE - Instead of creating three separate files for HTML, CCS and JavaScript you can create just one HTML file with .html extension, and write CSS code in the tag above the body tag, and write JavaScript code in the <script></script> tag at the end of the body tag or within the head tag. < /p>

The following is the JavaScript program

index.js

 function Range() {
   //fetch the input range value through its id
   let range_value = document.getElementById('slider');
   //fetch the span tag through its id
   let result = document.getElementById('res');
   //show the value using innerHTML property
   res.innerHTML = "Range value is: " + range_value.value;
}
Copy after login

Example

Execute the above HTML, CSS and JavaScript.

<!DOCTYPE html>
<html lang="en">
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Custom range Sliders</title>
   <style>
      span{
         position: relative;
         top: 20px;
         left: 20px;
         font-size: 30px;
         font-weight: 700;
      }

      p{
         position: relative;
         left: 10px;
         font-size: 20px;
      }

      input[type='range']
      {
         -webkit-appearance: none;
         width: 400px;
         height: 30px;
         background-color: black;
         border-radius: 60px;
      }

      #slider::-webkit-slider-thumb{
         -webkit-appearance: none;
         width: 50px;
         height: 50px;
         border-radius: 40px;
         appearance: none;
         cursor: pointer;
         background-color: blue;
      }
   </style>
</head>
<body>
   <h1>Custom Range Sliders with HTML, CSS and JavaScript</h1>
   <script>
      function Range()
      {
         //fetch the input range value through its id
         let range_value = document.getElementById('slider');
         //fetch the span tag through its id
         let result = document.getElementById('res');
         //show the value using innerHTML property
         res.innerHTML = "Range value is: " + range_value.value;
      }
   </script>
   <div class="sliders">
      <p>Custom Range Slider: </p>
      <input type="range" min = "1" max = "100" oninput="Range()" id = 'slider' title = 'range'><br>
      <span id = 'res'></span>
   </div>
</body>
</html>
Copy after login

As you can see in the program above, we use HTML, CSS, and JavaScript to create a custom range slider.

Using HTML, we create the content of the page. We first create an input field that accepts a range as type and pass a minimum value equal to 1 and a maximum value equal to 100 inside the input field as shown below -

<input type = ‘range’ min = ‘1’ max = ‘100’ oninput = ‘eventname()’>
Copy after login

Later, we create an oninput event as shown in the above code snippet, the oninput event is used to calculate the value when the user enters a value in the input field. Then we get the input range value by its id as shown below -

let range_value = document.getElementById('slider');
Copy after login

We get the span tag and through the innerHTML attribute, we display the range slider value as shown below -

res.innerHTML = "Range value is: " + range_value.value;
Copy after login

Use onclick() event

onclick() event is an HTML event that is used to perform an action when the user clicks a specific button. The following is a code snippet using the onclick event

<button onclick = ‘event_name()’>
Copy after login

The following is the procedure to create a custom range slider

The following is the HTML code

index.html

<!DOCTYPE html>
<html lang="en">
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Custom range Sliders</title>
</head>
<body>
   <h2>Custom Range Sliders with HTML, CSS and JavaScript</h2>
   <div class="sliders">
      <p>Custom Range Slider: </p>
      <input type="range" min = "1" max = "100" id = 'slider' title = 'range'><br>
      <button id = 'btn'>Calulate Range</button>
      <span id = 'res'></span>
   </div>
</body>
</html>
Copy after login

The following is the CSS code

style.css

span{ position: relative;
      top: 35px;
      left: 40px;
      font-size: 30px;
      font-weight: 700;
}

p{
   position: relative;
   left: 10px;
   font-size: 20px;
}

input[type='range']
{
   -webkit-appearance: none;
   width: 400px;
   height: 30px;
   background-color: yellow;
   border-radius: 60px;
}

#slider::-webkit-slider-thumb{
   -webkit-appearance: none;
   width: 50px;
   height: 50px;
   border-radius: 40px;
   appearance: none;
   cursor: pointer;
   background-color: red;
}

button{
   width: 150px;
   height: 40px;
   background-color: blue;
   color: white;
   border: none;
   cursor: pointer;
   position: relative;
   left: 20px;
   top: 30px;
   transition: 0.5s;
   border-radius: 5px;
}

button:hover{
   opacity: 0.7;
}
Copy after login

The following is the JavaScript program

index.js

let btn = document.getElementById('btn');
btn.onclick = function() {
   //fetch the input range value through its id
   let range_value = document.getElementById('slider');

   //fetch the span tag through its id
   let result = document.getElementById('res');
         
   //show the value using innerHTML property
   res.innerHTML = "Range value is: " + range_value.value;
}
Copy after login

Example

Execute the above HTML, CSS and JavaScript.




   
   Custom range Sliders
   


   

Custom Range Sliders with HTML, CSS and JavaScript

<script> let btn = document.getElementById('btn'); btn.onclick = function() { //fetch the input range value through its id let range_value = document.getElementById('slider'); //fetch the span tag through its id let result = document.getElementById('res'); //show the value using innerHTML property res.innerHTML = &quot;Range value is: &quot; + range_value.value; } </script>

Custom Range Slider:


Copy after login

In the above program, we used HTML, CSS and JavaScript, in HTML we create an input field that accepts type as range, then create an HTML button (calculate range), then we create a span Label to display range values.

In CSS files, we manage the style of the page. In JavaScript, we get the HTML button by id and then use onclick event as shown below -

let btn = document.getElementById('btn');
btn.onclick = function(){}
Copy after login

Then inside the function, when the user clicks the button, we get the input range and display the value using the innerHTML attribute.

The above is the detailed content of How to create a custom range slider using CSS and JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Create a JavaScript Contact Form With the Smart Forms Framework Create a JavaScript Contact Form With the Smart Forms Framework Mar 07, 2025 am 11:33 AM

This tutorial demonstrates creating professional-looking JavaScript forms using the Smart Forms framework (note: no longer available). While the framework itself is unavailable, the principles and techniques remain relevant for other form builders.

Demystifying Screen Readers: Accessible Forms & Best Practices Demystifying Screen Readers: Accessible Forms & Best Practices Mar 08, 2025 am 09:45 AM

This is the 3rd post in a small series we did on form accessibility. If you missed the second post, check out "Managing User Focus with :focus-visible". In

Adding Box Shadows to WordPress Blocks and Elements Adding Box Shadows to WordPress Blocks and Elements Mar 09, 2025 pm 12:53 PM

The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let&#039;s look at a few examples of how it works in real themes, and what options we have to apply these styles to WordPress blocks and elements.

Create an Inline Text Editor With the contentEditable Attribute Create an Inline Text Editor With the contentEditable Attribute Mar 02, 2025 am 09:03 AM

Building an inline text editor isn't trivial. The process starts by making the target element editable, handling potential SyntaxError exceptions along the way. Creating Your Editor To build this editor, you'll need to dynamically modify the content

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

File Upload With Multer in Node.js and Express File Upload With Multer in Node.js and Express Mar 02, 2025 am 09:15 AM

This tutorial guides you through building a file upload system using Node.js, Express, and Multer. We'll cover single and multiple file uploads, and even demonstrate storing images in a MongoDB database for later retrieval. First, set up your projec

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Mar 04, 2025 am 10:22 AM

This article explores the top PHP form builder scripts available on Envato Market, comparing their features, flexibility, and design. Before diving into specific options, let's understand what a PHP form builder is and why you'd use one. A PHP form

See all articles