Can anyone explain why the tick does not appear when I check my custom checkbox and help me fix the issue? I attempted to replicate the example from W3Schools and here is my code:
.bottom {
display: flex;
justify-content: space-between;
width: 300px;
}
.left {
display: flex;
flex-direction: row;
position: relative;
}
/* Hide the actual checkbox */
input[type=checkbox] {
opacity: 0;
margin-right: 4px;
}
/* Create a custom checkbox*/
.checkbox::before {
content: "";
border: none;
border-radius: 2px;
height: 16px;
width: 16px;
position: absolute;
background-color: #737373;
}
.checkbox::after {
content: "";
border: 3px solid;
border-top: 0;
border-left: 0;
width: 4px;
height: 9px;
position: absolute;
top: 1px;
left: 5px;
transform: rotate(45deg);
opacity: 0;
transition: opacity 10ms;
}
.checkbox input:checked ~ .checkbox::after {
opacity: 1;
}
<div class="bottom">
<div class="left">
<div class="checkbox" style="margin-right: 3px;">
<input type="checkbox">
</div>
<span>Remember me</span>
</div>
<span>Need help?</span>
</div>
Thanks in advance!
2
Answers
You had a issue in the last block of your css, exactly here:
Explanation:
You were selecting an element by a class name
checkbox
using a precedence selector. The above css block meant: "Search for element with classcheckbox
and inside it search for an input element which is checked:checked
and then search for element with classcheckbox
which is available after the selected input which is set checked or in other words search for element with classcheckbox
which is preceded by input:checked element but in your html you have no element after the input tag,hence it is not working. Also the:after
and:before
selectors overlaps the actual input checkbox hence you were not be able to click on it. This is solved by setting thez-index
of both the selectors to -1.The corrected code snippet is as follows:
I hope I am clear with the explanation and the solution is working on your side as well. Cheers!
There is many errors in your code :
Also, you should add javascript to trigger click on the input when clicking on the before