Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Make Entire Custom Checkbox/Div Clickable -- Not Just the Label + Input

Tags:

html

css

checkbox

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>
like image 226
amyyyyy Avatar asked Aug 15 '14 00:08

amyyyyy


People also ask

How do I make a DIV act like a checkbox?

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" .

How to make an HTML checkbox with a clickable label?

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?

What is a checkbox in HTML?

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!

Is there a way to make a checkbox look like this?

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.

How do I add labels to checkboxes and radio buttons?

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:


1 Answers

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.

like image 86
2 revs Avatar answered Sep 26 '22 01:09

2 revs