Skip to content

Instantly share code, notes, and snippets.

@pecuchet
Last active August 3, 2020 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pecuchet/f4798fe1f55dc3267e9b70579f05f870 to your computer and use it in GitHub Desktop.
Save pecuchet/f4798fe1f55dc3267e9b70579f05f870 to your computer and use it in GitHub Desktop.
HTML input checkbox as a switch button with css variables
/*
Checkbox input as a switch
---------------------------------------------------------- */
.switch {
--width: 60px;
--height: 34px;
--gutter: 4px;
--dot-width: calc(var(--height) - (var(--gutter) * 2));
--dot-color: #fff;
--color-on: #2196F3;
--color-off: #ccc;
position: relative;
display: inline-block;
vertical-align: middle;
width: var(--width);
height: var(--height);
cursor: pointer;
}
.switch input {
display: none;
}
.switch i {
display: block;
height: 100%;
background-color: var(--color-off);
border-radius: var(--height);
transition: .2s;
}
.switch i:before {
position: absolute;
left: var(--gutter);
bottom: var(--gutter);
content: "";
width: var(--dot-width);
height: var(--dot-width);
border-radius: var(--dot-width);
background: var(--dot-color);
transition: .2s;
}
.switch input:checked + i {
background-color: var(--color-on);
}
.switch input:checked + i:before {
transform: translateX(calc(var(--width) - (var(--gutter) * 2) - var(--dot-width)));
}
<label class="switch"><input type="checkbox" autocomplete="off"><i></i></label>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment