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. That name needs the bs- prefix from postcss-prefix-custom-properties when you compile Sass yourself—see CSS variables.
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.
<label for="range1" class="form-label">Example range</label>
<div class="form-range">
<input type="range" class="form-range-input" id="range1">
</div>
Disabled
Add the disabled boolean attribute on the input to give it a grayed out appearance, remove pointer events, and prevent focusing.
<label for="disabledRange" class="form-label">Disabled range</label>
<div class="form-range">
<input type="range" class="form-range-input" id="disabledRange" disabled>
</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.
<label for="range2" class="form-label">Example range</label>
<div class="form-range">
<input type="range" class="form-range-input" min="0" max="5" id="range2">
</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".
<label for="range3" class="form-label">Example range</label>
<div class="form-range">
<input type="range" class="form-range-input" min="0" max="5" step="0.5" id="range3">
</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.
<label for="rangeBubble" class="form-label">Brightness</label>
<div class="form-range" data-bs-bubble>
<input type="range" class="form-range-input" min="0" max="100" value="60" id="rangeBubble">
</div>
To change the bubble after initialization, change data-bs-bubble and call update(). JavaScript options passed to the constructor still take priority over data attributes.
const element = document.querySelector('.form-range')
element.dataset.bsBubble = 'true'
bootstrap.Range.getOrCreateInstance(element).update()
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.
<label for="rangeTicks" class="form-label">Temperature</label>
<div class="form-range">
<input type="range" class="form-range-input" min="0" max="100" step="25" value="50" list="rangeTicksList" id="rangeTicks">
</div>
<datalist id="rangeTicksList">
<option value="0" label="Cold"></option>
<option value="25"></option>
<option value="50" label="Mild"></option>
<option value="75"></option>
<option value="100" label="Hot"></option>
</datalist>
After you change the datalist options, the input’s list, or its min and max, call update() to rebuild the tick marks.
With form field
Wrap the .form-range in a .form-field to pair it with a label and description.
<div class="form-field">
<label for="rangeField" class="form-label">Volume</label>
<div class="form-range" data-bs-bubble>
<input type="range" class="form-range-input" id="rangeField">
</div>
<small class="form-text">Adjust the volume level.</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.
When compiling Sass yourself, apply the bs- prefix with postcss-prefix-custom-properties. The Range plugin writes --bs-range-fill, which must match your compiled CSS. See CSS variables.
After setting input.value in JavaScript, call update() on the instance to refresh its display. Native form reset refreshes the fill and bubble automatically after the browser restores the value. If you change which form owns the input, call update() to refresh that association.
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
The range plugin requires the following JavaScript files if you’re building Bootstrap’s JS from source:
| File | Description |
|---|---|
js/src/range.ts
|
Main range component |
js/src/base-component.ts
|
Base component class |
js/src/dom/data.ts
|
Element data store |
js/src/dom/event-handler.ts
|
Event handling utilities |
js/src/dom/manipulator.ts
|
Data attribute manipulation |
js/src/dom/selector-engine.ts
|
DOM selector utilities |
js/src/util/component-functions.ts
|
Shared component helpers |
js/src/util/config.ts
|
Configuration base class |
js/src/util/index.ts
|
Core utility functions |
Options
| Name | Type | Default | Description |
|---|---|---|---|
bubble | boolean | false | Show a value bubble above the thumb. |
formatter | function | null | (value) => string used for bubble text. Tick labels use the datalist options’ labels. |
Methods
| Method | Description |
|---|---|
update() | Refresh the fill and bubble from the input’s current value, apply current data attributes, and rebuild tick marks. JavaScript constructor options retain priority. |
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 during initialization, explicit updates, native input and change events, and completed form resets. 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
Range use local CSS variables on .form-range for real-time customization. Values for the CSS variables
are generated from Sass maps unique to each component and applied to the aforementioned class.
// stylelint-disable-next-line scss/dollar-variable-default
$range-tokens: defaults(
(
--range-track-width: 100%,
--range-track-height: var(--range-thumb-height),
--range-track-rail-height: .25rem,
--range-track-cursor: pointer,
--range-track-bg: color-mix(in oklch, var(--fg-body) 10%, transparent),
--range-track-border-radius: var(--radius-pill),
--range-track-box-shadow: none,
--range-track-fill-bg: var(--theme-bg, light-dark(var(--black), var(--white))),
--range-track-fill-direction: to right,
--range-track-disabled-bg: color-mix(in oklch, var(--fg-body) 30%, var(--bg-body)),
--range-thumb-width: 1.25rem,
--range-thumb-height: var(--range-thumb-width),
--range-thumb-bg: var(--bg-body),
--range-thumb-border-width: 3px,
--range-thumb-border-color: var(--range-track-fill-bg),
--range-thumb-border-radius: var(--radius-pill),
--range-thumb-box-shadow: none,
--range-thumb-active-bg: color-mix(in oklch, var(--range-track-fill-bg) 15%, var(--bg-body)),
--range-thumb-focus-bg: var(--range-track-fill-bg),
--range-thumb-focus-inset: 3px,
--range-thumb-transition-property: "background-color, border-color, box-shadow",
--range-thumb-transition-duration: .15s,
--range-thumb-transition-timing: ease-in-out,
--range-tick-width: var(--border-width),
--range-tick-height: .5rem,
--range-tick-bg: var(--border-color),
),
$range-tokens
);
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.
@mixin range-thumb() {
box-sizing: border-box;
width: var(--range-thumb-width);
height: var(--range-thumb-height);
appearance: none;
border: var(--range-thumb-border-width) solid var(--range-thumb-border-color);
@include gradient-bg(var(--range-thumb-bg));
@include border-radius(var(--range-thumb-border-radius));
@include box-shadow(var(--range-thumb-box-shadow));
@include transition-props(
var(--range-thumb-transition-property),
var(--range-thumb-transition-duration),
var(--range-thumb-transition-timing)
);
&:active {
@include gradient-bg(var(--range-thumb-active-bg));
}
}
@mixin range-track() {
// Fill ends at the thumb center. The Range plugin keeps `--range-fill` (0–1) in sync.
--range-fill-end: calc((var(--range-thumb-width) * .5) + var(--range-fill, 0) * (100% - var(--range-thumb-width)));
width: var(--range-track-width);
height: var(--range-track-height);
color: transparent;
cursor: var(--range-track-cursor);
// The fill is as tall as the thumb. The unfilled rail is a thin band centered behind it.
background:
linear-gradient(
var(--range-track-fill-direction),
var(--range-track-fill-bg) var(--range-fill-end),
transparent var(--range-fill-end)
), linear-gradient(var(--range-track-bg), var(--range-track-bg)) center / 100% var(--range-track-rail-height) no-repeat;
border-color: transparent;
@include border-radius(var(--range-track-border-radius));
@include box-shadow(var(--range-track-box-shadow));
}