Tailwind Switch
Tags: tailwindcss, webdev
Published: 2021-09-30 12:41:17

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](http://tailwind-elements.com/tw-starter.zip)

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

{% youtube RAhugF8NOBs %}
_______

## Customization

##### Basic (not clickable)



###### HTML
```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>
```

##### With icons (not clickable)

###### HTML
```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>
```





#### You can see more customization examples on the [📄 Switch documentation page](https://tailwind-elements.com/docs/standard/forms/switch/)

______

## Crucial Resources

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

1. Read [📄 Switch documentation page](https://tailwind-elements.com/docs/standard/forms/switch/) <-- 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)](https://mdbootstrap.com/docs/standard/cli/)

-----

## Related Forms options & features
- [Checkbox](https://tailwind-elements.com/docs/standard/components/checkbox/)
- [Datepicker](https://tailwind-elements.com/docs/standard/forms/datepicker/)
- [File input](https://tailwind-elements.com/docs/standard/components/file-input/)
- [Forms Templates](https://tailwind-elements.com/docs/standard/components/form-templates/)
- [Inputs](https://tailwind-elements.com/docs/standard/forms/inputs/)
- [Multiselect](https://tailwind-elements.com/docs/standard/components/multiselect/)
- [Radios](https://tailwind-elements.com/docs/standard/components/radio/)
- [Range](https://tailwind-elements.com/docs/standard/forms/range/)
- [Searchs](https://tailwind-elements.com/docs/standard/components/search/)
- [Select](https://tailwind-elements.com/docs/standard/components/select/)
- [Textarea](https://tailwind-elements.com/docs/standard/components/textarea/)
- [Timepicker](https://tailwind-elements.com/docs/standard/forms/timepicker/)



-----

## Additional resources

Learn web development with our **learning roadmap**:
**[:mortar_board: Start Learning](https://mdbootstrap.com/docs/standard/getting-started/)**

Join our mailing list & receive **exclusive resources** for developers
**[:gift: Get gifts](https://mdbootstrap.com/newsletter/)**

Join our private FB group for **inspiration & community experience**
**[👨‍👩‍👧‍👦 Ask to join](https://www.facebook.com/groups/682245759188413)**

**Support creation of open-source packages** with a STAR on GitHub
[![GitHub stars](https://img.shields.io/github/stars/dawidadach/Tailwind-Elements.svg?style=social&label=Star&maxAge=2592000)](https://github.com/dawidadach/Tailwind-Elements/)