HTML5/CSS3 Toggle Switch

HTML5/CSS3 Toggle Switch

by Luis Valle

HTML

<div>
    <input type="checkbox" id="switch1" name="switch1" class="switch" />
    <label for="switch1">First switch</label>
</div>
<div class="clear">&nbsp;</div>
<div>
    <input type="checkbox" id="switch2" name="switch2" class="switch" />
    <label for="switch2">Second switch</label>
</div>

CSS

body{
    font: small arial,helvetica,sans-serif;
}
.clear {
    clear: both;
}
input.switch:empty {
    margin-left: -999px;
}
input.switch:empty ~ label {
    position: relative;
    float: left;
    line-height: 1.6em;
    text-indent: 4em;
    margin: 0.2em 0;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
input.switch:empty ~ label:before, input.switch:empty ~ label:after {
    position: absolute;
    display: block;
    top: 0;
    bottom: 0;
    left: 0;
    content:' ';
    width: 3.6em;
    background-color: #E0E0E0;
    border-radius: 0.3em;
    box-shadow: inset 0 0.2em 0 rgba(0, 0, 0, 0.3);
    -webkit-transition: all 100ms ease-in;
    transition: all 100ms ease-in;
}
input.switch:empty ~ label:after {
    width: 1.4em;
    top: 0.1em;
    bottom: 0.1em;
    margin-left: 0.1em;
    background-color: #E0E0E0;
    border-radius: 0.15em;
    box-shadow: inset 0 -0.2em 0 rgba(0, 0, 0, 0.2);
}
input.switch:checked ~ label:before {
    background-color: #393;
}
input.switch:checked ~ label:after {
    margin-left: 2em;
}