'Custom checkbox without label's "for" attribute (Bootstrap 4)
Is there a way to still have the Bootstrap 4 custom checkbox styling without using the id on the input and the attribute for on the label? The styling for checked is not there when you remove it.
Example:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="custom-control custom-checkbox mb-3">
<input type="checkbox" class="custom-control-input" id="customControlValidation1" required>
<label class="custom-control-label">Check this custom checkbox</label>
</div>
I've tried wrapping the input with label, but that doesn't seem to do anything either. Is there a way I could avoid giving the input a static id and still get the Boostrap styled checkbox? Thanks!
Solution 1:[1]
Here is simplest solution. Label should be used as wrapper for checkbox being clickable.
<div class="custom-control custom-checkbox">
<label><input type="checkbox" name="checkbox-name" class="custom-control-input">
<div class="custom-control-label"></div>
</label>
</div>
Solution 2:[2]
yes. simple with no JS or id
<style>
.custom-checkbox {
padding: 0;
}
.custom-checkbox .fa-toggle-on,
.custom-checkbox .fa-toggle-off {
font-size: 135%;
/*this icon is relatively small*/
}
.custom-checkbox input[type=checkbox] {
visibility: collapse;
width: 0px;
margin-left: -0.25em;
}
.custom-checkbox input[type=checkbox] ~ .custom-check-on {
display: none;
}
.custom-checkbox input[type=checkbox]:checked ~ .custom-check-on {
display: inline;
}
.custom-checkbox input[type=checkbox]:checked ~ .custom-check-off {
display: none;
}
.custom-checkbox input[type=checkbox]:disabled ~ * {
color: #b6b4b4;
}
.custom-checkbox input[type=checkbox].error ~ .custom-check-on,
.custom-checkbox input[type=checkbox].error ~ .custom-check-off {
border: solid 2px red;
}
.custom-checkbox i.btn {
overflow: hidden;
color: transparent;
position: relative;
display: inline-block;
width: 3em;
padding: 0;
font-style: normal;
}
.custom-checkbox .btn:after {
content: "";
font-style: normal;
border: 7px solid white;
position: absolute;
top: 0;
bottom: 0;
border-radius: 5px;
}
.custom-checkbox .custom-check-on.btn:after {
right: -4px;
}
.custom-checkbox .custom-check-off.btn:after {
left: -4px;
}
.custom-checkbox .custom-check-on.btn:before {
content: "On";
color: white;
margin-left: -10px;
}
.custom-checkbox .custom-check-off.btn:before {
content: "Off";
color: #333;
margin-right: -15px;
}
.custom-checkbox input[type=checkbox]:checked ~ .btn.custom-check-on {
display: inline-block;
}
</style>
<link href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<label class="custom-checkbox">
<input name="check" type="checkbox" autocomplete="off" >
<i class="custom-check-on btn btn-primary btn-sm">
</i><i class="custom-check-off btn btn-light active btn-sm">
</i><input name="check" type="hidden" value="false">
<span class="ml-1">
Check Box
</span>
</label>
<br>
<label class="custom-checkbox">
<input name="check" type="checkbox" autocomplete="off" >
<i class="custom-check-on fa fa fa-circle">
</i><i class="custom-check-off fal fa-circle">
</i><input name="check" type="hidden" value="false">
<span class="ml-1">
Check Box
</span>
</label>
<br>
<label class="custom-checkbox">
<input name="check" type="checkbox" autocomplete="off" >
<i class="custom-check-on far fa-check-square" style="color:black" >
</i><i class="custom-check-off far fa-square" style="color:lightgray">
</i><input name="check" type="hidden" value="false">
<span class="ml-1">
Check Box
</span>
</label>
<br>
<label class="custom-checkbox">
<input name="check" type="checkbox" autocomplete="off" >
<i class="custom-check-on text-info fa fa-toggle-on">
</i><i class="custom-check-off text-secondary fa fa-toggle-off">
</i><input name="check" type="hidden" value="false">
<span class="ml-1">
Check Box
</span>
</label>
<br>
<label class="custom-checkbox">
<input name="check" type="checkbox" autocomplete="off" >
<i class="custom-check-on text-success far fa-smile">
</i><i class="custom-check-off text-danger fas fa-angry">
</i><input name="check" type="hidden" value="false">
<span class="ml-1">
Check Box
</span>
</label>
<br>
<label class="custom-checkbox">
<input name="check" type="checkbox" autocomplete="off" >
<i class="custom-check-on fas fa-arrow-alt-circle-up">
</i><i class="custom-check-off far fa-arrow-alt-circle-down">
</i><input name="check" type="hidden" value="false">
<span class="ml-1">
Check Box
</span>
</label>
<br>
</div>
Solution 3:[3]
no that is not possible to remove ID and For.
because the for="customControlValidation1" attribute allow us to click the id="customControlValidation1" means when we click the label it considers checkbox click and checkbox state will change.
Solution 4:[4]
i sure this work perfectly
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="custom-control custom-checkbox mb-3">
<input type="checkbox" class="custom-control-input" id="customControlValidation1" required>
<label class="custom-control-label" for="customControlValidation1">Check this custom checkbox</label>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | THess |
| Solution 2 | Kevin Eghbali |
| Solution 3 | KuldipKoradia |
| Solution 4 | Himali Patel |
