Rating Slider 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 design pattern. This rating widget employs a slider because the slider pattern supports step values of any size. This particular input enables half-star steps. A typical five-star rating widget that allows only five possible values could instead be implemented as a radio group.
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 andaria-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
Accessibility Features
-
To ensure assistive technology users correctly perceive the maximum slider value, this example uses the
aria-valuetext
property to communicate both the current and maximum values. However, since repeating the maximum value every time the slider value changes is potentially distracting, the maximum value is included inaria-valuetext
only when the slider is initialized and when the thumb loses keyboard focus. - To highlight the interactive nature of the rating stars, a focus ring appears around the group of stars when the thumb has focus.
-
To ensure the borders of the stars and focus ring have sufficient contrast with the background when high contrast settings invert colors, the color of the borders are synchronized with the color of the text content.
For example, the color of the star borders is set to match the foreground color of high contrast mode text by specifying the CSS
currentColor
value for thestroke
property of each inline SVGpolygon
element. To enable the high contrast background color to be the used as the contrasting color when a star is not fully or partially filled, thefill-opacity
attribute of thepolygon
is set to zero. If specific colors were used to specify thestroke
andfill
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 CSSforced-color-adjust
property set to the valueauto
for thecurrentColor
value to be updated in high contrast modes. Some browsers do not useauto
for the default value.
Keyboard Support
Key | Function |
---|---|
Right Arrow | Increases slider one half star. |
Up Arrow | Increases slider one half star. |
Left Arrow | Decreases slider one half star. |
Down Arrow | Decreases slider one half star. |
Page Up | Increases slider value multiple steps. In this slider, jumps one star. |
Page Down | Decreases slider value multiple steps. In this slider, jumps one star. |
Home | Sets slider to its minimum value, no stars. |
End | Sets slider to its maximum value, five stars. |
Role, Property, State, and Tabindex Attributes
Role | Attribute | Element | Usage |
---|---|---|---|
slider
|
div
|
|
|
tabindex="0"
|
div
|
Includes the slider thumb in the page tab sequence. | |
aria-valuemax="5"
|
div
|
Specifies the maximum value of the slider. | |
aria-valuemin="0"
|
div
|
Specifies the minimum value of the slider. | |
aria-valuenow="NUMBER"
|
div
|
Indicates the current value of the slider. | |
aria-valuetext="STRING"
|
div
|
|
|
aria-labelledby="IDREF"
|
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
- CSS: slider-rating.css
- Javascript: slider-rating.js
HTML Source Code