HTML and CSS

Checkboxes as switch buttons

What does this CSS snippet do?

This CSS snippet creates a custom toggle switch using a hidden checkbox and a styled label

How does it work:

  • Checkbox is hidden but still functional.
  • Label acts as the switch track, styled with rounded edges and a default gray background.
  • When checked, the label turns green, and:
    • The knob (::after) slides to the right.
    • A checkmark icon (::before) appears using FontAwesome.
  • Transitions add smooth movement for the toggle knob.
  • An optional span element can display a label or status next to the switch.

The interaction relies on the :checked + label CSS selector to update the visual state based on whether the checkbox is selected.

CSS
.demo-switch input[type=checkbox] {
  visibility: hidden;
  height: 0;
  width: 0;
}

.demo-switch input[type=radio] {
  visibility: hidden;
  height: 0;
  width: 0;
}

.demo-switch label {
  position: relative;
  display: block;
  cursor: pointer;
  background-color: #CCCCCC;
  border-radius: 100px;
  width: 66px;
  height: 30px;
}

.demo-switch input:checked + label {
  background-color: #63AE6F;
}

.demo-switch input:checked + label::after {
  left: calc(100% - 4px);
  transform: translateX(-100%);
}

.demo-switch input:checked + label::before {
  font: 18px/1 FontAwesome;
  position: absolute;
  content: '\f00c';
  top: 6px;
  left: 10px;
  color: #FFFFFF;
  font-weight: bold;
}

.demo-switch label::after {
  content: '';
  position: absolute;
  top: 4px;
  left: 6px;
  width: 22px;
  height: 22px;
  background-color: #FFFFFF;
  border-radius: 90px;
  transition: 0.3s;
}

.demo-switch span {
  display: inline-block;
  position: absolute;
  width: 160px;
  top: 2px;
  left: 90px;
  text-transform: uppercase;
  font-size: 12px;
  letter-spacing: 1px;
  text-align: center;
  border-top: 1px dashed #CCCCCC;
  border-bottom: 1px dashed #CCCCCC;
  padding: 4px 0;
}

.demo-switch span:hover {
  color: #FB9338;
}
HTML
<div class="demo-content">
 <div class="demo-switch">
  <input type="checkbox" name="checkbox-01" id="demo-checkbox-01">
   <label for="demo-checkbox-01"><span>First Item</span></label>
    <input type="checkbox" name="checkbox-02" id="demo-checkbox-02">
  <label for="demo-checkbox-02"><span>Second Item</span></label>
 </div>
</div>