导航

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>

#分组

将相关的复选框放在带有组标题(legend 元素)的 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>
<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" input,呈方形凹槽,采用与胶囊标签相同的圆角。

#源码

@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;  }}