HTML / HTML Forms with Advanced Controls

Adding Range Sliders for User Input

This tutorial will teach you how to implement range sliders for numeric input. Range sliders are a user-friendly way to get numeric input from users.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Explores form elements with advanced features like file upload, range sliders, and date pickers.

1. Introduction

Goal

The purpose of this tutorial is to guide you on how to implement range sliders for numeric input in your web projects.

Learning Outcome

By the end of this tutorial, you will be able to create an HTML range slider, style it using CSS, and manipulate its values with JavaScript.

Prerequisites

Basic knowledge of HTML, CSS, and JavaScript is required.

2. Step-by-Step Guide

HTML Range Input

The HTML <input type="range"> defines a control for inputting a number whose exact value is not important. This is often used in the case of data that falls within a set range of values, like volume or brightness control.

CSS Styling

The appearance of the range slider can be improved with CSS. The properties used to style sliders vary slightly between browsers.

JavaScript Interaction

You can use JavaScript to interact with the slider, read its current value, or change the value programmatically.

3. Code Examples

Basic HTML Range Slider

Here's how to create a basic slider that goes from 0 to 100.

<input type="range" min="0" max="100" value="50" id="myRange">

In this code snippet:
- type="range" creates the range slider.
- min="0" and max="100" set the minimum and maximum values for the slider.
- value="50" sets the initial value for the slider.
- id="myRange" is used to identify the slider in our JavaScript code.

Styling with CSS

Now let's add some style. Note that the styling properties can differ between browsers.

input[type=range] {
  width: 100%;
  height: 25px;
  background: #d3d3d3;
  outline: none;
  opacity: 0.7;
  transition: opacity .2s;
}

input[type=range]:hover {
  opacity: 1;
}

In this code snippet:
- We select the range input with input[type=range].
- width: 100% makes the slider take up the full width of its container.
- height: 25px sets a fixed height.
- background: #d3d3d3 sets a gray background color.
- outline: none removes the default browser outline.
- The opacity and transition properties add a simple hover effect.

Interacting with JavaScript

Finally, let's use JavaScript to display the current value of the slider.

var slider = document.getElementById("myRange");
var output = document.getElementById("value");
output.innerHTML = slider.value;

slider.oninput = function() {
  output.innerHTML = this.value;
}

In this code snippet:
- We get a reference to our slider with document.getElementById("myRange").
- We also get a reference to an element where we'll display the value.
- We then set the initial value with output.innerHTML = slider.value.
- Whenever the slider's value changes (slider.oninput), we update the displayed value.

4. Summary

In this tutorial, we've covered how to create a range slider with HTML, style it with CSS, and interact with it using JavaScript.

5. Practice Exercises

  1. Create a range slider that goes from -50 to 50 with a default value of 0.
  2. Style the slider from exercise 1 in a way that pleases you.
  3. Use JavaScript to display the slider's value in real-time as a user interacts with it.

Remember to practice, experiment with different values, styles, and try to create more complex interactions. Explore the MDN Web Docs for more detailed information about <input type="range">, CSS styling, and JavaScript interactivity.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Open Graph Preview Tool

Preview and test Open Graph meta tags for social media.

Use tool

Age Calculator

Calculate age from date of birth.

Use tool

Date Difference Calculator

Calculate days between two dates.

Use tool

Random Number Generator

Generate random numbers between specified ranges.

Use tool

XML Sitemap Generator

Generate XML sitemaps for search engines.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help