탐색

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>
별에 호버해 점수를 미리 보고 클릭해 확정하세요.

#별 채우기 방식

라디오 버튼은 읽기 순서에 따라 1점부터 5점까지 배치합니다. :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는 별마다 라디오 버튼을 두 개씩 두어 점수를 0.5 단위로 조절할 수 있게 합니다. 각 라디오 버튼은 별 반 개 너비이며, 한 쌍의 경계에 걸친 별 하나의 절반을 각각 표시합니다. 따라서 채우기와 미리 보기 표시는 위의 예시와 같고, 3.5점은 반쪽 일곱 개를 채웁니다. 방향키로 점수를 0.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>
별 위로 포인터를 움직여 반쪽씩 확인하거나, 방향키로 점수를 0.5점씩 조절해 보세요.

터치 화면에서는 각 반쪽이 44px 높이의 행에서 22px 너비가 되므로, 별 하나는 44px 크기의 터치 영역이 됩니다.

#점수 표시

평균 4.3점처럼 선택할 수 없는 점수에는 rating-display를 사용합니다. role="img"가 있는 빈 요소 하나에 점수를 풀어 쓴 접근 가능한 이름을 지정하고, --rating-value에 숫자를 설정하세요. 별은 처음부터 점수까지 채워지며, 마지막 별은 일부만 채워집니다. --rating-max는 그릴 별의 개수를 정하며 기본값은 5개입니다. 크기는 라디오 버튼과 같은 방식으로 적용됩니다.

점수 표시

<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()를 지원하지 않으면 채우기가 전체 행에서 점수가 차지하는 비율만큼 이어지며, 1픽셀 정도 어긋날 수 있습니다.

사용자가 선택할 때 쓰는 라디오 버튼으로 정수 단위의 점수를 표시하려면 라디오 버튼을 비활성화하세요. 별은 잉크 색을 유지하며 포인터 입력에 반응하지 않습니다. 어느 방식이든 별만으로는 정확한 점수를 알 수 없으므로 옆에 점수를 텍스트로 적으세요.

#접근성

  • aria-label 또는 <legend>로 그룹에 접근 가능한 이름을 지정하고, 각 라디오 버튼에는 해당 점수를 이름으로 지정하세요. 반 별도 마찬가지입니다(예: "3.5 stars").
  • rating-display에는 “5점 만점에 4.3점”처럼 점수를 풀어 쓴 접근 가능한 이름을 지정하세요. 별은 숫자를 나타내는 그림이며, role="img"는 보조 기술이 그 안에서 하위 요소를 찾지 않게 합니다.
  • 별 모양을 만드는 마스크가 별 자체의 포커스 윤곽선까지 잘라내므로, 라디오 버튼에 키보드 포커스가 있는 동안에는 그룹 전체에 포커스 윤곽선을 표시합니다.
  • 별에는 기본 전경색을 사용합니다. 노란색 같은 색조는 상태를 나타낼 때만 사용합니다.

#클래스

클래스설명
rating별 그룹은 aria-label 또는 legend로 이름을 지정한 fieldset에 배치합니다. 라디오 버튼의 포커스 윤곽선은 그룹에 표시합니다. rating-display를 사용하면 대신 점수를 그림으로 표시합니다.
rating-input라디오 버튼에 별을 표시하고 해당 점수를 접근 가능한 이름으로 지정합니다. 가장 낮은 점수부터 읽는 순서대로 배치합니다
rating-sm목록의 한 행에 쓰는 16px 별입니다. 기본값은 20px입니다.적용할 클래스는 rating입니다
rating-lg24px 별입니다. 기본값은 20px입니다.적용할 클래스는 rating입니다
rating-half별 하나에 라디오 버튼 두 개를 두고 각각 별의 절반으로 그려 0.5점 단위로 점수를 선택하게 합니다. 각 라디오 버튼에는 별 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;        }      }    }  }}