Toggle switch using only CSS
December 30, 2017 | 758 Views

Toggle switch using only CSS

Learn how to create ON & OFF switch or toggle switch using pure css

1.Creating The Structure

Follow the below code to achive the toggle switch structure

<label class="switch">
   <input type="checkbox">
   <div class="slider">
   </div>
  </label>

2.Add Styling

The following are the CSS to perform the styles for the toggle switch.

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
  border-radius:100%;
}
.switch input {
  display:none;
}
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
  border-radius:20px;
}
.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: #fff;
  -webkit-transition: .4s;
  transition: .4s;
  border-radius:20px;
}
input:checked + .slider {
  background-color: #66BB6A;
}
input:focus + .slider {
  box-shadow: 0 0 1px #66BB6A;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}
  
  

Live demo

See the Pen Pure CSS Toggle ON and OFF Switch by themeswild (@themeswild) on CodePen.





Free html templates

Download from a vast library of Open Source HTML & CSS templates and use them for free. No licenses required. Apart from that, do you need to build a whole website from scratch? Write to us anytime and we will get it sorted.

Contact Us

More Links