HTML Range Slider

Range Slider in HTML Forms: Interactivity in One Simple Control

Sliders aren’t just stylish — they’re functional. Whether you're rating bananas or choosing a price range, <input type="range"> offers a fast, touch-friendly way to select numeric values with precision and visual clarity.

What is a Range Input?

A range input displays a horizontal slider. It lets users pick a number within a specified range by dragging a thumb across a track — a far better experience than typing.

Basic Syntax

<input type="range">

But without attributes, it’s not very useful. Let's bring it to life with min, max, and step.

Setting Minimum and Maximum Values

To limit the range of values, add min and max.

<label for="rating">Rate the cherry (1 to 5):</label><br>
<input type="range" id="rating" name="rating" min="1" max="5">
Rate the cherry: [ ●——— ] (drag from 1 to 5)

The user can only select values between 1 and 5 — but what values, exactly?

Controlling Increments with step

Use the step attribute to control the intervals between values.

<input type="range" min="0" max="10" step="2">
Values allowed: 0, 2, 4, 6, 8, 10

This is perfect when you want the user to select even numbers, price brackets, or rating tiers.

Displaying the Selected Value (Bonus: JS Enhancement)

HTML alone doesn’t show the value of a slider as you move it. Here’s a basic enhancement with JavaScript:

<label for="bananaSweetness">Banana Sweetness (1–10):</label><br>
<input type="range" id="bananaSweetness" name="sweetness" min="1" max="10" value="5" 
        oninput="document.getElementById('output').value = this.value">
<output id="output">5</output>
Banana Sweetness: [ ●——— ] 5

The output tag shows the live value as the user slides across the track — clean, responsive, and useful.

Complete Example: Fruit Freshness Survey

<form action="/submit-rating" method="post">

  <label for="fruit">Select a fruit:</label><br>
  <select name="fruit" id="fruit">
    <option>Apple</option>
    <option>Banana</option>
    <option>Cherry</option>
  </select><br><br>

  <label for="freshness">Freshness Rating (0–100):</label><br>
  <input type="range" name="freshness" id="freshness" min="0" max="100" step="10"
          oninput="document.getElementById('val').textContent = this.value">
  <span id="val">50</span><br><br>

  <input type="submit" value="Submit Feedback">

</form>
Select a fruit: [ Banana ▼ ]
Freshness Rating: [ ●——— ] 50
[Submit Feedback]

Best Practices

  • Always define min and max to give sliders meaningful boundaries
  • Use step to control precision — 1 for ratings, 10 or 100 for ranges
  • Use JavaScript to reflect live value — improves usability dramatically
  • Label clearly — users need to know what they’re adjusting

Summary

The HTML range slider is an elegant, interactive way to collect numeric data. You've now learned to:

  • Use <input type="range"> for slider controls
  • Set valid ranges with min, max, and step
  • Show real-time values with a simple JS enhancement

Check your understanding 1

Which HTML input type is used to create a slider for selecting numeric values?
Choose one answer. You can try again.

Answer and explanation

<code>type="range"</code>

The type="range" input renders a horizontal slider allowing numeric selection within a specified range — often used in feedback or settings forms.

Check your understanding 2

The min and max attributes are required to make the range input functional.
Choose one answer. You can try again.

Answer and explanation

False

They are optional. Without them, the default range is from 0 to 100. However, setting them makes the slider more meaningful, especially in use cases like score scales.

Check your understanding 3

Choose the option that lists all and only the correct statements.

  1. min
  2. max
  3. step
  4. pattern
Which attributes are commonly used to control the behavior of a range slider?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 3

min, max, and step define the value range and increment of the slider. pattern is used with text input, not range sliders.

Check your understanding 4

What does the following code do? <input type="range" min="0" max="10" step="2">
Choose one answer. You can try again.

Answer and explanation

Creates a slider that jumps by 2 units from 0 to 10

With step="2", the slider moves in increments of 2 (0, 2, 4, 6, 8, 10). This is useful when you want controlled user input, like rating with even numbers.

Check your understanding 5

The slider created with type="range" can display its selected value without JavaScript.
Choose one answer. You can try again.

Answer and explanation

False

To display the current slider value visually next to the slider, you need JavaScript or a linked output element — HTML alone doesn’t render the value.

Check your understanding 6

Choose the option that lists all and only the correct statements.

  1. Collecting parent contact number
  2. Student satisfaction rating (1 to 5)
  3. Volume control on an e-learning module
  4. Attendance percentage estimate (0 to 100)
Which use cases are ideal for using a range slider in a school web form?
Choose one answer. You can try again.

Answer and explanation

Statements 2 and 3 and 4

Sliders are great for bounded numeric input like rating or estimating ranges. For exact values like contact numbers, type="tel" or number is preferred.

What’s Next?

Let’s brighten things up — in the next tutorial, we’ll explore the Color Picker and how it helps users visually choose colors in your forms.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: HTML Color Picker →