CodeBlanks Logo Icon CodeBlanks

Range and color

HTML course · Advanced · Lesson 21/56

Use <input type="range"> and <input type="color">.

The range slider

<input type="range"> shows a slider. The user picks a value inside a range. You set the bounds with min, max and step.

Example: <input type="range" min="0" max="100" value="50">. This is a volume slider between 0 and 100; the starting value is 50.

value sets the slider's initial position. As the user drags, the value changes.

The colour picker

<input type="color"> shows a colour picker. When the user clicks, the operating system's colour panel opens.

The value is always a six-digit hex code: #rrggbb. Example: <input type="color" value="#3366cc">. The default value is #000000 (black).

The picker is handy when you do not want the user to type a colour in a specific format; the browser always produces a valid hex value.

Exercise Write a volume slider and theme colour

Add a <form> inside body. Put two labelled inputs inside: a volume slider <input type="range" id="volume" min="0" max="100"> and a theme colour <input type="color" id="theme">. Match each with a <label for="...">.

Show example solution
<body>
  <form>
    <label for="volume">Volume</label>
    <input type="range" id="volume" name="volume" min="0" max="100" value="50">
    <label for="theme">Theme colour</label>
    <input type="color" id="theme" name="theme" value="#3366cc">
  </form>
</body>

Test yourself

  1. Which input type is used for a volume slider?

    • number
    • text
    • color
    • range
    Show answer

    range — Correct. type="range" makes a slider.

  2. Complete the upper bound of the range input

    <input type="range" min="0" max="____" value="50">
    Show answer
    <input type="range" min="0" max="100" value="50">
  3. Match each input type to its use

    range
    Volume slider
    color
    Theme colour
    email
    Email address
    number
    Age

All lessons in Advanced