Skip to content

Rating Slider Example

Rating Slider Example

Read This First

The code in this example is not intended for production environments. Before using it for any purpose, read this to understand why.

This is an illustrative example of one way of using ARIA that conforms with the ARIA specification.

About This Example

Warning!

Some users of touch-based assistive technologies may experience difficulty utilizing widgets that implement this slider pattern because the gestures their assistive technology provides for operating sliders may not yet generate the necessary output. To change the slider value, touch-based assistive technologies need to respond to user gestures for increasing and decreasing the value by synthesizing key events. This is a new convention that may not be fully implemented by some assistive technologies. Authors should fully test slider widgets using assistive technologies on devices where touch is a primary input mechanism before considering incorporation into production systems.

Following is an example of a rating input that demonstrates the Slider Pattern. This rating widget employs a slider because it enables users to choose from ten rating values, which is a relatively large number of choices for users to navigate. For inputs with seven or fewer choices, another pattern that could be used is radio group as demonstrated by the Rating Radio Group Example. However, when there are more than seven choices, other patterns provide additional keyboard commands that significantly increase efficiency for users who rely on keyboard navigation to perceive options and make a selection. These include slider, spin button, combobox, and listbox.

Similar examples include:

  • Rating Radio Group Example: Radio group that provides input for a five star rating scale.
  • Color Viewer Slider Example: Basic horizontal sliders that illustrate setting numeric values for a color picker.
  • Vertical Temperature Slider Example: Demonstrates using aria-orientation to specify vertical orientation and aria-valuetext to communicate unit of measure for a temperature input.
  • Media Seek Slider Example: Horizontal slider that demonstrates using aria-valuetext to communicate current and maximum values of time in media to make the values easy to understand for assistive technology users by converting the total number of seconds to minutes and seconds.
  • Horizontal Multi-Thumb Slider Example: Demonstrates using sliders with two thumbs to provide inputs for numeric ranges, such as for searching in a price range.

Example

Rate your satisfaction with the service you received

Accessibility Features

  • To ensure assistive technology users correctly understand the meaning of the current value, this example uses the aria-valuetext property to communicate the current value, maximum value, and the meaning of the maximum value (Extremely Satisfied). However, since repeating the maximum value every time the slider value changes is potentially distracting, the maximum value is included in aria-valuetext only when the slider is initialized and when the thumb loses keyboard focus.
  • To highlight the interactive nature of the satisfaction rating, a focus ring appears around the group of rating options when the slider has focus and a rating value has not yet been set.
  • To ensure the borders of the rating values and focus ring have sufficient contrast with the background when high contrast settings invert colors, the color of the borders is synchronized with the color of the text content using a CSS media query selector (e.g. @media (forced-colors: active)) . For example, the color of the rating value borders is set to match the link foreground color of high contrast mode text by specifying the CSS canvas and linkText values for the stroke and fill properties of each inline SVG rect and text element. If specific colors were used to specify the stroke and fill properties, the color of these elements would remain the same in high contrast mode, which could lead to insufficient contrast between them and their background or even make them invisible if their color were to match the high contrast mode background.
    Note: The SVG element needs to have the CSS forced-color-adjust property set to the value auto for the currentcolor value to be updated in high contrast modes. Some browsers do not use auto for the default value.

Keyboard Support

Key Function
Right Arrow Increases slider by one rating step.
Up Arrow Increases slider by one rating step.
Left Arrow Decreases slider by one rating step.
Down Arrow Decreases slider by one rating step.
Page Up Increases slider value multiple steps. In this slider, by two rating steps.
Page Down Decreases slider value multiple steps. In this slider, by two rating steps.
Home Sets slider to its minimum value, extremely dissatisfied.
End Sets slider to its maximum value, extremely satisfied.

Role, Property, State, and Tabindex Attributes

Role Attribute Element Usage
slider div
  • Identifies the element as a slider.
  • Set on the div that represents the movable thumb because it is the operable element that controls the slider value.
tabindex="0" div Includes the slider thumb in the page tab sequence.
aria-valuemax="10" div Specifies the maximum value of the slider.
aria-valuemin="0" div Specifies the minimum value of the slider.
NOTE: 0 indicates a rating value has not yet been set.
aria-valuenow="NUMBER" div Indicates the current value of the slider.
aria-valuetext="STRING" div
  • A string that provides a user-friendly name for the current value.
  • When the value is 0, 1, or 10, provides the name of the value.
  • When initialized and when the slider loses focus, the string also includes the number of rating values and the meaning of the maximum value, e.g., seven out of 10 where 10 is extremely satisfied.
aria-labelledby="ID_REFERENCE" div Refers to the element containing the name of the slider.
aria-hidden="true" svg Removes the SVG elements from the accessibility tree to prevent assistive technologies from presenting them as elements separate from the slider.

JavaScript and CSS Source Code

HTML Source Code

Back to Top