Toggle Button using Html and Css only

 How to make Toggle button using html and css ?


Hello! Let's learn how we can Toggle button using Html and Css only.

HTML Code

<div class="wrapper">
  <input type="checkbox" id="checkbox">
  <label for="checkbox" class="toggle"></label>
</div>

CSS Code

*,
::before {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  height: 100vh;
  display: flex;
}

.wrapper {
  position: relative;
  margin: auto;
}

.toggle {
  position: relative;
  width: 100px;
  height: 50px;
  display: block;
  background: #ccc;
  border-radius: 30px;
  transition: .25s ease;
}

.toggle::before {
  position: absolute;
  content: '';
  top: 50%;
  left: 5px;
  transform: translateY(-50%);
  width: 42px;
  height: 42px;
  background: #fff;
  border-radius: 50%;
  transition: all .25s ease;
}

#checkbox {
  position: absolute;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

#checkbox:checked + label {
  background: #007bff;
}

#checkbox:checked + label::before {
  left: 54px;
}

Output


That is all. Thank you for visiting.


Post a Comment (0)
Previous Post Next Post