ナビゲーション

v2.1.0
フォーム

チェックボックス

チェックを入れるとインク色で塗りつぶされる、小さなくぼみのあるネイティブコントロール。

このページの内容

チェックボックスは小さなくぼみのあるコントロールで、チェックを入れるとインク色で塗りつぶされます。ネイティブの<input type="checkbox">に.checkboxを指定し、テキストと一緒に<label class="check">で囲むと、行全体を操作できます。各チェックボックスは個別にオン・オフを切り替えられます。複数の選択肢から1つを選ぶ場合はラジオボタンを使います。

チェックボックス

<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>

#長いラベル

チェックボックスはラベルの1行目の中央にそろい、折り返しても位置が変わりません。ラベルには文章も使えます。

長いラベル

<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>

選択肢ごとにタイトルと説明が必要な場合は、選択肢を使います。

#一部選択

子項目の一部だけが選択されている場合、親のチェックボックスには横線が表示されます。HTMLに対応する属性がないため、スクリプトからindeterminateを設定します。

一部選択

<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>

#形状

チェックボックスは、チップと同じ丸みを持つ正方形で、複数の選択肢を選べることを示します。連続曲線の角に対応するブラウザーでは、アプリアイコンのような形になります。チェックマークは短いアニメーションで所定の位置に収まり、チェックを外すとすぐに消えます。動きを抑える設定では、フェードだけで切り替わります。

#アクセシビリティ

  • ネイティブの入力要素なので、Spaceキーで切り替えられ、フォームから送信できます。
  • 表の行に置く場合など、表示テキストがないチェックボックスにはaria-labelが必要です。
  • 保存ボタンを押さずに変更が即座に反映される設定には、スイッチを使います。

#クラス

クラス説明
checkチェックボックス、ラジオボタン、スイッチのいずれかと、そのテキストを囲むラベルです。コントロールが無効な間は、行全体に無効状態の文字色を使います。
checkboxネイティブの type="checkbox" 入力要素です。正方形の入力欄で、角はチップと同じ丸みです。

#ソース

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