Published on

Practical Guide: Create A Toggle switch without JS With Tailwind CSS

Toggle switch without JS

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

The description of Toggle switch without JS ui component

Toggle switch using only css

Why use Tailwind CSS to build a Toggle switch without JS ui component?

  • It can make the building process of Toggle switch without JS ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Toggle switch without JS component file.

The preview of Toggle switch without JS ui component

Free download of the Toggle switch without JS's source code

The source code of Toggle switch without JS ui component

<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>

How to build a Toggle switch without JS with Tailwind CSS?

Install tailwind css of verion 1.3.4

Use the link html tag to import the stylesheet of Tailwind CSS of the version 1.3.4

<link href=https://unpkg.com/[email protected]/dist/tailwind.min.css rel="stylesheet">

All the unility class needed to build a Toggle switch without JS component

  • relative
  • inline-block
  • w-10
  • mr-2
  • absolute
  • block
  • w-6
  • h-6
  • bg-white
  • border-4
  • overflow-hidden
  • bg-gray-300
  • text-xs
  • text-gray-700

14 steps to build a Toggle switch without JS component with Tailwind CSS

  1. Use relative to position an element according to the normal flow of the document.

  2. Use inline-block utilities to wrap the element to prevent the text inside from extending beyond its parent.

  3. Use w-10 to set an element to a fixed width(2.5rem).

  4. Control the margin on right side of an element to 0.5rem using the mr-2 utilities.

  5. Use absolute to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.

  6. Use inline utilities to put the element on its own line and fill its parent.

  7. Use w-6 to set an element to a fixed width(1.5rem).

  8. Use h-6 to set an element to a fixed height(1.5rem).

  9. Control the background color of an element to white using the bg-white utilities.

  10. Control the border color of an element to 1rem using the border-4 utilities.

  11. Use overflow-hidden to clip any content within an element that overflows the bounds of that element.

  12. Control the background color of an element to gray-300 using the bg-gray-300 utilities.

  13. Control the text color of an element to xs using the text-xs utilities.

  14. Control the text color of an element to gray-700 using the text-gray-700 utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to build a Toggle switch without JS components, learn and follow along to implement your own components.