Last active
August 3, 2020 12:13
-
-
Save pecuchet/f4798fe1f55dc3267e9b70579f05f870 to your computer and use it in GitHub Desktop.
HTML input checkbox as a switch button with css variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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))); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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