Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Checkbox is not working properly

I'm using the following mark up and styles (Bootstrap). It shows my checkbox but it is paralysed, that is, it cannot be checked. here is my mark up: I want something more Bootstrap-ish. I know there are other options to make the checkbox look fancy but that do not solve the problem.

<div class="form-group">
  <div class="checkbox">
    1.
    <input type="checkbox" name="options" id="chk2" />
    <label class="checkbox-label">Option 2</label>
  </div>
</div>

Here is how it looks.

checkbox not working

What exactly is the issue? If I put the input element inside label I get this ugly thing:

enter image description here

like image 626
Simple Fellow Avatar asked Feb 20 '15 14:02

Simple Fellow


2 Answers

<input type="checkbox" name="options" id="chk2" />
<label class="checkbox-label">Option 2</label>

The problem is with your label. The for attribute must match with the name attribute of your label

like image 77
broswilli Avatar answered Sep 22 '22 00:09

broswilli


Looks need to tweak bootstrap styling for custom checkbox.

Check this

HTML

<div class="form-group">
    <div class="checkbox">
        <label for="check">
            <input type="checkbox" id="check"/>
            <span class="fake-input"></span>
            <span class="fake-label">Option 2</span>
        </label>
    </div>
</div

CSS

.fake-input {
    float: left;
    width: 20px;
    height: 20px;
    border: 1px solid #9f9f9f;
    background: #fff;
    vertical-align: middle;
    position: relative;
    margin-right: 10px;
    border-radius: 4px;
}
input[type="checkbox"] {
    position: fixed;
    top: -9999px;
    left: -9999px;
}
input[type="checkbox"]:checked + .fake-input:before {
    content:"\2713";
    position: absolute;
    color: #000;
    text-align: center;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

Check in Fiddle

like image 35
raju Avatar answered Sep 23 '22 00:09

raju