Tailwind switch

Toggle Switch button built with the latest Tailwind. Learn how to use on / off input and disabled state.


Basic (not clickable)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<div class="flex items-center justify-center ">
  <input type="checkbox" name="toggle" class="hidden" />
  <label
    class="relative w-12 h-6 flex select-none cursor-pointer"
    for="toggle"
  >
    <span
      class="absolute left-0 top-0 h-full w-full bg-gray-100 rounded-full bg-gray-100"
    ></span>
    <span
      class="h-6 w-6 border-2 absolute z-10 rounded-full bg-white transition-transform duration-300 ease-in-out flex justify-center items-center border-gray-100"
    ></span>
  </label>
</div>

With icons (not clickable)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<div class="w-full h-full flex flex-col justify-center items-center">
  <div class="flex justify-center items-center">
    <span class="">
      <i class="far fa-sun text-gray-400"></i>
    </span>
    <!-- Switch Container -->
    <div
      :class="{ 'bg-cyan-700': toggleActive}"
      class="w-14 h-7 flex items-center bg-gray-300 rounded-full mx-3 px-1"
      @click="handleToggleActive"
    >
      <!-- Switch -->
      <div
        class="bg-white w-5 h-5 rounded-full shadow-md transform"
        :class="{ 'translate-x-7': toggleActive}"
      ></div>
    </div>
    <span class="">
      <i class="far fa-moon text-gray-400"></i>
    </span>
  </div>
  <!-- Toggled switch -->
  <div class="flex justify-center items-center mt-4">
    <span class="">
      <!-- <svg class="h-6 w-6 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z" />
    </svg> -->
      <i class="far fa-sun"></i>
    </span>
    <!-- Switch Container -->
    <div
      class="w-14 h-7 flex items-center bg-gray-300 rounded-full mx-3 px-1 bg-blue-700"
      @click="handleToggleActive"
    >
      <!-- Switch -->
      <div
        class="bg-white w-5 h-5 rounded-full shadow-md transform translate-x-7"
      ></div>
    </div>
    <span class="">
      <i class="far fa-moon"></i>
    </span>
  </div>
</div>

Custom CSS

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<style>
  /* CHECKBOX TOGGLE SWITCH */
  /* @apply rules for documentation, these do not work as inline style */
  .toggle-checkbox:checked {
    @apply: right-0 border-green-400;
    right: 0;
    border-color: #68d391;
  }
  .toggle-checkbox:checked + .toggle-label {
    @apply: bg-green-400;
    background-color: #68d391;
  }
</style>

<div
  class="relative inline-block w-10 mr-2 align-middle select-none transition duration-200 ease-in"
>
  <input
    type="checkbox"
    name="toggle"
    id="toggle"
    class="toggle-checkbox absolute block w-6 h-6 rounded-full bg-white border-4 appearance-none cursor-pointer"
  />
  <label
    for="toggle"
    class="toggle-label block overflow-hidden h-6 rounded-full bg-gray-300 cursor-pointer"
  ></label>
</div>
<label for="toggle" class="text-xs text-gray-700">Toggle me.</label>

Simple card