data-range
Cross-browser styled range slider with optional value bubble, prefix/suffix formatting, and tick markers. Covers data-range, data-bubble, and data-markers.
Overview
The data-range attribute enhances a native <input type="range"> with cross-browser styling, an optional value bubble, prefix/suffix formatting, and tick markers. This is a combined page covering data-range, data-bubble, and data-markers.
How It Works
Add data-range to any <input type="range">. The init script:
- Wraps the input in a
.range-wrapperdiv for layout control - Normalizes cross-browser track and thumb styling via
appearance: none - Sets a
--range-pctCSS custom property (0–100%) on everyinputevent - If
data-bubbleis present, creates an<output>element positioned above the thumb - If
data-markersis present, renders tick marks along the track based on thestepattribute - If a
<datalist>is linked vialist, renders labeled tick marks from the option elements
The underlying input remains a real form control. It submits with the form and fires native input and change events.
Attributes
| Attribute | Type | Description |
|---|---|---|
data-range |
boolean | Enables cross-browser range styling and the --range-pct custom property. |
data-bubble |
boolean | Shows a floating value bubble above the thumb that updates on input. |
data-prefix |
string | Text prepended to the bubble value (e.g., "$"). |
data-suffix |
string | Text appended to the bubble value (e.g., "%", "°"). |
data-markers |
boolean | Renders tick marks along the track at each step interval. |
data-range-init |
boolean | Set automatically to prevent double-binding. Do not set manually. |
Value Bubble
Add data-bubble to display a floating value readout above the thumb. The bubble tracks the thumb position and updates in real time as the user drags.
Prefix and Suffix
Use data-prefix and data-suffix to format the bubble value. The prefix appears before the number and the suffix after it.
Tick Markers
Add data-markers to render tick marks along the track. Ticks are generated at each step interval between min and max.
Datalist Labels
Link a <datalist> via the native list attribute to render labeled tick marks. Each <option> in the datalist becomes a labeled marker on the track.
With Form Field
Wrap in <form-field> for helper text and consistent spacing with other form controls.
CSS Custom Property
The --range-pct custom property is set on the input element and updated on every input event. Its value ranges from 0% to 100%, representing the thumb position relative to the track. Use it for filled-track effects or other dynamic styling.
DOM Structure
After initialization, the following DOM structure is created:
.range-wrapper— layout container wrapping the input<output for="inputId">— the value bubble (only withdata-bubble).range-markers— tick mark container (only withdata-markers).range-labels— label container (only with a linked<datalist>)
Styling
The track and thumb are fully customizable via CSS custom properties. All styles are gated on [data-range-init] so the input renders normally without JavaScript.
Dynamic Elements
Range inputs added to the DOM after page load are automatically enhanced via a MutationObserver. No manual initialization is needed.
Accessibility
- The
<output for="inputId">element semantically links the bubble to the input for screen readers - Native keyboard support is preserved — arrow keys adjust the value, Home/End jump to min/max
- A visible
<label>is required for the input - Tick labels from
<datalist>provide additional context for sighted users prefers-reduced-motion: bubble transitions are disabled when reduced motion is preferred- Without JavaScript, the range input renders as a standard browser slider — progressive enhancement