<div class="toggle">
  <input type="checkbox">
  <span class="rail"></span>
</div>
.toggle {
  position: relative;
  width: 30vw;
  height: 15vw;
  
  .rail {
    display: block;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,.2);
    border-radius: 1000px;
    transition: background ease-in-out 0.1s;
    
    &:after {
      content: '';
      aspect-ratio: 1;
      height: calc(100% - 2vw);
      inset: 1vw;
      position: absolute;
      background: white;
      border-radius: 1000px;
      transition: transform ease-in-out 0.1s;
    }
  }
  
  input {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
    z-index: 1;
    opacity: 0.00001;
    margin: 0;
    
    &:checked + .rail {
       background: green;
      
      &:after {
        transform: translateX(calc(100% + 2vw));
      }
    }
    
    &:focus-visible + .rail {
      box-shadow: 0 0 0 1vw white, 0 0 0 2vw blue; 
    }
  }
}


// Just for decoration...

body {
  display: grid;
  height: 100vh;
  place-content: center;
}
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.