Skip to content

Instantly share code, notes, and snippets.

@r3id
Created March 10, 2020 20:37
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 r3id/baf83e79c0e789fd239f3a1cf040c7ac to your computer and use it in GitHub Desktop.
Save r3id/baf83e79c0e789fd239f3a1cf040c7ac to your computer and use it in GitHub Desktop.
CSS only toggle switch
<div class="demo-holder">
<form action="#">
<div class="switch">
<input id="switch-1" type="checkbox" class="switch-input">
<label for="switch-1" class="switch-label">Switch</label>
</div>
</form>
<style>
form {
margin: 0 auto;
width: 50px;
}
.switch {
position: relative;
display: inline-block;
}
.switch-input {
display: none;
}
.switch-label {
display: block;
width: 55px;
height: 28px;
padding: 0;
text-indent: -150%;
clip: rect(0 0 0 0);
color: transparent;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.switch-label:before,
.switch-label:after {
content: "";
display: block;
position: absolute;
cursor: pointer;
}
.switch-label:before {
width: 100%;
height: 100%;
background-color: #dedede;
border-radius: 9999em;
-webkit-transition: background-color 0.25s ease;
transition: background-color 0.25s ease;
}
.switch-label:after {
top: 2px;
left: 2px;
width: 24px;
height: 24px;
border-radius: 50%;
background-color: #fff;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.45);
-webkit-transition: left 0.25s ease;
transition: left 0.25s ease;
}
.switch-input:checked + .switch-label {
}
.switch-input:checked + .switch-label:before {
background-color: #4bd763;
}
.switch-input:checked + .switch-label:after {
left: 30px;
}
</style>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment