I am trying to create a CSS only custom radio with label, and I cannot figure out why it is not getting checked.
I'm expecting that when I click the custom radio it should get checked. In reality, the actual result is that when I click on the custom radio it does not get checked.
Please explain why is this happening.
Many thanks!
Here it is:
.choice {
display: block;
width: fit-content;
margin: 0.25em 0 0 1.5em;
padding-top: 0.2em;
padding-left: 0.2em;
font-size: 1.5em;
cursor: pointer;
}
.container {
cursor: pointer;
top: -0.125em;
position: relative;
}
input {
position: absolute;
opacity: 0;
visibility: hidden;
}
.checkmark {
display: inline-block;
position: relative;
height: 0.8em;
width: 0.8em;
outline-style: none;
box-shadow: 0 0 0 0 rgba(0, 0, 0, 0);
background-color: rgba(0, 0, 0, 0);
border: 0.1em solid #2185d0;
border-radius: 50%;
transition: all 0.5s;
cursor: pointer;
}
.checkmark:after {
content: "";
width: 0.6em;
height: 0.6em;
position: relative;
top: 50%;
left: 50%;
background-color: #2185d0;
border-radius: 50%;
display: block;
cursor: pointer;
transform: translate(-50%, -50%) scale(0);
transition: all 0.2s;
}
.choice:hover input~.checkmark {
box-shadow: 0 0 0 0.25em rgba(33, 133, 208, 0.3);
background-color: rgba(33, 133, 208, 0.3);
}
input[checked]~.checkmark:after {
transform: translate(-50%, -50%) scale(1);
}
<div class="choice">
<input type="radio" name="radio" id="radio1" />
<span class="checkmark"></span>
<label for="radio1" class="container">One</label>
</div>
Thank you Temani Afif
The :checked selector matches every checked element (only for radio buttons and checkboxes) and element.
I made the mistake of using input[checked]
instead of input:checked