Skip to content

Instantly share code, notes, and snippets.

@matt-daniel-brown
Created December 10, 2020 16:11
Show Gist options
  • Save matt-daniel-brown/0c634712400039233e87716474694989 to your computer and use it in GitHub Desktop.
Save matt-daniel-brown/0c634712400039233e87716474694989 to your computer and use it in GitHub Desktop.
Simple CSS Switch / Toggle
<label class="switch">
<input type="checkbox">
<div>
<span></span>
</div>
</label>
<label class="switch">
<input type="checkbox" checked>
<div>
<span></span>
</div>
</label>
<!-- dribbble -->
<a class="dribbble" href="https://dribbble.com/shots/7587327-Switch" target="_blank"><img src="https://cdn.dribbble.com/assets/dribbble-ball-mark-2bd45f09c2fb58dbbfb44766d5d1d07c5a12972d602ef8b32204d28fa3dda554.svg" alt=""></a>
.switch {
--line: #505162;
--dot: #F7F8FF;
--circle: #9EA0BE;
--duration: .3s;
--text: #9EA0BE;
cursor: pointer;
input {
display: none;
& + div {
position: relative;
&:before,
&:after {
--s: 1;
content: '';
position: absolute;
height: 4px;
top: 10px;
width: 24px;
background: var(--line);
transform: scaleX(var(--s));
transition: transform var(--duration) ease;
}
&:before {
--s: 0;
left: 0;
transform-origin: 0 50%;
border-radius: 2px 0 0 2px;
}
&:after {
left: 28px;
transform-origin: 100% 50%;
border-radius: 0 2px 2px 0;
}
span {
padding-left: 56px;
line-height: 24px;
color: var(--text);
&:before {
--x: 0;
--b: var(--circle);
--s: 4px;
content: '';
position: absolute;
left: 0;
top: 0;
width: 24px;
height: 24px;
border-radius: 50%;
box-shadow: inset 0 0 0 var(--s) var(--b);
transform: translateX(var(--x));
transition: box-shadow var(--duration) ease, transform var(--duration) ease;
}
&:not(:empty) {
padding-left: 64px;
}
}
}
&:checked {
& + div {
&:before {
--s: 1;
}
&:after {
--s: 0;
}
span {
&:before {
--x: 28px;
--s: 12px;
--b: var(--dot);
}
}
}
}
}
}
html {
box-sizing: border-box;
-webkit-font-smoothing: antialiased;
}
* {
box-sizing: inherit;
&:before,
&:after {
box-sizing: inherit;
}
}
// Center & dribbble
body {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
position: relative;
background: #262730;
.switch {
& + .switch {
margin-top: 32px;
}
}
.dribbble {
position: fixed;
display: block;
right: 20px;
bottom: 20px;
img {
display: block;
height: 28px;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment