網站導覽

v2.1.0
表單

量尺

從代表兩端之間不同程度的一列數字中選出一個答案。

本頁內容

量尺讓使用者從一列數字中選一個答案,表示可能性、滿意度或難度等程度。每個數字都用 <label> 包住原生單選按鈕,因此使用者仍可用方向鍵在群組內切換,表單也能取得選取值。未選取的方格以單一外框勾勒,選取的方格則以深色凸顯。

量尺

<div class="card w-full max-w(480)">  <fieldset class="fieldset">    <legend class="fieldset-legend">How likely are you to recommend Atlas to a colleague?</legend>    <div class="scale">      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="0">0      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="1">1      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="2">2      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="3">3      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="4">4      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="5">5      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="6">6      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="7">7      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="8" checked>8      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="9">9      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="10">10      </label>      <p class="scale-ends"><span>Not at all likely</span><span>Extremely likely</span></p>    </div>  </fieldset></div>
<div className="card w-full max-w(480)">  <fieldset className="fieldset">    <legend className="fieldset-legend">      How likely are you to recommend Atlas to a colleague?    </legend>    <div className="scale">      <label className="scale-option">        <input className="scale-input" type="radio" name="recommend" value="0" />0      </label>      <label className="scale-option">        <input className="scale-input" type="radio" name="recommend" value="1" />1      </label>      <label className="scale-option">        <input className="scale-input" type="radio" name="recommend" value="2" />2      </label>      <label className="scale-option">        <input className="scale-input" type="radio" name="recommend" value="3" />3      </label>      <label className="scale-option">        <input className="scale-input" type="radio" name="recommend" value="4" />4      </label>      <label className="scale-option">        <input className="scale-input" type="radio" name="recommend" value="5" />5      </label>      <label className="scale-option">        <input className="scale-input" type="radio" name="recommend" value="6" />6      </label>      <label className="scale-option">        <input className="scale-input" type="radio" name="recommend" value="7" />7      </label>      <label className="scale-option">        <input className="scale-input" type="radio" name="recommend" value="8" defaultChecked />8      </label>      <label className="scale-option">        <input className="scale-input" type="radio" name="recommend" value="9" />9      </label>      <label className="scale-option">        <input className="scale-input" type="radio" name="recommend" value="10" />10      </label>      <p className="scale-ends"><span>Not at all likely</span><span>Extremely likely</span></p>    </div>  </fieldset></div>
<div class="card w-full max-w(480)">  <fieldset class="fieldset">    <legend class="fieldset-legend">How likely are you to recommend Atlas to a colleague?</legend>    <div class="scale">      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="0">0      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="1">1      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="2">2      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="3">3      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="4">4      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="5">5      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="6">6      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="7">7      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="8" checked>8      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="9">9      </label>      <label class="scale-option">        <input class="scale-input" type="radio" name="recommend" value="10">10      </label>      <p class="scale-ends"><span>Not at all likely</span><span>Extremely likely</span></p>    </div>  </fieldset></div>
<template>  <div class="card w-full max-w(480)">    <fieldset class="fieldset">      <legend class="fieldset-legend">How likely are you to recommend Atlas to a colleague?</legend>      <div class="scale">        <label class="scale-option">          <input class="scale-input" type="radio" name="recommend" value="0">0        </label>        <label class="scale-option">          <input class="scale-input" type="radio" name="recommend" value="1">1        </label>        <label class="scale-option">          <input class="scale-input" type="radio" name="recommend" value="2">2        </label>        <label class="scale-option">          <input class="scale-input" type="radio" name="recommend" value="3">3        </label>        <label class="scale-option">          <input class="scale-input" type="radio" name="recommend" value="4">4        </label>        <label class="scale-option">          <input class="scale-input" type="radio" name="recommend" value="5">5        </label>        <label class="scale-option">          <input class="scale-input" type="radio" name="recommend" value="6">6        </label>        <label class="scale-option">          <input class="scale-input" type="radio" name="recommend" value="7">7        </label>        <label class="scale-option">          <input class="scale-input" type="radio" name="recommend" value="8" checked>8        </label>        <label class="scale-option">          <input class="scale-input" type="radio" name="recommend" value="9">9        </label>        <label class="scale-option">          <input class="scale-input" type="radio" name="recommend" value="10">10        </label>        <p class="scale-ends"><span>Not at all likely</span><span>Extremely likely</span></p>      </div>    </fieldset>  </div></template>

#數字列與兩端標籤

各格平均分配整列寬度,間距為 4px,所以不論有五個或十一個數字,都會填滿所在欄的寬度。每格高 38px,和按鈕等高,因此能與周圍的欄位對齊。每格最窄為 28px,整列放不下時,方格會換到第二列。

數字本身無法說明程度。scale-ends 是數字列下方的一行文字,分別標示兩端的意義。請將它放在量尺內最後一格之後。題目本身可如上例放在欄位群組的標題(legend 元素)中,也可寫在具有 role="radiogroup" 的量尺的 aria-label 中。

五點量尺

<div class="scale w-full max-w(280)" role="radiogroup" aria-label="How hard was it to set up?">  <label class="scale-option">    <input class="scale-input" type="radio" name="effort" value="1">1  </label>  <label class="scale-option">    <input class="scale-input" type="radio" name="effort" value="2" checked>2  </label>  <label class="scale-option">    <input class="scale-input" type="radio" name="effort" value="3">3  </label>  <label class="scale-option">    <input class="scale-input" type="radio" name="effort" value="4">4  </label>  <label class="scale-option">    <input class="scale-input" type="radio" name="effort" value="5">5  </label>  <p class="scale-ends"><span>Very easy</span><span>Very hard</span></p></div>
<div className="scale w-full max-w(280)" role="radiogroup" aria-label="How hard was it to set up?">  <label className="scale-option">    <input className="scale-input" type="radio" name="effort" value="1" />1  </label>  <label className="scale-option">    <input className="scale-input" type="radio" name="effort" value="2" defaultChecked />2  </label>  <label className="scale-option">    <input className="scale-input" type="radio" name="effort" value="3" />3  </label>  <label className="scale-option">    <input className="scale-input" type="radio" name="effort" value="4" />4  </label>  <label className="scale-option">    <input className="scale-input" type="radio" name="effort" value="5" />5  </label>  <p className="scale-ends"><span>Very easy</span><span>Very hard</span></p></div>
<div class="scale w-full max-w(280)" role="radiogroup" aria-label="How hard was it to set up?">  <label class="scale-option">    <input class="scale-input" type="radio" name="effort" value="1">1  </label>  <label class="scale-option">    <input class="scale-input" type="radio" name="effort" value="2" checked>2  </label>  <label class="scale-option">    <input class="scale-input" type="radio" name="effort" value="3">3  </label>  <label class="scale-option">    <input class="scale-input" type="radio" name="effort" value="4">4  </label>  <label class="scale-option">    <input class="scale-input" type="radio" name="effort" value="5">5  </label>  <p class="scale-ends"><span>Very easy</span><span>Very hard</span></p></div>
<template>  <div class="scale w-full max-w(280)" role="radiogroup" aria-label="How hard was it to set up?">    <label class="scale-option">      <input class="scale-input" type="radio" name="effort" value="1">1    </label>    <label class="scale-option">      <input class="scale-input" type="radio" name="effort" value="2" checked>2    </label>    <label class="scale-option">      <input class="scale-input" type="radio" name="effort" value="3">3    </label>    <label class="scale-option">      <input class="scale-input" type="radio" name="effort" value="4">4    </label>    <label class="scale-option">      <input class="scale-input" type="radio" name="effort" value="5">5    </label>    <p class="scale-ends"><span>Very easy</span><span>Very hard</span></p>  </div></template>

#量尺、分段控制項、評分或滑桿

量尺用來表示程度,中間各級只標示數字。若要讓使用者在兩到五個有名稱的選項間切換檢視,請用分段控制項。用幾顆星表示分數時,請用星等評分。在較大的範圍內選取近似值時,請用滑桿。若要投票並顯示所有人的作答結果,請用投票調查。

#無障礙設計

  • 單選按鈕共用同一個 name。瀏覽器會將整組視為一個 Tab 停駐點,並讓使用者用方向鍵在選項間切換。
  • 每格的數字是該單選按鈕的標籤。兩端的說明未繫結到個別方格,因此請在題目中說明數字的意義,例如「0 代表完全不可能,10 代表極有可能」。
  • 焦點環會畫在方格外圍。在觸控螢幕上,方格維持原本的高度,觸控區則從方格向上下延伸,總高度達 44px;十一格約需 320px 寬才能維持好按的尺寸。
  • 量尺停用後,已選取的方格會以較低調的色調顯示,讓使用者仍能看出先前的答案。

#類別

類別說明
scale該列使用 fieldset 或設有 role="radiogroup" 的元素,各儲存格平分寬度,彼此相隔 4px
scale-option每個數字使用一個 label,預設外觀平坦,高 38px,最小寬度為 28px。單列放不下所有儲存格時會換行。按下時呈凹陷樣式,選取後以前景色填滿。
scale-input儲存格內的原生單選按鈕會覆蓋整個儲存格,外觀隱藏。
scale-ends列下方的一行文字,兩端各有一個名稱

#自訂屬性

屬性預設值說明
--scale-gap.25rem儲存格之間的間距

#原始碼

@master/ui/components/scale.css
/* * Scale: one answer on a row of numbers, from one end to the other. * How likely, how satisfied, how much: each number is a <label> around a native radio, so the group keeps the * arrow keys and the form keeps the value. The cells share the row's width. At rest a cell is a flat face on * one contour; the chosen one is a mark of ink, as a chosen day of a calendar is. * scale-ends is a line under the row that names its two ends, since a number alone says nothing. * Geometry: cells are 38px tall, as a button is, 4px apart, with a small control's corner. On a touch screen a * cell's target reaches 44px above and below it. */@layer components.ui {  .scale {    --scale-gap: .25rem;    display: flex;    flex-wrap: wrap;    gap: var(--scale-gap);    min-width: 0;    margin: 0;    padding: 0;    border: 0;  }  /* One number. */  .scale-option {    position: relative;    display: grid;    flex: 1 1 0;    place-items: center;    /* A cell is never narrower than 28px: in a row too narrow for them all, the cells wrap. */    min-width: 1.75rem;    height: 2.375rem;    border-radius: calc(var(--radius-control-sm) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--color-surface-raised);    color: var(--color-text-body);    box-shadow: var(--shadow-flat);    font: var(--font-weight-control) .84375rem/1 var(--font-family-sans);    font-feature-settings: inherit;    font-variant-numeric: tabular-nums;    cursor: pointer;    touch-action: manipulation;    user-select: none;    -webkit-user-select: none;    -webkit-tap-highlight-color: transparent;    transition: box-shadow var(--duration-quick) var(--easing-glide);    @media (hover: hover) {      &:hover:not(:has(:checked, :disabled)) {        color: var(--color-text-strong);        box-shadow: 0 0 0 .5px var(--color-line-mark);      }    }    /* Pressed: the cell sinks into a well before it turns to ink. */    &:active:not(:has(:checked, :disabled)) {      background: var(--color-surface-inset);      box-shadow: var(--shadow-pressed);      transition-duration: var(--duration-press);    }    /* Chosen: ink. The face swaps at once; only its shadow eases. */    &:has(.scale-input:checked) {      background: var(--color-surface-inverse);      color: var(--color-text-inverse);      box-shadow: var(--shadow-ink-mark);    }    &:has(.scale-input:focus-visible) {      @apply --focus-ring;    }    &:has(.scale-input:disabled) {      background: var(--color-surface-inset);      color: var(--color-text-disabled);      cursor: not-allowed;    }    &:has(.scale-input:checked:disabled) {      background: var(--color-text-disabled);      color: var(--color-surface-raised);      box-shadow: none;    }    /* Cells stand 4px apart, so on touch a target grows only above and below. */    @media (pointer: coarse) {      &::after {        @apply --touch-target-block;      }    }    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;      /* Kept out of forced colors, the chosen cell drops the theme's ink edge and shade along with its face. */      &:has(.scale-input:checked) {        background: Highlight;        color: HighlightText;        box-shadow: none;        forced-color-adjust: none;      }      /* A cell that cannot be chosen, chosen or not, is in the system's gray. */      &:has(.scale-input:disabled) {        outline-color: GrayText;        color: GrayText;      }      &:has(.scale-input:checked:disabled) {        background: GrayText;        color: Canvas;      }      /* The chosen cell is kept out of forced colors and is where the keys stand: its ring is given the system's color. */      &:has(.scale-input:focus-visible) {        outline: 2px solid Highlight;        outline-offset: 2px;      }    }  }  /* The native radio covers its cell and is not drawn: the cell is its face. */  .scale-input {    position: absolute;    inset: 0;    width: 100%;    height: 100%;    margin: 0;    padding: 0;    border: 0;    opacity: 0;    appearance: none;    cursor: inherit;  }  /* What the two ends mean: a line under the row, one name at each end. */  .scale-ends {    display: flex;    flex: none;    justify-content: space-between;    gap: 1rem;    order: 1;    width: 100%;    margin: .125rem 0 0;    color: var(--color-text-muted);    font-size: .78125rem;    line-height: 1.25rem;  }}