导航

v2.1.0
表单

评分

用原生单选按钮提供星级评分,键盘、表单和屏幕阅读器都能正常使用。

本页内容

评分以星数表示分值。每颗星都是用墨色绘制的原生单选按钮,方向键操作、表单值和辅助技术支持均与普通单选按钮组一致。

评分

<fieldset class="rating" aria-label="Rating">  <input type="radio" class="rating-input" name="rating" value="1" aria-label="1 star">  <input type="radio" class="rating-input" name="rating" value="2" aria-label="2 stars">  <input type="radio" class="rating-input" name="rating" value="3" aria-label="3 stars">  <input type="radio" class="rating-input" name="rating" value="4" aria-label="4 stars" checked>  <input type="radio" class="rating-input" name="rating" value="5" aria-label="5 stars"></fieldset>
<fieldset className="rating" aria-label="Rating">  <input type="radio" className="rating-input" name="rating" value="1" aria-label="1 star" />  <input type="radio" className="rating-input" name="rating" value="2" aria-label="2 stars" />  <input type="radio" className="rating-input" name="rating" value="3" aria-label="3 stars" />  <input type="radio" className="rating-input" name="rating" value="4" aria-label="4 stars" defaultChecked />  <input type="radio" className="rating-input" name="rating" value="5" aria-label="5 stars" /></fieldset>
<fieldset class="rating" aria-label="Rating">  <input type="radio" class="rating-input" name="rating" value="1" aria-label="1 star">  <input type="radio" class="rating-input" name="rating" value="2" aria-label="2 stars">  <input type="radio" class="rating-input" name="rating" value="3" aria-label="3 stars">  <input type="radio" class="rating-input" name="rating" value="4" aria-label="4 stars" checked>  <input type="radio" class="rating-input" name="rating" value="5" aria-label="5 stars"></fieldset>
<template>  <fieldset class="rating" aria-label="Rating">    <input type="radio" class="rating-input" name="rating" value="1" aria-label="1 star">    <input type="radio" class="rating-input" name="rating" value="2" aria-label="2 stars">    <input type="radio" class="rating-input" name="rating" value="3" aria-label="3 stars">    <input type="radio" class="rating-input" name="rating" value="4" aria-label="4 stars" checked>    <input type="radio" class="rating-input" name="rating" value="5" aria-label="5 stars">  </fieldset></template>
悬停预览分值,点击设定分值。

#星星如何填充

单选按钮按从一到五的阅读顺序排列。:has() 会填充选中项及其前面的所有星星:.rating-input:has(~ .rating-input:checked)。同一个选择器配合 :hover 可预览指针所指的分值。在 :has() 出现之前,需要将星星倒序排列,再反转整行的显示方向,导致方向键无法正常操作。

#尺寸

星星默认为 20px。rating-sm 为 16px,适合列表行,rating-lg 为 24px。在触摸屏上,每颗可选的星都有独立的 44px 触控区域,通过拉开星星之间的距离来留出空间,星形不会放大,每颗星保持原有尺寸。仅用于显示分值的星星保持原有间距。

尺寸

<fieldset class="rating rating-sm" aria-label="Small rating">  <input type="radio" class="rating-input" name="rating-sm" aria-label="1 star">  <input type="radio" class="rating-input" name="rating-sm" aria-label="2 stars" checked>  <input type="radio" class="rating-input" name="rating-sm" aria-label="3 stars">  <input type="radio" class="rating-input" name="rating-sm" aria-label="4 stars">  <input type="radio" class="rating-input" name="rating-sm" aria-label="5 stars"></fieldset><fieldset class="rating rating-lg" aria-label="Large rating">  <input type="radio" class="rating-input" name="rating-lg" aria-label="1 star">  <input type="radio" class="rating-input" name="rating-lg" aria-label="2 stars">  <input type="radio" class="rating-input" name="rating-lg" aria-label="3 stars">  <input type="radio" class="rating-input" name="rating-lg" aria-label="4 stars">  <input type="radio" class="rating-input" name="rating-lg" aria-label="5 stars" checked></fieldset>
<>  <fieldset className="rating rating-sm" aria-label="Small rating">    <input type="radio" className="rating-input" name="rating-sm" aria-label="1 star" />    <input type="radio" className="rating-input" name="rating-sm" aria-label="2 stars" defaultChecked />    <input type="radio" className="rating-input" name="rating-sm" aria-label="3 stars" />    <input type="radio" className="rating-input" name="rating-sm" aria-label="4 stars" />    <input type="radio" className="rating-input" name="rating-sm" aria-label="5 stars" />  </fieldset>  <fieldset className="rating rating-lg" aria-label="Large rating">    <input type="radio" className="rating-input" name="rating-lg" aria-label="1 star" />    <input type="radio" className="rating-input" name="rating-lg" aria-label="2 stars" />    <input type="radio" className="rating-input" name="rating-lg" aria-label="3 stars" />    <input type="radio" className="rating-input" name="rating-lg" aria-label="4 stars" />    <input type="radio" className="rating-input" name="rating-lg" aria-label="5 stars" defaultChecked />  </fieldset></>
<fieldset class="rating rating-sm" aria-label="Small rating">  <input type="radio" class="rating-input" name="rating-sm" aria-label="1 star">  <input type="radio" class="rating-input" name="rating-sm" aria-label="2 stars" checked>  <input type="radio" class="rating-input" name="rating-sm" aria-label="3 stars">  <input type="radio" class="rating-input" name="rating-sm" aria-label="4 stars">  <input type="radio" class="rating-input" name="rating-sm" aria-label="5 stars"></fieldset><fieldset class="rating rating-lg" aria-label="Large rating">  <input type="radio" class="rating-input" name="rating-lg" aria-label="1 star">  <input type="radio" class="rating-input" name="rating-lg" aria-label="2 stars">  <input type="radio" class="rating-input" name="rating-lg" aria-label="3 stars">  <input type="radio" class="rating-input" name="rating-lg" aria-label="4 stars">  <input type="radio" class="rating-input" name="rating-lg" aria-label="5 stars" checked></fieldset>
<template>  <fieldset class="rating rating-sm" aria-label="Small rating">    <input type="radio" class="rating-input" name="rating-sm" aria-label="1 star">    <input type="radio" class="rating-input" name="rating-sm" aria-label="2 stars" checked>    <input type="radio" class="rating-input" name="rating-sm" aria-label="3 stars">    <input type="radio" class="rating-input" name="rating-sm" aria-label="4 stars">    <input type="radio" class="rating-input" name="rating-sm" aria-label="5 stars">  </fieldset>  <fieldset class="rating rating-lg" aria-label="Large rating">    <input type="radio" class="rating-input" name="rating-lg" aria-label="1 star">    <input type="radio" class="rating-input" name="rating-lg" aria-label="2 stars">    <input type="radio" class="rating-input" name="rating-lg" aria-label="3 stars">    <input type="radio" class="rating-input" name="rating-lg" aria-label="4 stars">    <input type="radio" class="rating-input" name="rating-lg" aria-label="5 stars" checked>  </fieldset></template>

#半星

rating-half 为每颗星提供两个单选按钮,分值以半星为单位变化。每个单选按钮宽半颗星,显示一整颗星的一半,这颗星立在两个单选按钮的接缝上。因此填充和预览与上文相同,3.5 分会填满七个半星。方向键每次移动半颗星,每个单选按钮都以其分值命名。

半星

<fieldset class="rating rating-half" aria-label="Rating">  <input type="radio" class="rating-input" name="rating-half" value="0.5" aria-label="0.5 stars">  <input type="radio" class="rating-input" name="rating-half" value="1" aria-label="1 star">  <input type="radio" class="rating-input" name="rating-half" value="1.5" aria-label="1.5 stars">  <input type="radio" class="rating-input" name="rating-half" value="2" aria-label="2 stars">  <input type="radio" class="rating-input" name="rating-half" value="2.5" aria-label="2.5 stars">  <input type="radio" class="rating-input" name="rating-half" value="3" aria-label="3 stars">  <input type="radio" class="rating-input" name="rating-half" value="3.5" aria-label="3.5 stars" checked>  <input type="radio" class="rating-input" name="rating-half" value="4" aria-label="4 stars">  <input type="radio" class="rating-input" name="rating-half" value="4.5" aria-label="4.5 stars">  <input type="radio" class="rating-input" name="rating-half" value="5" aria-label="5 stars"></fieldset>
<fieldset className="rating rating-half" aria-label="Rating">  <input type="radio" className="rating-input" name="rating-half" value="0.5" aria-label="0.5 stars" />  <input type="radio" className="rating-input" name="rating-half" value="1" aria-label="1 star" />  <input type="radio" className="rating-input" name="rating-half" value="1.5" aria-label="1.5 stars" />  <input type="radio" className="rating-input" name="rating-half" value="2" aria-label="2 stars" />  <input type="radio" className="rating-input" name="rating-half" value="2.5" aria-label="2.5 stars" />  <input type="radio" className="rating-input" name="rating-half" value="3" aria-label="3 stars" />  <input type="radio" className="rating-input" name="rating-half" value="3.5" aria-label="3.5 stars" defaultChecked />  <input type="radio" className="rating-input" name="rating-half" value="4" aria-label="4 stars" />  <input type="radio" className="rating-input" name="rating-half" value="4.5" aria-label="4.5 stars" />  <input type="radio" className="rating-input" name="rating-half" value="5" aria-label="5 stars" /></fieldset>
<fieldset class="rating rating-half" aria-label="Rating">  <input type="radio" class="rating-input" name="rating-half" value="0.5" aria-label="0.5 stars">  <input type="radio" class="rating-input" name="rating-half" value="1" aria-label="1 star">  <input type="radio" class="rating-input" name="rating-half" value="1.5" aria-label="1.5 stars">  <input type="radio" class="rating-input" name="rating-half" value="2" aria-label="2 stars">  <input type="radio" class="rating-input" name="rating-half" value="2.5" aria-label="2.5 stars">  <input type="radio" class="rating-input" name="rating-half" value="3" aria-label="3 stars">  <input type="radio" class="rating-input" name="rating-half" value="3.5" aria-label="3.5 stars" checked>  <input type="radio" class="rating-input" name="rating-half" value="4" aria-label="4 stars">  <input type="radio" class="rating-input" name="rating-half" value="4.5" aria-label="4.5 stars">  <input type="radio" class="rating-input" name="rating-half" value="5" aria-label="5 stars"></fieldset>
<template>  <fieldset class="rating rating-half" aria-label="Rating">    <input type="radio" class="rating-input" name="rating-half" value="0.5" aria-label="0.5 stars">    <input type="radio" class="rating-input" name="rating-half" value="1" aria-label="1 star">    <input type="radio" class="rating-input" name="rating-half" value="1.5" aria-label="1.5 stars">    <input type="radio" class="rating-input" name="rating-half" value="2" aria-label="2 stars">    <input type="radio" class="rating-input" name="rating-half" value="2.5" aria-label="2.5 stars">    <input type="radio" class="rating-input" name="rating-half" value="3" aria-label="3 stars">    <input type="radio" class="rating-input" name="rating-half" value="3.5" aria-label="3.5 stars" checked>    <input type="radio" class="rating-input" name="rating-half" value="4" aria-label="4 stars">    <input type="radio" class="rating-input" name="rating-half" value="4.5" aria-label="4.5 stars">    <input type="radio" class="rating-input" name="rating-half" value="5" aria-label="5 stars">  </fieldset></template>
将指针移过一颗星的左右两半可预览对应分值,也可用方向键以半星为单位调整分值。

在触摸屏上,每个半星宽 22px,所在行高 44px,因此一颗星的触控目标为 44px。

#显示分值

不供选择的分值,例如 4.3 的平均分,使用 rating-display:一个带 role="img" 的空元素,用文字形式的分值命名,并将 --rating-value 设为该数值。星星从起点填充到该分值,最后一颗星按比例部分填充。--rating-max 设置绘制的星星数量,默认为五颗。尺寸与单选按钮的用法相同。

显示分值

<div class="flex items-center gap-xs text-sm fg-text-muted">  <span class="rating rating-sm rating-display" role="img" aria-label="Rated 4.3 out of 5" style="--rating-value: 4.3"></span> 4.3 from 128 reviews</div>
<div className="flex items-center gap-xs text-sm fg-text-muted">  <span className="rating rating-sm rating-display" role="img" aria-label="Rated 4.3 out of 5" style={{ '--rating-value': '4.3' } as React.CSSProperties}></span> 4.3 from 128 reviews</div>
<div class="flex items-center gap-xs text-sm fg-text-muted">  <span class="rating rating-sm rating-display" role="img" aria-label="Rated 4.3 out of 5" style="--rating-value: 4.3"></span> 4.3 from 128 reviews</div>
<template>  <div class="flex items-center gap-xs text-sm fg-text-muted">    <span class="rating rating-sm rating-display" role="img" aria-label="Rated 4.3 out of 5" style="--rating-value: 4.3"></span> 4.3 from 128 reviews  </div></template>

填充通过 round() 计算整颗星、半个间隙以及最后一颗星的比例。浏览器不支持 round() 时,填充按分值占整行的比例延伸,可能偏差一个像素。

如果沿用评分表单中的单选按钮来显示整星分值,请禁用这些按钮:星星保留原有墨色,并停止响应指针操作。无论采用哪种方式,都要在星星旁用文字标明分值,因为一排星星本身并不是数字。

#无障碍

  • 使用 aria-label 或 <legend> 为单选按钮组提供无障碍名称,并用各自的分值为每个单选按钮命名,半星也一样,例如“3.5 星”。
  • 用文字形式的分值为 rating-display 命名,例如“评分 4.3 星,满分 5 星”。星星是该数值的图示,role="img" 可避免辅助技术在其中查找子部件。
  • 星形由遮罩裁出,遮罩也会裁掉单颗星的焦点环。因此,只要组内有单选按钮获得键盘焦点,就在整组上显示焦点环。
  • 星星采用墨色而非黄色,界面将彩色留给状态提示。

#类名

类名说明
rating星级评分组,应用于通过 aria-label 或 legend 命名的 fieldset。组负责绘制其单选按钮的焦点环。使用 rating-display 时,则改为评分的图示
rating-input在以分值命名的 radio 上显示星形。各 radio 按阅读顺序从低分到高分排列。
rating-sm16px 的星标,适合列表行。默认大小为 20px与 rating 一同使用
rating-lg星标大小为 24px,默认为 20px与 rating 一同使用
rating-half每颗星对应两个单选按钮,各绘制星形的一半,以半颗星为单位选择评分。用评分为每个单选按钮命名:0.5 颗星、1 颗星、1.5 颗星与 rating 一同使用
rating-display只供阅读、不供选择的评分:带 role="img" 的空元素,以文字形式的评分命名,填充至 --rating-value,最后一颗星部分填充。其中不含单选按钮与 rating 一同使用

#自定义属性

属性默认值说明
--rating-valuerating-display 填充到的评分,例如 4.3;与以文字说明评分的名称一同设置在 rating-display 上设置
--rating-max5rating-display 绘制的星数在 rating-display 上设置

#源码

@master/ui/components/rating.css
/* * Rating: a score out of a few stars, as a group of native radios drawn as stars in ink. * :has() fills every star up to the checked one, and up to the one under the pointer as a preview, so the * markup is five radios in reading order with no reversed rows and no script. * rating-half gives each star two radios, each drawn as one half of it, so the score moves by halves with the * same keys and the same fill. rating-display draws a score that is not a choice, such as an average of 4.3: * one element, the stars cut by a repeated mask and filled from the start to the score, the last one in part. */@layer components.ui {  .rating {    --rating-size: 1.25rem;    display: inline-flex;    gap: .125rem;    min-width: 0;    margin: 0;    padding: 0;    border: 0;    border-radius: calc((var(--radius-chip) - 2px) * var(--corner-scale));    corner-shape: var(--corner-shape);    vertical-align: middle;    /* A star is cut with a mask, which would also cut its own focus ring: the group carries the ring. */    &:has(.rating-input:focus-visible) {      @apply --focus-ring;      outline-offset: 4px;    }    &.rating-sm { --rating-size: 1rem; }    &.rating-lg { --rating-size: 1.5rem; }    /* Two radios to a star: the gap stands between stars, not between the halves of one. */    &.rating-half {      gap: 0;    }    /*     * A score to read, not to set: no radios, the element is the picture. Each star stands in a tile one gap     * wider than itself, so the fill runs whole tiles for the whole stars and the star's own width for the     * fraction of the last. Half a gap overhangs each end and the margin takes it back.     */    &.rating-display {      --rating-max: 5;      --rating-star: var(--rating-size);      --rating-tile: calc(var(--rating-star) + .125rem);      --rating-fill: calc(var(--rating-value, 0) * var(--rating-tile));      display: inline-block;      width: calc(var(--rating-max) * var(--rating-tile));      height: var(--rating-star);      margin-inline: -.0625rem;      background: linear-gradient(90deg, var(--color-text-strong) var(--rating-fill), var(--color-line-strong) 0);      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2.6c.4 0 .8.2 1 .6l2.5 5.2 5.7.8c.9.1 1.3 1.3.6 1.9l-4.1 4 1 5.7c.2.9-.8 1.6-1.6 1.2L12 19.3l-5.1 2.7c-.8.4-1.8-.3-1.6-1.2l1-5.7-4.1-4c-.7-.6-.3-1.8.6-1.9l5.7-.8L11 3.2c.2-.4.6-.6 1-.6Z'/%3E%3C/svg%3E") 0 0 / var(--rating-tile) var(--rating-star) repeat-x;      /* Exact where round() exists: whole tiles, half a gap, then the fraction of one star. */      @supports (width: round(down, 1px, 1px)) {        --rating-fill: calc(round(down, var(--rating-value, 0), 1) * var(--rating-tile) + .0625rem + (var(--rating-value, 0) - round(down, var(--rating-value, 0), 1)) * var(--rating-star));      }      &:dir(rtl) {        background: linear-gradient(270deg, var(--color-text-strong) var(--rating-fill), var(--color-line-strong) 0);      }      @media (forced-colors: active) {        forced-color-adjust: none;        background: linear-gradient(90deg, Highlight var(--rating-fill), GrayText 0);        &:dir(rtl) {          background: linear-gradient(270deg, Highlight var(--rating-fill), GrayText 0);        }      }    }    /* The 44px targets of a touch screen stand edge to edge, each around a star of the same size as before. */    @media (pointer: coarse) {      &:has(.rating-input:not(:disabled)) {        gap: 0;      }    }  }  .rating-input {    flex: none;    width: var(--rating-size);    height: var(--rating-size);    margin: 0;    padding: 0;    border: 0;    outline: none;    background: var(--color-line-strong);    mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2.6c.4 0 .8.2 1 .6l2.5 5.2 5.7.8c.9.1 1.3 1.3.6 1.9l-4.1 4 1 5.7c.2.9-.8 1.6-1.6 1.2L12 19.3l-5.1 2.7c-.8.4-1.8-.3-1.6-1.2l1-5.7-4.1-4c-.7-.6-.3-1.8.6-1.9l5.7-.8L11 3.2c.2-.4.6-.6 1-.6Z'/%3E%3C/svg%3E") center / contain no-repeat;    appearance: none;    cursor: pointer;    touch-action: manipulation;    -webkit-tap-highlight-color: transparent;    transition: background-color var(--duration-quick) var(--easing-glide), scale var(--duration-quick) var(--easing-glide);    /* Filled: the checked star and every star before it. */    &:is(:checked, :has(~ .rating-input:checked)) {      background: var(--color-text-strong);    }    &:active:not(:disabled) {      scale: .88;      transition-duration: var(--duration-press);    }    &:disabled {      cursor: default;    }    /*     * On a touch screen a star that can be chosen stands in a 44px target: the mask keeps the star's size and the     * box around it grows, so the stars part instead of swelling. Stars sit too close for a target to reach past     * its own box, so here the room between them is what grows; a star that only shows a score does not move.     */    @media (pointer: coarse) {      &:not(:disabled) {        width: 2.75rem;        height: 2.75rem;        mask-size: var(--rating-size);      }    }    @media (prefers-reduced-motion: reduce) {      &:active:not(:disabled) {        scale: none;      }    }    @media (forced-colors: active) {      background: GrayText;      forced-color-adjust: none;      &:is(:checked, :has(~ .rating-input:checked)) {        background: Highlight;      }    }  }  /*   * Half stars. A radio is half a star wide and shows one half of a whole star centred on the seam between   * the pair: the first of each pair its start half, the second its end half. The fill and the preview are   * the ones above, so a score of 3.5 fills seven halves.   */  .rating-half .rating-input {    --rating-star: var(--rating-size);    width: calc(var(--rating-star) / 2);    mask-size: var(--rating-star);    mask-position: calc(100% + var(--rating-star) / 2) center;    &:nth-child(even of .rating-input) {      mask-position: calc(var(--rating-star) / -2) center;    }    &:nth-child(odd of .rating-input):not(:nth-child(1 of .rating-input)) {      margin-inline-start: .125rem;    }    /* Right to left the pair turns around: the first radio is the half on the right. */    &:dir(rtl) {      mask-position: calc(var(--rating-star) / -2) center;      &:nth-child(even of .rating-input) {        mask-position: calc(100% + var(--rating-star) / 2) center;      }    }    /* On a touch screen a half is 22px wide and the 44px of height, so a star is a 44px target. */    @media (pointer: coarse) {      &:not(:disabled) {        width: 1.375rem;      }      &:nth-child(odd of .rating-input):not(:nth-child(1 of .rating-input)) {        margin-inline-start: 0;      }    }  }  /* Under the pointer the stars preview the score it would set: filled up to the pointer, empty after it. */  @media (hover: hover) {    .rating:has(.rating-input:hover:not(:disabled)) .rating-input {      background: var(--color-line-strong);      &:is(:hover, :has(~ .rating-input:hover)) {        background: var(--color-text-strong);      }      /* The stars are kept out of forced colors, so the preview is drawn in the system's colors too. */      @media (forced-colors: active) {        background: GrayText;        &:is(:hover, :has(~ .rating-input:hover)) {          background: Highlight;        }      }    }  }}