Skip to content

Instantly share code, notes, and snippets.

@bodnaristvan
Last active July 6, 2020 12:53
Show Gist options
  • Save bodnaristvan/7fa1cab9961c1fb53673b826a0c990b7 to your computer and use it in GitHub Desktop.
Save bodnaristvan/7fa1cab9961c1fb53673b826a0c990b7 to your computer and use it in GitHub Desktop.
CSS-only slider toggle input
input {
display: none;
}
label {
position: relative;
display: flex;
align-items: center;
width: 200px;
height: 30px;
overflow: hidden;
border: 1px solid grey;
border-radius: 5px;
background-color: rgba(180, 180, 180, 0.6);
font-family: system-ui;
color: white;
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
user-select: none;
}
.label {
flex: 1 0 0;
text-align: center;
}
/* "highlight" box -- box-shadow will set the overlay */
label:after {
content: '';
position: absolute;
flex: 1 0 0;
height: 100%;
width: 50%;
box-sizing: border-box;
border: 1px solid rgba(0, 0, 0, 0.6);
border-radius: 5px;
box-shadow: 0px 0px 0px 100vw rgba(0, 0, 0, 0.3);
transition: transform 250ms ease;
transform: translateX(0);
}
/* selected checkbox style */
input[type="checkbox"]:checked+label:after{
transform: translateX(100%);
}
<input type="checkbox" id="toggler" name="isInviting" />
<label for="toggler">
<span class="label">Share</span>
<span class="label">Invite</span>
</label>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment