Skip to main content
Login

Sign in to access your account

Slider - National Design System

A slider lets the user pick a number, or a range between two numbers, by dragging a thumb along a bar

Overview

A slider is a native <input type="range"> in a form field. The field holds a label, a bar with a thumb, and an <output> that shows the value. The bar fills from the start up to the thumb. A range slider has two thumbs, and the bar fills between them.

Pick another component when:

  • the user must type an exact number: a number field in Forms
  • the user picks one of a few fixed values: Radio buttons
  • the setting is on or off: Switch

Markup

Options
Structure
Size
State
More
Preview
5000
<div class="nds-form-container nds-slider-container"> <div class="nds-form-header"> <label for="slider-budget"> <span class="nds-label">Monthly budget</span> <span class="nds-info">Drag the thumb or use the arrow keys</span> </label> </div> <div class="nds-form-control"> <div class="nds-slider-track"> <input type="range" id="slider-budget" class="nds-slider" min="0" max="20000" step="500" value="5000"> </div> <output for="slider-budget" class="nds-slider-value nds-number-format">5000</output> </div> </div>

Behavior

Range

The nds-slider-range class on the container turns on two thumbs. Each thumb is its own input, so the user can tab to it and move it with the keys. The thumbs cannot cross: a thumb dragged past the other stops at its value.

Stacked

By default the value sits beside the bar, and the bar takes the rest of the row. A wide value leaves a short bar on a phone. The nds-stacked class moves the value above the bar, so the bar takes the full width.

Number Format

The nds-number-format class on the value adds thousands separators. Without it, the value shows as the input holds it, so a year reads 2024. The data-currency attribute adds a currency symbol, with the class. The slider formats the value again on every move. The Numbers page lists the currencies.

Read-only

data-state="readonly" on the container blocks the mouse and the step keys. The thumb can still get the focus, so a screen reader can read the value. Browsers ignore readonly on a range input, so the slider script blocks the keys itself.

Loading

The nds-loading class shows the bar and the value as a skeleton, and hides the thumb. Add it while your script loads the bounds or the value, and remove it once they are set. From a script, NDS.state.add(container, 'loading') also adds the class.

Built-in Features

  • Auto-initialization

    Every nds-slider-container on the page starts on its own, and so does a slider added later. The first slider on a page that had none needs NDS.init.mount() on the new content.

  • Keyboard Control

    The arrow keys move the thumb one step. Home and End move it to the lowest and highest value. Page Up and Page Down move it in larger steps. The focus ring shows on keyboard focus only.

  • Touch Target

    Each thumb takes a click or a touch in a 24px circle, the WCAG 2.5.8 minimum, while the dot you see stays 12px, or 16px with MD. The two thumbs of a range slider have one each.

  • Steady Bar

    The value keeps the width of the widest value the slider can show. The bar does not move as the number of digits changes.

  • Skeleton State

    Until the slider script starts, the bar and the value show as a skeleton and the thumb is hidden. The bar never shows a fill that does not match the value.

  • State Sync

    disabled or readonly on the inputs and the same data-state token on the container stay in sync, in both directions. Write one: the forms script sets the other.

Best Practices

  • Use a slider when the user picks an approximate value, such as a budget or a distance.
  • Use a range slider to filter a list by a lowest and a highest value, such as a price or an age.
  • Keep the <output> beside every input. The user reads the exact value there.
  • Set step to the unit the value snaps to, such as 500 for a budget. Without it, the value moves in steps of 1.
  • Give each input of a range slider an aria-label, such as “Lowest price” and “Highest price”. The one label does not name the two thumbs.
  • Use MD on touch-first screens. Its 16px dot is easier to see under a finger. Both sizes take a touch in a 24px circle.
  • Use Stacked with Currency, so a wide value does not shorten the bar.
  • To set the value from a script, call NDS.Slider.setValue(). After you set input.value yourself, fire an input event on the input. The bar and the value then update.
  • After you change min or max from a script, call NDS.Slider.refresh(container). The value width is measured again for the new bounds.
  • For the label, info text and feedback, see Forms. They work the same on every field.

API

Data Attributes

Attribute Element Effect
min, max, step, value .nds-slider Native range attributes: the bounds, the step and the start value. Without them, the bounds are 0 and 100 and the step is 1
data-currency .nds-slider-value The currency symbol, with nds-number-format. See Numbers
data-state .nds-slider-container disabled or readonly. The forms script copies it to every input in the container

CSS Custom Properties

The script writes --slider-fill-start and --slider-fill-end on .nds-slider-track on every move. Do not set them.

Tokens

Set a token at :root, or on a wrapper to reach every slider inside it. Give a token with a Dark mode value a dark override too: see Tokens. The hovered thumb color also shows on keyboard focus.

Source: the slider group in _sass/tokens/_components.scss.

TokenPreviewValue
--slider-track-bg
var(--background-neutral-light)
Dark mode var(--colors-alpha-white-10)
--slider-track-fill
var(--background-primary)
Dark mode var(--colors-primary-500)
--slider-thumb-bg
var(--background-primary)
Dark mode var(--colors-primary-500)
--slider-thumb-bg-hovered
var(--controls-primary-hovered)
Dark mode var(--colors-primary-400)
--slider-thumb-bordervar(--colors-alpha-black-10)

It also reads these shared tokens. A change to one also changes other components.

Source: the var() reads in _sass/components/_slider.scss.

TokenPreviewValue
--text-primary-paragraph
Read by color

var(--colors-neutral-700)
Dark mode var(--colors-neutral-300)
--focus-outline
Read by outline

var(--colors-base-black)
Dark mode var(--colors-base-white)
--focus-inner
Read by box-shadow

var(--colors-base-white)
Dark mode var(--colors-base-black)

JavaScript

The NDS.Slider script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.

Method Effect
NDS.Slider.init() Starts every slider on the page. It runs once at page load, and a slider added later starts on its own. The first slider on a page that had none needs NDS.init.mount() on the new content
NDS.Slider.refresh(container) Measures one container again, or every container with no argument
NDS.Slider.create(container) Starts one container and returns it
NDS.Slider.destroy(container) Stops one container, or every container with no argument
NDS.Slider.getValue(el) Single slider. Returns its value as a number, or '' for a range or when the slider is not found. el is the container, an input in it, its id or a selector
NDS.Slider.setValue(el, n) Single slider. Moves it as a user does and returns true. It returns false, and changes nothing, when n is not a finite number or is outside the input’s min and max
NDS.Slider.getValues(el) Range slider (.nds-slider-range). Returns [min, max] as numbers, or [] for a single slider or when the slider is not found
NDS.Slider.setValues(el, [min, max]) Range slider. Moves both thumbs and returns true. It returns false, and changes nothing, when a number is bad or min is above max
NDS.Slider.clear(el) Moves a single slider to its min, and a range to its whole span

The slider fires no event of its own. Listen for the native input event on the input.

var input = document.querySelector('#slider-budget'); // Read the value on every move input.addEventListener('input', function () { console.log(Number(input.value)); }); // Set the value from a script, then update the bar and the value input.value = 8000; input.dispatchEvent(new Event('input', { bubbles: true })); // A range slider: read each thumb from its own input var range = document.querySelector('.nds-slider-range'); var lowest = Number(range.querySelector('.nds-slider-min').value); var highest = Number(range.querySelector('.nds-slider-max').value);

The full API is in the banner of _js/nds-slider.js.

Last Modified Date: 05/11/2026 - 12:02 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks