탐색

v2.1.0
폼

체크박스

선택하면 잉크 색으로 채워지는 작은 오목한 영역 형태의 네이티브 입력 요소입니다.

이 페이지의 내용

체크박스는 선택하면 잉크 색으로 채워지는 작은 오목한 영역입니다. 네이티브 <input type="checkbox">에 .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>

옵션마다 별도의 제목과 설명이 필요하면 선택 옵션을 사용하세요.

#부분 선택 상태

하위 항목 중 일부만 선택하면 상위 체크박스에 가로줄이 표시됩니다. 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>

#모양

체크박스는 칩과 같은 모서리를 가진 정사각형으로, 여러 항목을 선택할 수 있음을 나타냅니다. 브라우저가 연속 곡선 모서리를 지원하면 정사각형이 앱 아이콘과 같은 모양으로 표시됩니다. 체크 표시는 짧은 전환을 거쳐 제자리에 놓이고 해제하면 즉시 사라집니다. 동작 줄이기가 활성화되어 있으면 투명도만 바뀝니다.

#접근성

  • 네이티브 입력 요소이므로 스페이스바로 선택 상태를 전환할 수 있으며, 체크박스가 선택되어 있으면 폼을 제출할 때 그 값도 함께 전송됩니다.
  • 표의 행에 있는 체크박스처럼 화면에 표시되는 텍스트가 없다면 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;  }}