Skip to main content
Login

Sign in to access your account

Rating - National Design System

A row of stars that shows a score, or lets the user pick one

Overview

A rating is a row of stars. With <span> stars it shows a score. With <button> stars the user picks a score. A menu variant puts a score summary in a trigger and the vote in a panel.

Pick another component when:

  • the user answers yes or no: Switch
  • the bar shows progress, not opinion: Progress
  • the page asks for feedback at its end: User Feedback

Markup

Options
Structure
Size
Value
State
More
Preview
<div class="nds-rating" data-rating="3.5"> <span class="nds-rating-star"></span> <span class="nds-rating-star"></span> <span class="nds-rating-star"></span> <span class="nds-rating-star"></span> <span class="nds-rating-star"></span> </div>

Behavior

Display

Stars built from <span> elements show data-rating and nothing else. They do not react to the pointer or the keyboard. Set data-rating to the score, from 0 to the number of stars.

Interactive

Stars built from <button> elements let the user pick a score. Hover shows the score a click would give. A click, Enter or Space sets the score. The script writes it back to data-rating and fires nds:rating:change. The script also writes an aria-label on each star in the page’s language, such as 3 stars, so the buttons need no label in the markup.

Form

The Form structure puts an interactive rating in a .nds-form-group, with a <legend> that asks the question. The stars are buttons, not inputs, so the score posts through a hidden input with data-rating-value. The script writes the score into it on each pick, and 0 when no star is picked. After a form reset, the script puts back the score the page loaded with. User Feedback uses the same input.

Required

data-required on the .nds-form-group makes the score required. Forms validation treats data-rating="0" as empty: it shows “Please choose a rating” and focuses the first star.

The Menu structure puts a display rating and a vote count in a Dropmenu trigger. The panel holds an interactive rating and a Vote button. Put nds-rating-menu on the .nds-dropmenu-menu. The page owns the vote: listen for nds:rating:change, and send the score from your own code.

Built-in Features

  • Auto-initialization

    The script starts every .nds-rating on the page, and every one added or removed later.

  • Screen Reader Score

    A screen reader reads a display rating as one image with its score, such as "3.5 of 5", in the page's language. An interactive rating is a group of radios: each star has its own name, such as "2 stars", and the picked star reads as checked.

  • Mode Detection

    The tag of the stars sets the mode. <button> stars are interactive, <span> stars are display only.

  • Half Stars

    The stars show the score to the nearest half: 4.2 shows 4 stars, 4.3 to 4.7 show four and a half, and 4.8 shows 5. A screen reader still reads the exact score.

  • Keyboard Navigation

    The rating is one tab stop. The arrow keys pick the next or previous score; left and right follow the text direction. Home and End pick the lowest and highest score. Enter and Space pick the focused star. Escape leaves the star.

  • Phone Size

    The large stars shrink from 48px to 40px on phones.

  • Loading Placeholder

    .nds-loading turns the stars into pulsing placeholders.

  • Programmatic Control

    Read, set, disable and enable a rating from code. Each change that picks a new score fires nds:rating:change.

Best Practices

  • Use five stars. Users expect five.
  • Use nds-xs or nds-sm next to a title or a list item. Use the default size in most layouts. Use nds-lg for a featured review.
  • In a List, leave the size class off. The list makes the stars SM, a small list (nds-sm) XS, and a large list (nds-lg) MD.
  • Set data-rating to the score on a display rating. Set it to 0 on an interactive rating that collects a new score.
  • Do not use a rating for a yes or no choice. Use a Switch.
  • Do not use a rating to show progress. Use Progress.
  • Use nds-brand where the secondary color does not fit the page identity.
  • Add a visible label near an interactive rating, such as “Rate this service”. In a form, the group’s <legend> is the label.
  • Put the rating in a Dropmenu when a compact trigger collects the vote.
  • To show a review count beside the stars, write your own text next to the rating. .nds-card-rating is a Card part: use it only in a card.
  • Do not write nds-md. The default size has no class.

API

Other Classes

Class Element Effect
.nds-rating-star a child of .nds-rating One star. A <button> makes the rating interactive
.nds-rating-menu .nds-dropmenu-menu Centers the label and the stars of the vote item in a Dropmenu, and sets the menu padding

Data Attributes

Attribute Element Effect
data-rating .nds-rating The score, from 0 to the number of stars. The script writes the new score here on every pick
data-state~="interactive" .nds-rating The script sets it at load when the first star is a <button> and the rating is not disabled. It turns on hover, press and pointer styles
data-state~="disabled" .nds-rating Write it, with disabled on each star, for a rating that starts disabled. The script also sets it in setDisabled(true) and removes it in setDisabled(false)
data-state~="selected" .nds-rating-star The script sets it on the first N stars, where N is the whole part of the score, and removes it when the score drops
data-state~="half" .nds-rating-star The script sets it on the star after the whole stars when the score rounds to a half
data-state~="preview" .nds-rating-star The script sets it on the stars up to the hovered star, and removes it when the pointer leaves
data-value .nds-rating-star The script writes the 1-based position of the star
data-required .nds-form-group that holds a rating The rating must be above 0 before the form submits
data-rating-value a hidden <input> in the .nds-form-group, or in .nds-rating Holds the score for the form. The script writes it on each pick, and 0 when no star is picked

CSS Custom Properties

Set these on .nds-rating. --star-size also reaches it from a parent, and it wins over the size classes. nds-brand sets the four color properties.

Property Default Controls
--star-size 32px Width and height of each star
--star-color var(--rating-star-default-default) Color of an empty star
--star-selected var(--rating-star-default-selected) Color of a filled star
--star-pressed var(--rating-star-default-pressed) Color of a star while pressed
--star-hovered var(--rating-star-default-hovered) Color of a star on hover, interactive only

Tokens

Set a token at :root, or on a wrapper to reach every rating inside it. Give a token with a Dark mode value a dark override too: see Tokens.

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

TokenPreviewValue
--rating-star-default-defaultvar(--colors-neutral-300)
--rating-star-default-selectedvar(--colors-secondary-600)
--rating-star-default-pressedvar(--colors-secondary-700)
--rating-star-default-hoveredvar(--colors-secondary-500)
--rating-star-brand-defaultvar(--colors-neutral-300)
--rating-star-brand-selectedvar(--colors-primary-600)
--rating-star-brand-pressedvar(--colors-primary-700)
--rating-star-brand-hoveredvar(--colors-primary-500)

JavaScript

The NDS.Rating 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.

The instance is on the element as el.ndsRating. It is there once the component starts: code that runs at page load waits for NDS.init.ready.

Method Effect
NDS.Rating.init() Start every .nds-rating that has no instance
NDS.Rating.create(el) Start one rating and return its instance. It returns the existing instance when there is one
NDS.Rating.destroy(el) The same as el.ndsRating.destroy()
NDS.Rating.validate(el, options) Check the rating group and return { valid, message, rating }. Pass { showMessage: false } to check without a message
NDS.Rating.enable(el) Start the rating if needed, then take it out of disabled
NDS.Rating.getValue(el) Return the score, 0 for no stars, or '' when the rating is not found. el is the rating, its group, its hidden input, its id or a selector
NDS.Rating.setValue(el, value) Set the score and return true. It returns false, and changes nothing, when value is not from 0 to the star count, or the rating is not found. The event fires only when the score changes
NDS.Rating.clear(el) Set the score to 0, with no stars. Return true, or false when the rating is not found
el.ndsRating.setValue(v) The same as NDS.Rating.setValue, on a rating that has started
el.ndsRating.getValue() Return the score
el.ndsRating.clear() Set the score to 0
el.ndsRating.enable(), el.ndsRating.disable() Enable or disable the rating
el.ndsRating.isDisabled() Return whether the rating is disabled
el.ndsRating.destroy() Remove the listeners and el.ndsRating. NDS.Rating.create(el) then starts a new one
Event Fired on Detail
nds:rating:change .nds-rating (bubbles) { rating, value, element }. rating and value are the same score
var rating = document.querySelector('.nds-rating'); rating.addEventListener('nds:rating:change', function (e) { console.log(e.detail.rating); }); rating.ndsRating.setValue(4);

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

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