TExcelTAD
Home › Bootstrap › BS5 Range

Bootstrap 5 Range

A range input (<input type="range">) is a slider for choosing a number between a minimum and a maximum — volume, brightness, price limit. Browsers draw sliders in wildly different ways; the .form-range class gives them a consistent Bootstrap look with a theme-coloured thumb.

Basic range

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <label for="volume" class="form-label">Volume</label>
  <input type="range" class="form-range" id="volume">
  <label for="locked" class="form-label">Disabled</label>
  <input type="range" class="form-range" id="locked" disabled>
</body>
</html>

By default a range goes from 0 to 100 in steps of 1, starting in the middle (50).

Min, max and step

Attribute Default Meaning
min 0 Lowest value
max 100 Highest value
step 1 Increment (e.g. 0.5, 5)
value middle of the range Starting value
disabled — Can't be moved
html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <label for="rating" class="form-label">Rating: 1 to 5</label>
  <input type="range" class="form-range" min="1" max="5" step="1" value="3" id="rating">
  <label for="temp" class="form-label">Temperature: 15 to 30 °C in half degrees</label>
  <input type="range" class="form-range" min="15" max="30" step="0.5" value="21.5" id="temp">
  <script>
    for (const id of ['rating', 'temp']) {
      document.getElementById(id).addEventListener('change', (e) => console.log(id, '=', e.target.value));
    }
  </script>
</body>
</html>

The change event fires when the user lets go of the thumb; input fires continuously while dragging.

Showing the current value

A range doesn't show its number by itself. Use the input event to display it live, for example in a badge or an <output> element.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <div class="d-flex justify-content-between">
    <label for="price" class="form-label">Max price</label>
    <output for="price" id="priceOut" class="badge text-bg-primary align-self-start">$250</output>
  </div>
  <input type="range" class="form-range" min="0" max="500" step="10" value="250" id="price">
  <div class="d-flex justify-content-between small text-body-secondary"><span>$0</span><span>$500</span></div>

  <div class="mt-4 p-3 rounded text-center fs-4" id="preview" style="background: hsl(260, 60%, 50%); color: #fff;">Hue 260</div>
  <input type="range" class="form-range" min="0" max="360" value="260" id="hue" aria-label="Hue">
  <script>
    const price = document.getElementById('price');
    price.addEventListener('input', () => {
      document.getElementById('priceOut').textContent = '
  

 + price.value;
    });
    const hue = document.getElementById('hue');
    hue.addEventListener('input', () => {
      const p = document.getElementById('preview');
      p.style.background = `hsl(${hue.value}, 60%, 50%)`;
      p.textContent = 'Hue ' + hue.value;
    });
  </script>
</body>
</html>

Ranges in a settings panel

Combine ranges with the grid and a card to build a compact control panel.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <div class="card">
    <div class="card-header">🎚 Audio</div>
    <div class="card-body">
      <div class="row align-items-center mb-2">
        <label for="bass" class="col-3 col-form-label col-form-label-sm">Bass</label>
        <div class="col-7"><input type="range" class="form-range" min="-10" max="10" value="2" id="bass"></div>
        <div class="col-2 text-end small" id="bassVal">2</div>
      </div>
      <div class="row align-items-center">
        <label for="treble" class="col-3 col-form-label col-form-label-sm">Treble</label>
        <div class="col-7"><input type="range" class="form-range" min="-10" max="10" value="-1" id="treble"></div>
        <div class="col-2 text-end small" id="trebleVal">-1</div>
      </div>
    </div>
  </div>
  <script>
    for (const id of ['bass', 'treble']) {
      const r = document.getElementById(id);
      r.addEventListener('input', () => { document.getElementById(id + 'Val').textContent = r.value; });
    }
  </script>
</body>
</html>

Note: Sliders are hard to use precisely on touch screens and for some motor-impaired users. When an exact value matters, offer a number input as well.

Tip: The thumb colour follows --bs-primary; in dark mode (data-bs-theme="dark") the track darkens automatically.

Test yourself

Which class styles a range slider?

  1. .form-control
  2. .form-range
  3. .custom-range
  4. .range

What are the default min and max of a range input?

  1. 0 and 10
  2. 1 and 100
  3. 0 and 100
  4. -100 and 100

Which event fires continuously while the thumb is dragged?

  1. change
  2. input
  3. slide
  4. drag