Range

Source: https://getbootstrap.com/docs/6.0/forms/range

Use our custom range inputs for consistent cross-browser styling with a filled track, value bubble, and tick marks.

Overview

The native <input type="range"> is hard to style and can’t show a filled track in a consistent, cross-browser way using CSS alone—only Firefox offers ::-moz-range-progress, and CSS can’t read an input’s value. So our range is a small JavaScript component: wrap an <input class="form-range-input"> in .form-range and the plugin keeps a --bs-range-fill custom property (0–1) in sync, which the CSS uses to draw the fill, the value bubble, and tick marks.

The .form-range wrapper owns the component’s tokens, so the input and any decorations inherit them. Every .form-range on the page is initialized automatically.

<Example code={&lt;label for="range1" class="form-label">Example range&lt;/label> &lt;div class="form-range"> &lt;input type="range" class="form-range-input" id="range1"> &lt;/div>} />

Theme colors

Modify the appearance of the range by adding the .theme-{color} class to the .form-range wrapper. This will set the track fill and thumb color to the theme color.

<Example class="vstack gap-3" code={getData('theme-colors').map((themeColor) => &lt;div class="form-range theme-${themeColor.name}"> &lt;input type="range" class="form-range-input" aria-label="Example ${themeColor.name} range"> &lt;/div>)} />

Disabled

Add the disabled boolean attribute on the input to give it a grayed out appearance, remove pointer events, and prevent focusing.

<Example code={&lt;label for="disabledRange" class="form-label">Disabled range&lt;/label> &lt;div class="form-range"> &lt;input type="range" class="form-range-input" id="disabledRange" disabled> &lt;/div>} />

Min and max

Range inputs have implicit values for min and max—0 and 100, respectively. You may specify new values for those using the min and max attributes.

<Example code={&lt;label for="range2" class="form-label">Example range&lt;/label> &lt;div class="form-range"> &lt;input type="range" class="form-range-input" min="0" max="5" id="range2"> &lt;/div>} />

Steps

By default, range inputs “snap” to integer values. To change this, you can specify a step value. In the example below, we double the number of steps by using step="0.5".

<Example code={&lt;label for="range3" class="form-label">Example range&lt;/label> &lt;div class="form-range"> &lt;input type="range" class="form-range-input" min="0" max="5" step="0.5" id="range3"> &lt;/div>} />

Value bubble

Add data-bs-bubble to the wrapper to show a value bubble that follows the thumb. The bubble reuses our tooltip styles.

<Example code={&lt;label for="rangeBubble" class="form-label">Brightness&lt;/label> &lt;div class="form-range" data-bs-bubble> &lt;input type="range" class="form-range-input" min="0" max="100" value="60" id="rangeBubble"> &lt;/div>} />

Tick marks

Link a <datalist> to the input with the list attribute and the plugin renders a tick mark for each <option>, positioned at its value (even when the values are unevenly spaced). An <option>’s label is shown beneath its tick.

<Example code={&lt;label for="rangeTicks" class="form-label">Temperature&lt;/label> &lt;div class="form-range"> &lt;input type="range" class="form-range-input" min="0" max="100" step="25" value="50" list="rangeTicksList" id="rangeTicks"> &lt;/div> &lt;datalist id="rangeTicksList"> &lt;option value="0" label="Cold">&lt;/option> &lt;option value="25">&lt;/option> &lt;option value="50" label="Mild">&lt;/option> &lt;option value="75">&lt;/option> &lt;option value="100" label="Hot">&lt;/option> &lt;/datalist>} />

With form field

Wrap the .form-range in a .form-field to pair it with a label and description.

&lt;label for="rangeField" class="form-label">Volume&lt;/label>
&lt;div class="form-range" data-bs-bubble>
  &lt;input type="range" class="form-range-input" id="rangeField">
&lt;/div>
&lt;small class="form-text">Adjust the volume level.&lt;/small>

</div>`} />

Usage

Every .form-range is initialized automatically on page load—the component is JavaScript-driven, so the fill won’t render without the plugin.

Via data attributes

Attribute Description
data-bs-bubble Add to the .form-range wrapper to show a value bubble.
list (on the input) + <datalist> Renders tick marks from the datalist options.
<div class="form-range" data-bs-bubble>
  <input type="range" class="form-range-input" min="0" max="100" value="40">
</div>

Via JavaScript

const element = document.querySelector('.form-range')
const range = new bootstrap.Range(element, {
  bubble: true,
  formatter: value => `${value}%`
})

Dependencies

Options

Name Type Default Description
bubble boolean false Show a value bubble above the thumb.
formatter function null (value) => string used for the bubble and tick label text.

Methods

Method Description
update() Recompute the fill from the input’s current value. Useful after setting the value programmatically.
dispose() Destroys the instance and removes the bubble and tick marks it created.
getInstance() Static method to get the instance associated with a .form-range element.
getOrCreateInstance() Static method to get the instance, or create one if it doesn’t exist.

Events

Event Description
changed.bs.range Fired on the input when the value updates (on input and change). The event’s value property holds the current numeric value.
const input = document.querySelector('.form-range-input')

input.addEventListener('changed.bs.range', event => {
  console.log('Value:', event.value)
})

CSS

Variables

Sass mixins

Two mixins generate the vendor-specific styles for the range’s track and thumb. Note that selectors for pseudo-elements in WebKit and Firefox cannot be combined—each must be used in a separate CSS rule otherwise the styles will not apply.