DEV Community

Cover image for Tailwind Switch
Keep Coding
Keep Coding

Posted on

Tailwind Switch

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


Installation

Quick Start

In order to start using Tailwind simply download our starter.

DOWNLOAD ZIP STARTER

Tailwind Elements does not change or add any CSS to the already one from TailwindCSS.

You can directly copy our components into your Tailwind design and they will work straight away.

In some dynamic components (like dropdowns or modals) we add Font Awesome icons and custom JavaScript. However, they do not require any additional installation, all the necessary code is always included in the example and copied to any Tailwind project - it will work.


MDB GO


Customization

Basic (not clickable)
HTML
<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>
Enter fullscreen mode Exit fullscreen mode
With icons (not clickable)
HTML
<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>
Enter fullscreen mode Exit fullscreen mode

You can see more customization examples on the ๐Ÿ“„ Switch documentation page


Crucial Resources

Here are the resources that we have prepared to help you work with this component:

  1. Read ๐Ÿ“„ Switch documentation page <-- start here
  2. In to get the most out of your project, you should also get acquainted with other Forms options related to Switch. See the section below to find the list of them.
  3. After finishing the project you can publish it with CLI in order to receive ๐Ÿ’ฝ Free hosting (beta)

Related Forms options & features


Additional resources

Learn web development with our learning roadmap:
๐ŸŽ“ Start Learning

Join our mailing list & receive exclusive resources for developers
๐ŸŽ Get gifts

Join our private FB group for inspiration & community experience
๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Ask to join

Support creation of open-source packages with a STAR on GitHub
GitHub stars

Top comments (0)