API
Slider
The native range input, restyled.
Installation
pnpm dlx zazz-ui add slidernpx zazz-ui add slideryarn dlx zazz-ui add sliderbunx zazz-ui add sliderRun zazz-ui init first if the project has no zazz/ folder yet.
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Paint the right theme (background, form controls, scrollbars) before CSS loads -->
<meta name="color-scheme" content="light dark">
<!-- Zazz styles: base layers, then slider in cascade order -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_layers.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_variables.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_shadows.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_breakpoints.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_reset.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_typography.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_view-transitions.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/slider/slider.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-sm.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-md.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-lg.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-xl.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-2xl.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-disabled.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-active.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-focus-visible.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-focus-within.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-hover.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-checked.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-current.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-open.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-stuck.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-starting.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-sm.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-md.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-lg.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-xl.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-2xl.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-disabled.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-active.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-focus-visible.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-focus-within.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-hover.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-checked.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-current.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-open.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-stuck.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-starting.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-flow.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-grid.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-spacing.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-margin.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-sizing.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-typography.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-color.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-effects.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-box.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-pseudo.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_defaults.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-sr.css">
<!-- Import map: bare specifiers in Zazz scripts resolve to pinned, SRI-checked files -->
<script type="importmap">
{
"imports": {
"signal-polyfill": "https://cdn.jsdelivr.net/npm/signal-polyfill@0.2.2/dist/index.js"
},
"integrity": {
"https://cdn.jsdelivr.net/npm/signal-polyfill@0.2.2/dist/index.js": "sha384-0OqM7OXGT6Oz1z98u/geiFn/FUyz4iWexkXHCvbIGXRRDXl6bp6IKc0j78AQ6DEK"
}
}
</script>
<!-- Polyfill: Interest Invokers (interestfor) — native in Chromium only -->
<script type="module" src="https://cdn.jsdelivr.net/npm/invokers@2.2.2/dist/esm/production/interest.js" integrity="sha384-hR2BVNtsS7fIIMwOm+f7MY0JZfuByGhQQfevCBB6evFATKzBsTw0JzH/RxD94z2T" crossorigin="anonymous"></script>
<!-- Zazz behavior: side-effect modules by tag; the rest via module imports -->
<script type="module" src="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/dialog-lifecycle.js"></script>
<!-- Theme: apply the persisted (or preferred) scheme before first paint -->
<script>
(() => {
try {
// A stored choice pins the scheme with data-theme; with no choice the
// page follows the system scheme.
const storedTheme = localStorage.getItem("theme");
if (storedTheme === "dark" || storedTheme === "light") {
document.documentElement.dataset.theme = storedTheme;
}
} catch {
/* no storage access: follow the system scheme */
}
})();
</script>A <head> that loads the base layers and slider from jsDelivr, pinned to 0.14.2. To load the whole kit in two requests, see the CDN install.
Examples
<input
type="range"
name="volume"
min="0"
max="100"
step="10"
value="50"
aria-label="Volume"
style="--max-w: 96"
/>/**
* slider.css — Range input (input[type="range"])
*
* @layer variables, reset
* @requires _layers.css, _variables.css
* @uses appearance: none — redraws track + thumb from theme tokens
* @uses --_touch-min (touch screens): the transparent input grows to the touch floor
* and so does the thumb's box, through a transparent border, while the visible
* thumb keeps its size. A thumb can't have an ::after, so on touch screens its
* border and ring draw inset, it loses its drop shadow, and the visible thumb
* stops a border's width short of each track end.
* @uses sign(): switches the ring inset once the thumb has a hit border
* @uses corner-shape — round by default, so circles stay circles; a hook takes any shape
* @uses the track fill: ::-moz-range-progress in Firefox. Chromium and Safari have no
* progress part, so a view timeline on the thumb (view-timeline + timeline-scope)
* animates --_slider-progress on the track, which paints the fill as a gradient.
* The timeline needs the input to be a scroll container, so there it clips its
* overflow and pads its inline ends to keep the thumb's ring in view, so a
* padding utility on the input moves the fill off the thumb there.
* @supports-gated: an engine without view timelines shows no fill. Nor does
* a right-to-left slider there: Chromium measures an inline view timeline from
* the left in RTL and Safari from the right, and CSS can't tell which it got.
* @uses @container style(--ui-slider-fill-side: end) — the fill runs from the thumb
* to the end; Firefox 151+ (style queries), older engines fill from the start.
* @tokens --ui-slider-* (@layer variables, on :root so subtree hooks work); the thumb
* and track sizes floor at --spacing-control-min
*
* Note: The thumb is centered on the track with a calc'd negative margin so retuning
* either size keeps it aligned. The gradient fill runs left to right; a vertical writing
* mode isn't handled.
*/
@layer variables {
:root {
--ui-slider-ring-color: var(--color-ring);
--ui-slider-track-h: max(calc(var(--spacing) * 1.5), calc(var(--spacing-control-min) * 0.375));
--ui-slider-track-bg: var(--ui-field-bg);
--ui-slider-track-border-width: var(--ui-field-border-width);
--ui-slider-track-border-color: var(--ui-field-border-color);
--ui-slider-track-radius: var(--radius-full);
--ui-slider-track-corner-shape: round;
/* the part of the track between its start and the thumb; `end` fills from the thumb
to the end of the track instead */
--ui-slider-fill-bg: var(--color-primary);
--ui-slider-fill-side: start;
--ui-slider-thumb-size: max(calc(var(--spacing) * 4), var(--spacing-control-min));
--ui-slider-thumb-bg: var(--color-primary);
/* composes into the thumb's box-shadow list with its border and ring: use a
transparent shadow, never `none` */
--ui-slider-thumb-shadow: var(--shadow-xs);
--ui-slider-thumb-border-width: 1px;
--ui-slider-thumb-border-color: var(--ui-field-border-color);
--ui-slider-thumb-radius: var(--radius-full);
--ui-slider-thumb-corner-shape: round;
}
}
/* Thumb plumbing, set on the input and inherited by the thumb pseudo-elements */
@property --_slider-thumb-hit {
syntax: "*";
inherits: true;
}
@property --_slider-thumb-in {
syntax: "*";
inherits: true;
}
@property --_slider-thumb-drop {
syntax: "*";
inherits: true;
}
@property --_slider-thumb-box {
syntax: "*";
inherits: true;
}
@property --_slider-bleed {
syntax: "*";
inherits: true;
}
@property --_slider-fill-before {
syntax: "*";
inherits: true;
}
@property --_slider-fill-after {
syntax: "*";
inherits: true;
}
/* 0 with the thumb at the start, 1 at the end; registered so the timeline can animate it */
@property --_slider-progress {
syntax: "<number>";
inherits: true;
initial-value: 0;
}
/* A view timeline counts up as its subject moves toward the start of the scroller, so
the keyframes run backward. */
@keyframes ui-slider-progress {
from {
--_slider-progress: 1;
}
to {
--_slider-progress: 0;
}
}
@layer reset {
:where(input[type="range"]) {
/* the transparent border that grows the thumb's box to the touch floor; 0 on a mouse */
--_slider-thumb-hit: max(0px, (var(--_touch-min) - var(--ui-slider-thumb-size)) / 2);
/* 1 once the thumb has a hit border: the ring then draws inside the visible thumb */
--_slider-thumb-in: sign(var(--_slider-thumb-hit));
/* the thumb's whole box, hit border included: what travels along the track */
--_slider-thumb-box: calc(var(--ui-slider-thumb-size) + 2 * var(--_slider-thumb-hit));
--_slider-thumb-drop: var(--ui-slider-thumb-shadow);
/* how far the ring reaches past the thumb, which the scroll container must keep in view */
--_slider-bleed: calc(
(var(--ring-offset-width) + var(--ring-width)) * (1 - var(--_slider-thumb-in))
);
/* the fill: from the start to the thumb, or from the thumb to the end */
--_slider-fill-before: var(--ui-slider-fill-bg);
--_slider-fill-after: transparent;
/* derived on the element so a subtree hook keeps the thumb centered on the track */
--ui-slider-thumb-offset: calc(
(var(--ui-slider-track-h) - var(--ui-slider-thumb-size)) / 2 - var(--_slider-thumb-hit) -
var(--ui-slider-track-border-width)
);
/* the ring widths grow from 0 on focus and press, so the ring eases in */
--_ring-offset-width: 0px;
--_ring-width: 0px;
--_ring-fill: color-mix(in oklch, var(--ui-slider-ring-color) var(--ring-opacity), transparent);
-webkit-appearance: none;
appearance: none;
inline-size: 100%;
min-block-size: var(--_touch-min);
background: transparent;
cursor: grab;
border-radius: var(--ui-slider-track-radius);
corner-shape: var(--ui-slider-track-corner-shape);
/* transparent outline keeps focus visible in forced-colors / high-contrast
modes; the visible focus ring lives on the thumb */
outline: var(--outline-width) var(--outline-style) transparent;
outline-offset: var(--outline-offset);
}
/* The view timeline needs the input as its scroller: it clips, so the inline padding
and the block size leave room for the thumb's ring and shadow, and the timeline's
inset takes the padding back out so the thumb's ends of travel are 0 and 1. */
@supports (animation-timeline: view()) {
:where(input[type="range"]) {
overflow: hidden;
padding-inline: var(--_slider-bleed);
min-block-size: max(
var(--_touch-min),
var(--ui-slider-thumb-size) + 2 * var(--_slider-bleed) + 2 * var(--spacing)
);
timeline-scope: --ui-slider-thumb;
}
:where(input[type="range"])::-webkit-slider-thumb {
view-timeline: --ui-slider-thumb inline;
view-timeline-inset: var(--_slider-bleed);
}
/* On the track, not the input: Safari's timeline doesn't reach the input itself.
Longhands only: with the `animation` shorthand, the build's minifier folds the
timeline into it, which no browser parses, and the fill never moves. */
:where(input[type="range"])::-webkit-slider-runnable-track {
animation-name: ui-slider-progress;
animation-duration: 1ms;
animation-timing-function: linear;
animation-fill-mode: both;
animation-timeline: --ui-slider-thumb;
animation-range: contain;
}
}
@container style(--ui-slider-fill-side: end) {
:where(input[type="range"])::-webkit-slider-runnable-track {
--_slider-fill-before: transparent;
--_slider-fill-after: var(--ui-slider-fill-bg);
}
:where(input[type="range"])::-moz-range-track {
background: var(--ui-slider-fill-bg);
}
:where(input[type="range"])::-moz-range-progress {
background: var(--ui-slider-track-bg);
}
}
/* No gradient fill right to left (see the header); after the side query, so it wins */
:where(input[type="range"]:dir(rtl))::-webkit-slider-runnable-track {
--_slider-fill-before: transparent;
--_slider-fill-after: transparent;
}
/* An outer shadow would paint at the edge of the grown thumb box, not the visible thumb */
@media (not (hover: hover)) {
:where(input[type="range"]) {
--_slider-thumb-drop: 0 0 transparent;
}
}
:where(input[type="range"])::-webkit-slider-runnable-track {
block-size: var(--ui-slider-track-h);
inline-size: 100%;
/* the fill's edge sits at the thumb's center, which travels half a thumb in from
each end of the track's content box */
background:
linear-gradient(
to right,
var(--_slider-fill-before)
calc(
var(--_slider-thumb-box) / 2 + var(--_slider-progress) *
(100% - var(--_slider-thumb-box))
),
var(--_slider-fill-after) 0
)
content-box no-repeat,
var(--ui-slider-track-bg);
border: var(--ui-slider-track-border-width) solid var(--ui-slider-track-border-color);
border-radius: var(--ui-slider-track-radius);
corner-shape: var(--ui-slider-track-corner-shape);
}
/* Without a view timeline the progress never moves off 0, so the track stays plain */
@supports not (animation-timeline: view()) {
:where(input[type="range"])::-webkit-slider-runnable-track {
background: var(--ui-slider-track-bg);
}
}
:where(input[type="range"])::-moz-range-track {
block-size: var(--ui-slider-track-h);
inline-size: 100%;
background: var(--ui-slider-track-bg);
border: var(--ui-slider-track-border-width) solid var(--ui-slider-track-border-color);
border-radius: var(--ui-slider-track-radius);
corner-shape: var(--ui-slider-track-corner-shape);
}
:where(input[type="range"])::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
margin-block-start: var(--ui-slider-thumb-offset);
box-sizing: content-box;
inline-size: var(--ui-slider-thumb-size);
block-size: var(--ui-slider-thumb-size);
/* the padding box is the visible thumb; the transparent border is the extra hit area */
border: var(--_slider-thumb-hit) solid transparent;
background-color: var(--ui-slider-thumb-bg);
background-clip: padding-box;
border-radius: calc(var(--ui-slider-thumb-radius) + var(--_slider-thumb-hit));
corner-shape: var(--ui-slider-thumb-corner-shape);
/* the visible border, then the ring outside the thumb (or inside it once the box has
grown), then the drop shadow */
box-shadow:
inset 0 0 0 var(--ui-slider-thumb-border-width) var(--ui-slider-thumb-border-color),
0 0 0 calc(var(--_ring-offset-width) * (1 - var(--_slider-thumb-in)))
var(--ring-offset-shadow-color),
0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * (1 - var(--_slider-thumb-in)))
var(--_ring-fill, var(--ring-shadow-color)),
inset 0 0 0 calc(var(--_ring-offset-width) * var(--_slider-thumb-in))
var(--ring-offset-shadow-color),
inset 0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * var(--_slider-thumb-in))
var(--_ring-fill, var(--ring-shadow-color)),
var(--_slider-thumb-drop);
transition: var(--default-transition);
}
:where(input[type="range"])::-moz-range-progress {
/* inside the track's border, as the gradient fill is in the other engines */
block-size: var(--ui-slider-track-h);
background: var(--ui-slider-fill-bg);
border-radius: var(--ui-slider-track-radius);
corner-shape: var(--ui-slider-track-corner-shape);
}
:where(input[type="range"])::-moz-range-thumb {
appearance: none;
box-sizing: content-box;
inline-size: var(--ui-slider-thumb-size);
block-size: var(--ui-slider-thumb-size);
/* the padding box is the visible thumb; the transparent border is the extra hit area */
border: var(--_slider-thumb-hit) solid transparent;
background-color: var(--ui-slider-thumb-bg);
background-clip: padding-box;
border-radius: calc(var(--ui-slider-thumb-radius) + var(--_slider-thumb-hit));
corner-shape: var(--ui-slider-thumb-corner-shape);
/* the visible border, then the ring outside the thumb (or inside it once the box has
grown), then the drop shadow */
box-shadow:
inset 0 0 0 var(--ui-slider-thumb-border-width) var(--ui-slider-thumb-border-color),
0 0 0 calc(var(--_ring-offset-width) * (1 - var(--_slider-thumb-in)))
var(--ring-offset-shadow-color),
0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * (1 - var(--_slider-thumb-in)))
var(--_ring-fill, var(--ring-shadow-color)),
inset 0 0 0 calc(var(--_ring-offset-width) * var(--_slider-thumb-in))
var(--ring-offset-shadow-color),
inset 0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * var(--_slider-thumb-in))
var(--_ring-fill, var(--ring-shadow-color)),
var(--_slider-thumb-drop);
transition: var(--default-transition);
}
@media (hover: hover) {
:where(input[type="range"]:hover) {
--ui-slider-thumb-border-color: var(--color-primary);
}
}
:where(input[type="range"]:is(:active, :focus-visible)) {
--_ring-offset-width: var(--ring-offset-width);
--_ring-width: var(--ring-width);
}
:where(input[type="range"]:active) {
cursor: grabbing;
}
:where(input[type="range"]:disabled) {
opacity: 0.5;
cursor: not-allowed;
}
}Attributes
This primitive is a composition of other primitives and utilities: it has no presets, slots, or hooks of its own. Copy its example and adapt it.
Hooks
Set a hook on :root, on a subtree, or inline on one element. A state hook (--…--hover) themes that state only; the primitive keeps its other states.
| Hook | Default | State |
|---|---|---|
--ui-slider-fill-bg | var(--color-primary) | — |
--ui-slider-fill-side | start | — |
--ui-slider-ring-color | var(--color-ring) | — |
--ui-slider-thumb-bg | var(--color-primary) | — |
--ui-slider-thumb-border-color | var(--ui-field-border-color) | — |
--ui-slider-thumb-border-width | 1px | — |
--ui-slider-thumb-corner-shape | round | — |
--ui-slider-thumb-offset | calc( (var(--ui-slider-track-h) - var(--ui-slider-thumb-size)) / 2 - var(--_slider-thumb-hit) - var(--ui-slider-track-border-width) ) | — |
--ui-slider-thumb-radius | var(--radius-full) | — |
--ui-slider-thumb-shadow | var(--shadow-xs) | — |
--ui-slider-thumb-size | max(calc(var(--spacing) * 4), var(--spacing-control-min)) | — |
--ui-slider-track-bg | var(--ui-field-bg) | — |
--ui-slider-track-border-color | var(--ui-field-border-color) | — |
--ui-slider-track-border-width | var(--ui-field-border-width) | — |
--ui-slider-track-corner-shape | round | — |
--ui-slider-track-h | max(calc(var(--spacing) * 1.5), calc(var(--spacing-control-min) * 0.375)) | — |
--ui-slider-track-radius | var(--radius-full) | — |