Categories
CSS HTML Web development

CSS Toggle Switch

CSS Toggle Switch

To learn how to create a “toggle switch” (on/off button) with CSS follow the steps below:

Demo:


Step1.

Add HTML

<label class="toggle-switch">
  <input type="checkbox" checked>
  <span class="slider round"></span>
</label>

Step2.

Add CSS

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 200px;
  height: 110px;
}

.toggle-switch input { 
  display: none;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #264653;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 90px;
  width: 90px;
  left: 12px;
  bottom: 10px;
  background-color: #e9c46a;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2a9d8f;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2a9d8f;
}

input:checked + .slider:before {
  transform: translateX(87px);
}

.slider.round {
  border-radius: 100px;
}

.slider.round:before {
  border-radius: 50%;
}

Enjoy coding!

Read also:

CSS Desk Lamp (Switch On/ Off)

CSS Umbrella (Open/Close on Click)

Pure CSS Envelope (Open/Close on click)