網站導覽

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>
讓指標橫越一顆星,即可看到它的兩半;也可以用方向鍵以半顆星為單位移動。

在觸控螢幕上,每半顆星在 44px 高的列中寬 22px,因此一顆星的觸控區域為 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() 讀出完整星數、半個間距,以及最後一顆星的比例。瀏覽器不支援時,填色會依分數占整列的比例延伸,可能偏差一個像素。

若要用讀者會操作的單選按鈕顯示整數星的分數,請停用它們:星星會保留原有的填色,並停止回應指標操作。無論採用哪種方式,都請在旁邊以文字標示分數,因為一列星星並不是數字。

#無障礙

  • 用 aria-label 或 <legend> 為群組命名,並以各自對應的分數為每個單選按鈕命名,半顆星也一樣,例如「3.5 顆星」。
  • 以文字描述分數為 rating-display 命名,例如「評分 4.3 分,滿分 5 分」。星星是數字的圖像;role="img" 讓輔助技術不會在其中尋找各個部分。
  • 星形由遮罩裁切而成,個別星星的焦點外框也會被裁掉。因此,只要其中一個單選按鈕取得鍵盤焦點,就在整個群組上顯示焦點外框。
  • 星星沿用介面文字色。黃色等色相保留給狀態提示。

#類別

類別說明
rating以 fieldset 將評分星星組成一組,並用 aria-label 或 legend 命名。組內的單選按鈕取得焦點時,焦點外框會顯示在群組上。使用 rating-display 時,則改為呈現分數的圖像
rating-input每顆星星使用一個以其分數命名的單選按鈕。單選按鈕依閱讀順序從最低分排起。
rating-sm16px 星形圖示,適用於清單項目。預設為 20px。搭配 rating 使用
rating-lg24px 星形圖示,預設為 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;        }      }    }  }}