<form action="">
<input type="radio" name="rdo" id="yes" checked />
<input type="radio" name="rdo" id="no" />
<div class="switch">
<label for="yes">Yes</label>
<label for="no">No</label>
<span></span>
</div>
</form>
@import url("https://fonts.googleapis.com/css2?family=Exo:wght@600&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
width: 100vw;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: "Exo", Arial, sans-serif;
background-color: #151522;
gap: 10px;
}
.switch {
position: absolute;
top: 50%;
left: 50%;
width: 150px;
height: 50px;
text-align: center;
margin: -30px 0 0 -75px;
transition: all 0.2s ease;
border-radius: 25px;
}
.switch span {
position: absolute;
width: 20px;
height: 4px;
top: 50%;
left: 50%;
margin: -2px 0px 0px -4px;
background: #fff;
display: block;
transform: rotate(-45deg);
transition: all 0.2s ease;
border-radius: 2px;
}
.switch span:after {
content: "";
display: block;
position: absolute;
width: 4px;
height: 12px;
margin-top: -8px;
background: #fff;
transition: all 0.2s ease;
border-radius: 2px;
}
input[type="radio"] {
display: none;
}
.switch label {
cursor: pointer;
color: var(--unchecked-color);
width: 60px;
line-height: 50px;
transition: all 0.2s ease;
}
label[for="yes"] {
position: absolute;
left: 0px;
height: 20px;
}
label[for="no"] {
position: absolute;
right: 0px;
}
#no:checked ~ .switch span {
margin-left: -8px;
}
#no:checked ~ .switch span:after {
height: 20px;
margin-top: -8px;
margin-left: 8px;
}
:root {
--ON: initial;
--OFF: ;
}
.switch {
--checked-bg-color: #4cd964;
--checked-color: #fff;
--unchecked-color: rgba(0, 0, 0, 0.2);
--unchecked-bg-color: #ff3b30;
background: var(--checked, var(--checked-bg-color))
var(--unchecked, var(--unchecked-bg-color));
color: var(--unchecked-color);
}
#no:checked ~ .switch {
--checked: var(--OFF);
--unchecked: var(--ON);
}
#yes:checked ~ .switch {
--checked: var(--ON);
--unchecked: var(--OFF);
}
#yes:checked ~ .switch label[for="yes"],
#no:checked ~ .switch label[for="no"] {
color: var(--checked-color);
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.