CSS Switch

Pure CSS On/Off switch with only two HTML elements.

by clawfire

HTML

<label class="switch_wrap" for="field">
  <input id="field" type="checkbox" value="1" />
  <div class="switch"><span class="bullet"></span></div>
  Coffee Machine
</label>

CSS

body {
  font: 14px 'Helvetica Neue', Helvetica, Arial, sans-serif;
  padding: 2em;
  background: #eee;
}

.switch_wrap input[type="checkbox"] {
  display: none;
}

.switch_wrap input[type="checkbox"]:checked + .switch {
  background-color: #6cbc43;
}

.switch_wrap input[type="checkbox"]:checked + .switch::before {
  display: block;
}

.switch_wrap input[type="checkbox"]:checked + .switch::after {
  display: none;
}

.switch_wrap input[type="checkbox"]:checked + .switch .bullet {
  left: 33px;
}

.switch_wrap .switch {
  position: relative;
  display: inline-block;
  width: 65px;
  height: 24px;
  line-height: 24px;
  margin-right: 1em;
  margin-top: -3px;
  vertical-align: middle;
  background-color: #c1272d;
  border-radius: 3px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: 0 0 3px rgba(0, 0, 0, 0.25) inset, 0 1px 0 rgb(255,255,255);
  box-shadow: 0 0 3px rgba(0, 0, 0, 0.25) inset, 0 1px 0 rgb(255,255,255);
}

.switch_wrap .switch::before,
.switch_wrap .switch::after {
  content: 'ON';
  display: none;
  width: 50%;
  position: absolute;
  top: 0;
  font-size: 11px;
  font-weight: bold;
  color: white;
  text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
  text-align: center;
}

.switch_wrap .switch::after {
  content: 'OFF';
  display: block;
  right: 0;
}

.switch_wrap .switch .bullet {
  position: relative;
  display: block;
  width: 32px;
  height: 26px;
  top: -2px;
  left: -2px;
  background-color: #f0f0f0;
  background-image: -webkit-gradient(linear, left top, left bottom, from(#f0f0f0), to(#e6e6e6));
  background-image: -webkit-linear-gradient(top, #f0f0f0, #e6e6e6);
  background-image: -moz-linear-gradient(top, #f0f0f0, #e6e6e6);
  background-image: -ms-linear-gradient(top, #f0f0f0, #e6e6e6);
  background-image: -o-linear-gradient(top, #f0f0f0, #e6e6e6);
  background-image: linear-gradient(top, #f0f0f0, #e6e6e6);
  border: 1px solid rgba(0, 0, 0, 0.3);
  border-radius: 3px;
  -webkit-box-shadow: 0 1px 0 white inset, 0 1px 1px rgba(0, 0, 0,...