Skip to content
Español

Navigate

Type to search. Press Escape to close.

    Components · css New in 1.1

    Rating

    Stars in two forms, both without JavaScript. The input is a native radio group — one tab stop, arrow keys to change the score, a value that a form sends, and a spoken name for every star. The display is a read-only score drawn by a star mask over a fill, so fractions such as 4.6 out of 5 show exactly, and the whole thing is one element with one accessible name.

    CSS: css/components/rating.css (in ivolt.css). JS: none. New in 1.1.

    Star input and score displayfixtures/rating/basic.htmlOpen alone — Star input and score display
    <div class="iv-stack iv-u-gap-6">
      <fieldset class="iv-rating">
        <legend class="iv-rating__legend">How useful was this page?</legend>
        <input class="iv-rating__input" type="radio" id="rt-1" name="rt" value="1">
        <label class="iv-rating__star" for="rt-1"><span class="iv-u-sr-only">1 star</span></label>
        <input class="iv-rating__input" type="radio" id="rt-2" name="rt" value="2">
        <label class="iv-rating__star" for="rt-2"><span class="iv-u-sr-only">2 stars</span></label>
        <input class="iv-rating__input" type="radio" id="rt-3" name="rt" value="3">
        <label class="iv-rating__star" for="rt-3"><span class="iv-u-sr-only">3 stars</span></label>
        <input class="iv-rating__input" type="radio" id="rt-4" name="rt" value="4" checked>
        <label class="iv-rating__star" for="rt-4"><span class="iv-u-sr-only">4 stars</span></label>
        <input class="iv-rating__input" type="radio" id="rt-5" name="rt" value="5">
        <label class="iv-rating__star" for="rt-5"><span class="iv-u-sr-only">5 stars</span></label>
      </fieldset>
    
      <div class="iv-stack iv-u-gap-2">
        <div class="iv-cluster iv-u-items-center iv-u-gap-2">
          <span class="iv-rating-display" role="img" aria-label="4.6 out of 5" style="--iv-rating-value: 4.6"></span>
          <span class="iv-u-text-sm"><strong>4.6</strong> <span class="iv-u-text-muted">· 128 reviews (sample)</span></span>
        </div>
        <div class="iv-cluster iv-u-items-center iv-u-gap-2">
          <span class="iv-rating-display iv-rating--sm" role="img" aria-label="3 out of 5" style="--iv-rating-value: 3"></span>
          <span class="iv-u-text-sm iv-u-text-muted">Small display, 3 of 5</span>
        </div>
      </div>
    
      <p class="iv-u-m-0 iv-u-text-sm iv-u-text-muted">Demo: the rating is a radio group in a form that sends nothing. Tab reaches it once; the arrow keys change the score.</p>
    </div>
    .iv-root {
      --iv-rating-value: 4.6;
    }

    When to use it

    Use the input when you ask for a judgement on a short, familiar scale: “How useful was this page?”, a product review, a support survey. For anything that needs labels on each step (“strongly disagree … strongly agree”), use a radio list or a segmented control with words instead. Use the display wherever a score is shown next to its count: a product card, a review summary, a plugin listing.

    The input

    <fieldset class="iv-rating">
      <legend class="iv-rating__legend">How useful was this page?</legend>
      <input class="iv-rating__input" type="radio" id="r1" name="r" value="1">
      <label class="iv-rating__star" for="r1"><span class="iv-u-sr-only">1 star</span></label>
      …
    </fieldset>

    Every input is followed by its label, whose text is visually hidden. A star lights when its own input or a later one is checked, which the stylesheet works out with :has(); on an engine without it only the chosen star lights, which is still unambiguous. Under a pointer the hovered star and those before it preview the score. The focus ring goes round the whole group, as for any radio group, and the focused star steps forward. iv-rating--sm and iv-rating--lg change the size.

    The display

    <span class="iv-rating-display" role="img" aria-label="4.6 out of 5"
          style="--iv-rating-value: 4.6"></span>

    The score is a custom property, --iv-rating-value, and the number of stars is --iv-rating-max (5 by default). The fill runs from the inline start, so in a right-to-left page it grows from the right. The display is an image: it needs role="img" and an aria-label that says the score in words. Write the number and the count next to it as visible text too.

    Accessibility

    • The <legend> is the question; each hidden label is the answer (“4 stars”). A screen reader announces “How useful was this page?, 4 stars, radio button, checked, 4 of 5”.
    • Arrow keys change the selection immediately, as in any radio group, so do not submit on change.
    • Stars are drawn with a mask, and in forced colours they switch to the system highlight and grey text colours, keeping lit and unlit stars apart.
    • A disabled input dims its star and the arrow keys skip it.

    Customising

    .reviews {
      --iv-rating-size: 1.25rem;
      --iv-rating-color: var(--iv-color-primary);
      --iv-rating-off: var(--iv-color-border);
    }

    Locals: --iv-rating-size, --iv-rating-color (the warning colour of the theme by default), --iv-rating-off, --iv-rating-value and --iv-rating-max. --iv-rating-fill is the state each star resolves to; read it, do not set it.

    Common mistakes

    • A display without aria-label: an empty span is announced as nothing at all.
    • Visible star labels and a hidden legend — the wrong way round.
    • Giving the radios different name values, which breaks the group.
    • Rounding a 4.6 average to five lit stars: the display shows fractions, let it.

    Class index

    Every class of the family. The full list per stylesheet lives in the reference.

    ClassWhat it does
    iv-ratingThe input group, on a <fieldset>.
    iv-rating__legendThe question.
    iv-rating__inputOne radio.
    iv-rating__starThe star drawn for the radio before it.
    iv-rating--sm · iv-rating--lgSizes (the small one also applies to the display).
    iv-rating-displayRead-only score with fractions.