본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Toggle

원문 보기 위키 갱신

Toggle (토글 스위치)

Toggle은 스위치 버튼처럼 보이게 스타일된 체크박스예요.

출처: 문서

본문

<input type="checkbox">에 toggle 클래스를 붙이면 스위치 모양이 됩니다. 색상과 크기를 바꿀 수 있고, 안에 아이콘을 넣을 수도 있어요.

클래스명 타입 설명
toggle Component <input type="checkbox">에 사용
toggle-primary Color primary 컬러
toggle-secondary Color secondary 컬러
toggle-accent Color accent 컬러
toggle-neutral Color neutral 컬러
toggle-success Color success 컬러
toggle-warning Color warning 컬러
toggle-info Color info 컬러
toggle-error Color error 컬러
toggle-xs Size 아주 작은 크기
toggle-sm Size 작은 크기
toggle-md Size 중간 크기 [기본값]
toggle-lg Size 큰 크기
toggle-xl Size 아주 큰 크기

Toggle (스위치)

<input type="checkbox" checked="checked" class="toggle" />

With fieldset and label (fieldset과 label 사용)

<fieldset class="fieldset bg-base-100 border-base-300 rounded-box w-64 border p-4">
  <legend class="fieldset-legend">Login options</legend>
  <label class="label">
    <input type="checkbox" checked="checked" class="toggle" />
    Remember me
  </label>
</fieldset>

Sizes (크기)

<input type="checkbox" checked="checked" class="toggle toggle-xs" />
<input type="checkbox" checked="checked" class="toggle toggle-sm" />
<input type="checkbox" checked="checked" class="toggle toggle-md" />
<input type="checkbox" checked="checked" class="toggle toggle-lg" />
<input type="checkbox" checked="checked" class="toggle toggle-xl" />

Colors (컬러)

<input type="checkbox" checked="checked" class="toggle toggle-primary" />
<input type="checkbox" checked="checked" class="toggle toggle-secondary" />
<input type="checkbox" checked="checked" class="toggle toggle-accent" />
<input type="checkbox" checked="checked" class="toggle toggle-neutral" />

<input type="checkbox" checked="checked" class="toggle toggle-info" />
<input type="checkbox" checked="checked" class="toggle toggle-success" />
<input type="checkbox" checked="checked" class="toggle toggle-warning" />
<input type="checkbox" checked="checked" class="toggle toggle-error" />

Disabled (비활성)

<input type="checkbox" class="toggle" disabled />
<input type="checkbox" class="toggle" disabled checked="checked" />

Indeterminate (중간 상태)

체크박스는 HTML 입력이므로 JS로 indeterminate 상태를 만들 수 있어요.

<!-- You can make a checkbox indeterminate using JS -->
<script>
  document.getElementById("my-toggle").indeterminate = true
</script>
<input type="checkbox" class="toggle" id="my-toggle" />

Toggle with icons inside (안에 아이콘이 있는 토글)

label에 toggle 클래스를 붙이고, 그 안에 체크박스와 아이콘 두 개를 넣으면 됩니다.

<label class="toggle text-base-content">
  <input type="checkbox" />
  <svg aria-label="enabled" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
    <g
      stroke-linejoin="round"
      stroke-linecap="round"
      stroke-width="4"
      fill="none"
      stroke="currentColor"
    >
      <path d="M20 6 9 17l-5-5"></path>
    </g>
  </svg>
  <svg
    aria-label="disabled"
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    stroke-width="4"
    stroke-linecap="round"
    stroke-linejoin="round"
  >
    <path d="M18 6 6 18" />
    <path d="m6 6 12 12" />
  </svg>
</label>

Toggle with custom colors (커스텀 컬러 토글)

<input
  type="checkbox"
  checked="checked"
  class="toggle border-indigo-600 bg-indigo-500 checked:border-orange-500 checked:bg-orange-400 checked:text-orange-800"
/>

더 알아보기 (Learn more)

Toggle은 온·오프 상태를 직관적으로 보여줘야 할 때 잘 어울려요. toggle-* 컬러·크기 클래스를 조합해 쓰고, 필요하면 Tailwind 유틸리티로 테두리와 배경을 직접 바꿀 수 있습니다. indeterminate처럼 중간 상태가 필요하면 JS로 제어하고, 아이콘을 넣고 싶다면 label을 래퍼로 쓰면 됩니다.