Skip to content

Rating Radio Group Example

Rating Radio Group 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

Following is an example of a rating input that demonstrates the Radio Group Pattern. The rating is indicated by the number of stars selected by the user.

Similar examples include:

Example

Rating

Accessibility Features

  • To enhance perceivability when operating the radios, visual keyboard focus and hover are styled using CSS:
    • The focus ring has a width of 2 pixels and at least 2 pixels of spacing between it and the star borders.
    • The cursor changes to a pointer when hovering over the stars so it is easier to perceive that the rating stars are interactive.
  • To ensure the borders of the stars and the 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 focus ring border is set to match the foreground color of high contrast mode text by specifying the CSS currentcolor value for the stroke property of the inline SVG polygon to draw the star borders and rect element used to draw the focus ring border. To make the background of the polygon and rect elements match the high contrast background color, the fill-opacity attribute of the rect is set to zero. If specific colors were instead used to specify the stroke and fill properties, those colors would remain the same in high contrast mode, which could lead to insufficient contrast between the star and the background or even make them invisible if their color matched the high contrast mode background.
    Note: The SVG element needs to have the CSS forced-color-adjust property set to auto for the currentcolor value to be updated in high contrast mode. Some browsers do not use auto for the default value.

Keyboard Support

Key Function
Tab
  • Moves focus to the checked radio button in the radiogroup.
  • If a radio button is not checked, focus moves to the first radio button in the group.
Space
  • If the radio button with focus is not checked, changes the state to checked.
  • Otherwise, does nothing.
  • Note: The state where a radio is not checked only occurs on page load.
Down arrow
Right arrow
  • Moves focus to and checks the next radio button in the group.
  • If focus is on the last radio button, moves focus to the first radio button.
  • The state of the previously checked radio button is changed to unchecked.
Up arrow
Left arrow
  • Moves focus to and checks the previous radio button in the group.
  • If focus is on the first radio button, moves focus to and checks the last radio button.
  • The state of the previously checked radio button is changed to unchecked.

Role, Property, State, and Tabindex Attributes

Role Attributes Element Usage
none svg The use of the none role on the SVG element ensures assistive technologies do not interpret the SVG element as an image or some other role.
radiogroup div
  • Identifies the div element as a container for a group of radio buttons.
  • Is not focusable because focus is managed using a roving tabindex strategy as described below.
aria-labelledby="ID_REFERENCE" div Refers to the element that contains the label of the radio group.
radio g Identifies the g element as an ARIA radio button.
aria-label="name" g Defines an accessible name that identifies the number of stars associated with the radio button (e.g. "one star", two stars", ..).
tabindex="-1" g
  • Makes the element focusable but not part of the page Tab sequence.
  • Applied to all radio buttons contained in the radio group except for one that is included in the page Tab sequence.
  • This approach to managing focus is described in the practice for Managing Focus Within Components Using a Roving tabindex.
tabindex="0" g
  • Makes the radio button focusable and includes it in the page Tab sequence.
  • Set on only one radio in the radio group.
  • On page load, is set on the first radio button in the radio group.
  • Moves with focus inside the radio group so the most recently focused radio button is included in the page Tab sequence.
  • This approach to managing focus is described in the practice for Managing Focus Within Components Using a Roving tabindex.
aria-checked="false" g
  • Identifies radio buttons which are not checked.
  • CSS attribute selectors (e.g. [aria-checked="false"]) are used to synchronize the visual states with the value of the aria-checked attribute.
  • The CSS ::before pseudo-element is used to indicate visual state of unchecked radio buttons to support high contrast settings in operating systems and browsers.
aria-checked="true" g
  • Identifies the radio button which is checked.
  • CSS attribute selectors (e.g. [aria-checked="true"]) are used to synchronize the visual states with the value of the aria-checked attribute.
  • The CSS ::before pseudo-element is used to indicate visual state of checked radio buttons to support high contrast settings in operating systems and browsers.

Javascript and CSS Source Code

HTML Source Code

Back to Top

This is an unpublished draft preview that might include content that is not yet approved. The published website is at w3.org/WAI/.