網站導覽

v2.1.0
表單

核取方塊

勾選後會填上顏色的原生小方框。

本頁內容

核取方塊是個勾選後會填上顏色的小方框。將 .checkbox 加到原生 <input type="checkbox"> 上,再用 <label class="check"> 把它和文字包在一起,讓整列都可以點按。每個核取方塊都能獨立勾選。若只能從多個選項中選一個,請用單選按鈕。

核取方塊

<label class="check">  <input type="checkbox" class="checkbox" checked> Email me about releases</label><label class="check"><input type="checkbox" class="checkbox"> Weekly digest</label>
<>  <label className="check">    <input type="checkbox" className="checkbox" defaultChecked /> Email me about releases  </label>  <label className="check"><input type="checkbox" className="checkbox" /> Weekly digest</label></>
<label class="check">  <input type="checkbox" class="checkbox" checked> Email me about releases</label><label class="check"><input type="checkbox" class="checkbox"> Weekly digest</label>
<template>  <label class="check">    <input type="checkbox" class="checkbox" checked> Email me about releases  </label>  <label class="check"><input type="checkbox" class="checkbox"> Weekly digest</label></template>

#群組

將相關的核取方塊放在同一個 fieldset 中,並用 legend 元素為群組命名。群組中可勾選任意數量的項目。

群組

<fieldset class="fieldset">  <legend class="fieldset-legend">Notify me about</legend>  <label class="check">    <input type="checkbox" class="checkbox" name="notify" value="failed" checked> Failed deployments  </label>  <label class="check">    <input type="checkbox" class="checkbox" name="notify" value="certificates" checked> Expiring certificates  </label>  <label class="check">    <input type="checkbox" class="checkbox" name="notify" value="news"> Product news  </label></fieldset>
<fieldset className="fieldset">  <legend className="fieldset-legend">Notify me about</legend>  <label className="check">    <input type="checkbox" className="checkbox" name="notify" value="failed" defaultChecked /> Failed deployments  </label>  <label className="check">    <input type="checkbox" className="checkbox" name="notify" value="certificates" defaultChecked /> Expiring certificates  </label>  <label className="check">    <input type="checkbox" className="checkbox" name="notify" value="news" /> Product news  </label></fieldset>
<fieldset class="fieldset">  <legend class="fieldset-legend">Notify me about</legend>  <label class="check">    <input type="checkbox" class="checkbox" name="notify" value="failed" checked> Failed deployments  </label>  <label class="check">    <input type="checkbox" class="checkbox" name="notify" value="certificates" checked> Expiring certificates  </label>  <label class="check">    <input type="checkbox" class="checkbox" name="notify" value="news"> Product news  </label></fieldset>
<template>  <fieldset class="fieldset">    <legend class="fieldset-legend">Notify me about</legend>    <label class="check">      <input type="checkbox" class="checkbox" name="notify" value="failed" checked> Failed deployments    </label>    <label class="check">      <input type="checkbox" class="checkbox" name="notify" value="certificates" checked> Expiring certificates    </label>    <label class="check">      <input type="checkbox" class="checkbox" name="notify" value="news"> Product news    </label>  </fieldset></template>

#長標籤

方框會與標籤的第一行垂直置中。即使標籤換行,方框也會維持在第一行的位置,因此後面可以接一整句話。

長標籤

<label class="check max-w(320)">  <input type="checkbox" class="checkbox" required> I have read the terms of service and understand that deployments on the free plan pause after 30 days without traffic.</label>
<label className="check max-w(320)">  <input type="checkbox" className="checkbox" required /> I have read the terms of service and understand that deployments on the free plan pause after 30 days without traffic.</label>
<label class="check max-w(320)">  <input type="checkbox" class="checkbox" required> I have read the terms of service and understand that deployments on the free plan pause after 30 days without traffic.</label>
<template>  <label class="check max-w(320)">    <input type="checkbox" class="checkbox" required> I have read the terms of service and understand that deployments on the free plan pause after 30 days without traffic.  </label></template>

若選項需要自己的標題和說明,請用選項。

#部分勾選

下層項目只有一部分勾選時,上層核取方塊會顯示一條短橫線。請透過腳本設定 indeterminate,HTML 沒有對應的屬性。

部分勾選

<label class="check"><input type="checkbox" class="checkbox" id="select-all"> All regions</label><label class="check ml-lg"><input type="checkbox" class="checkbox" checked> Tokyo</label><label class="check ml-lg"><input type="checkbox" class="checkbox"> Frankfurt</label><script>document.getElementById('select-all').indeterminate = true</script>

#停用

停用時,核取方塊會淡化,標籤也會改用停用狀態的文字色。

停用

<label class="check">  <input type="checkbox" class="checkbox" checked disabled> Required by your plan</label><label class="check">  <input type="checkbox" class="checkbox" disabled> Not available in your region</label>
<>  <label className="check">    <input type="checkbox" className="checkbox" defaultChecked disabled /> Required by your plan  </label>  <label className="check">    <input type="checkbox" className="checkbox" disabled /> Not available in your region  </label></>
<label class="check">  <input type="checkbox" class="checkbox" checked disabled> Required by your plan</label><label class="check">  <input type="checkbox" class="checkbox" disabled> Not available in your region</label>
<template>  <label class="check">    <input type="checkbox" class="checkbox" checked disabled> Required by your plan  </label>  <label class="check">    <input type="checkbox" class="checkbox" disabled> Not available in your region  </label></template>

#形狀

核取方塊是採用膠囊標籤圓角的正方形,表示這些項目可任意勾選。瀏覽器支援連續圓角時,方框會呈現類似應用程式圖示的輪廓。勾號出現時會短暫過渡到定位,取消勾選時則立即消失。啟用減少動態效果偏好時,勾號只會以透明度變化呈現。

#無障礙

  • 使用原生輸入元件,因此可用 空白鍵 切換勾選狀態,也能隨表單提交。
  • 沒有可見文字的核取方塊,例如表格列中的核取方塊,需要加上 aria-label。
  • 若設定會立即生效,無須按下儲存按鈕,請用開關。

#類別

類別說明
check將核取方塊、選項按鈕或開關與對應文字放在同一標籤列。控制項停用時,標籤列也會套用停用狀態的文字色
checkbox原生 type="checkbox" 輸入控制項,外觀為方形凹槽,使用小型元件的圓角 token。

#原始碼

@master/ui/components/checkbox.css
/* * Checkbox: a native input drawn as a small well that fills with ink when checked. * Its corner is a chip's, so with continuous corners it takes the shape of an app icon. * Wrap it with its text in a .check label to make the whole row a target; the same label * serves a radio and a switch. */@layer components.ui {  .check {    display: inline-flex;    align-items: flex-start;    gap: .5rem;    color: var(--color-text-strong);    font-size: .875rem;    line-height: 1.45;    /* A long word (a path, an address) breaks instead of pushing the label past its container, as a choice's title does. */    overflow-wrap: anywhere;    /* A sentence that wraps does not leave one word alone on its last line. */    text-wrap: pretty;    cursor: pointer;    touch-action: manipulation;    -webkit-tap-highlight-color: transparent;    &:has(.checkbox:disabled, .radio:disabled, .switch:disabled) {      color: var(--color-text-disabled);      cursor: not-allowed;    }    /*     * On touch the label is the target: an empty box reaches 44px above and below its line without moving what stands     * near it, so padding a page gives the row stays its own. It lies under the label's words, so a link among them     * (the terms of a sign-up) keeps its own taps.     */    @media (pointer: coarse) {      position: relative;      isolation: isolate;      &::after {        @apply --touch-target-block;        z-index: -1;      }    }  }  .checkbox {    --check-size: 1.125rem;    position: relative;    display: inline-grid;    flex: none;    place-items: center;    width: var(--check-size);    height: var(--check-size);    /* Centres the control on the first line of a label that wraps. */    margin: max(0px, calc((1lh - var(--check-size)) / 2)) 0;    padding: 0;    border: 0;    border-radius: calc((var(--radius-chip) - 1px) * var(--corner-scale));    corner-shape: var(--corner-shape);    /* A small control needs a firmer edge than a surface to be found: one step, still half a pixel. */    @apply --check-well;    vertical-align: middle;    appearance: none;    /* Standing alone, as a switch does: a pointer, no double-tap zoom and no grey flash around the well. */    cursor: pointer;    touch-action: manipulation;    -webkit-tap-highlight-color: transparent;    transition-property: background-color, box-shadow;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);    /* The mark is five ninths of the well (10px of 18), so it keeps its proportion where the well grows with the text. */    &::after {      content: '';      width: calc(var(--check-size) * 5 / 9);      height: calc(var(--check-size) * 5 / 9);      opacity: 0;      scale: .6;      background: var(--color-text-inverse);      /* The mark leaves at once and arrives with a short settle. */      transition: opacity var(--duration-press) linear, scale var(--duration-press) linear;    }    /* Forced colors draw the well with system colors; the hover's tokens, which are more specific, would show through. */    @media (hover: hover) and (forced-colors: none) {      &:hover:not(:checked, :indeterminate, :disabled) {        @apply --check-well-hover;      }    }    &:is(:checked, :indeterminate) {      @apply --check-ink;      &::after {        opacity: 1;        scale: 1;        transition: opacity var(--duration-quick) var(--easing-glide), scale var(--duration-base) var(--easing-glide);      }    }    &:focus-visible {      @apply --focus-ring;    }    /*     * Invalid: a required box left empty once the reader has tried to send the form, or aria-invalid. The well takes     * the danger edge a field takes, under the pointer too; the words that say why are the field's error beside it.     */    &:is([aria-invalid="true"], :user-invalid):not(:checked, :indeterminate, :disabled) {      box-shadow: var(--shadow-pressed), inset 0 0 0 .5px var(--color-danger), 0 0 0 .5px color-mix(in oklab, var(--color-danger) 45%, transparent);    }    &:disabled {      opacity: .5;      cursor: not-allowed;    }    @media (prefers-reduced-motion: reduce) {      &::after {        scale: 1;      }    }    /* Draw with system colors so the mark survives forced colors. */    @media (forced-colors: active) {      forced-color-adjust: none;      background: Field;      box-shadow: inset 0 0 0 1px FieldText;      &::after {        background: HighlightText;      }      &:is(:checked, :indeterminate) {        background: Highlight;        box-shadow: none;      }      &:disabled {        opacity: 1;        box-shadow: inset 0 0 0 1px GrayText;      }      /* The danger edge is a theme color: the system's mark color says it here, as a field's outline does. */      &:is([aria-invalid="true"], :user-invalid):not(:checked, :indeterminate, :disabled) {        box-shadow: inset 0 0 0 2px Mark;      }      /* Checked and disabled: the fill takes the disabled system color, so it is not read as one that can change. */      &:disabled:is(:checked, :indeterminate) {        background: GrayText;      }      &:focus-visible {        outline-color: Highlight;      }    }  }  .checkbox::after {    @apply --check-mark;  }  .checkbox:indeterminate::after {    @apply --check-dash;  }}