JSFiddle - React, Tailwind, and code Playground

by jpeter06

HTML

<label class="toggle">
  <input type="checkbox" class="toggle-checkbox">
  <div class="toggle-switch"></div>
  <span class="toggle-label">Your Label Text</span>
</label>

CSS

.toggle {
  /* Style the label element */
  display: inline-block;
  /* Adjust width if needed */
  width: 60px;
  height: 30px;
  background-color: #ccc;
  border-radius: 25px;
  cursor: pointer; /* Indicate clickable element */
  position: relative; /* Needed for pseudo-elements */
}

.toggle-checkbox {
  /* Hide the actual checkbox */
  opacity: 0;
  height: 0;
  width: 0;
}

.toggle-switch {
  /* Style the toggle button element */
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #fff;
  border-radius: 25px;
  transition: all 0.3s ease-in-out; /* Add smooth transition */
}

.toggle-label {
  /* Style the label text */
  text-align: center;
  line-height: 30px; /* Match toggle height */
}

.toggle-checkbox:checked + .toggle-switch {
  /* Style the toggle button when checked */
  background-color: #4CAF50; /* Change to your desired on color */
}

.toggle-checkbox:checked + .toggle-label {
  /* Optionally move label text when checked */
  /* Adjust position based on your needs */
  margin-left: 15px;
}