I have custom checkboxes I styled like buttons. When you click the label or input the div around it changes color. However, only the label and input are clickable.
Is there a way to make the entire div/button clickable (i.e. everything inside the border)?
Here's my code:
div.label {
border:solid 1px gray;
line-height:40px;
height:40px;
width: 250px;
border-radius:40px;
-webkit-font-smoothing: antialiased;
margin-top:10px;
font-family:Arial,Helvetica,sans-serif;
color:gray;
text-align:center;
}
label {
display:inline;
text-align:center;
}
input[type=checkbox] {
display: none;
}
input:checked + div {
border: solid 1px red;
color: #F00;
}
input:checked + div:before {
content: "\2713";
}
<input id="lists[Travel]" type="checkbox" name="lists[Travel]" />
<div class="label">
<label for="lists[Travel]">Travel</label> <br>
</div>
If you only use input[type=checkbox] in the CSS, you will apply the rule to any checkbox on the page. By using #checkboxes you only apply it to checkboxes that are in a div with id="checkboxes" .
To make an HTML checkbox with a clickable label means the checkbox gets on/off when the label is clicked. Using the for attribute: Create a checkbox using input tag then create a label for the created checkbox using the for attribute. How to get value of selected radio button using JavaScript?
The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!
Actually you can almost do this by positioning a real checkbox over another element and giving it 0% opacity, and a height and width as necessary. However making something visual happen based on the checkbox state would rely on browser support for the ":checked" pseudo-class, which I think remains limited.
Attach a <label> to the checkbox or radio button. Finally, wrap both the label and checkbox/radio in a <div class="cbox">. This may seem pretty confusing at first, but let us walk through section-by-section:
Here's a one way to achieve this:
<div onclick="this.querySelector('input[type=checkbox]').click()">
<input type="checkbox" style="pointer-events:none">
<span>Label</span>
</div>
Or if you give the checkbox an id
:
<div onclick="abc.click()">
<input id="abc" type="checkbox" style="pointer-events:none">
<span>Label</span>
</div>
You need pointer-events:none
in this appraoch because otherwise a double-click (causing check and uncheck) happens when you click on the checkbox directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With