Skip to content

Instantly share code, notes, and snippets.

@rdgd
Created February 12, 2016 23:43
Show Gist options
  • Save rdgd/f28efab61822d57fd0d3 to your computer and use it in GitHub Desktop.
Save rdgd/f28efab61822d57fd0d3 to your computer and use it in GitHub Desktop.
Toggle switch that doesn't require "for" attribute on label (ideal for many dynamically generated rows)
input {
display: none;
}
.capsule {
width: 60px;
height: 30px;
cursor: pointer;
display: inline-block;
position: relative;
background: rgb(189, 189, 189);
border-radius: 30px;
transition: background-color 0.4s;
-moz-transition: background-color 0.4s;
-webkit-transition: background-color 0.4s;
}
.switch {
left: 0;
width: 20px;
height: 20px;
margin: 5px;
content: '';
position: absolute;
background: #FFF;
border-radius: 10px;
}
input:checked + .capsule {
background: rgb(39, 173, 95);
}
input:checked + .capsule .switch {
left: auto;
right: 0;
}
<label>
<input type="checkbox" />
<div class="capsule">
<div class="switch"></div>
</div>
</label>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment