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

- What is Tailwind CSS?
- The description of Toggle switch without JS ui component
- Why use Tailwind CSS to build a Toggle switch without JS ui component?
- The preview of Toggle switch without JS ui component
- The source code of Toggle switch without JS ui component
- How to build a Toggle switch without JS with Tailwind CSS?
- Install tailwind css of verion 1.3.4
- All the unility class needed to build a Toggle switch without JS component
- 14 steps to build a Toggle switch without JS component with Tailwind CSS
- Conclusion
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
Use
relative
to position an element according to the normal flow of the document.Use
inline-block
utilities to wrap the element to prevent the text inside from extending beyond its parent.Use
w-10
to set an element to a fixed width(2.5rem).Control the margin on right side of an element to 0.5rem using the
mr-2
utilities.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.Use
inline
utilities to put the element on its own line and fill its parent.Use
w-6
to set an element to a fixed width(1.5rem).Use
h-6
to set an element to a fixed height(1.5rem).Control the background color of an element to white using the
bg-white
utilities.Control the border color of an element to 1rem using the
border-4
utilities.Use
overflow-hidden
to clip any content within an element that overflows the bounds of that element.Control the background color of an element to gray-300 using the
bg-gray-300
utilities.Control the text color of an element to xs using the
text-xs
utilities.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.