Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap checkbox input align vertically

Using Bootstrap version 2.3.2, I have a form layout like the below image and since the checkbox has an inline label, there is an aligning issue.

enter image description here

Adding margin to input[type="checkbox"] only gives margin to the checkbox, not the inline label. How do I make it so the checkbox and its label vertically align to the text fields next to it?

Here is the JS BIN if you are interested.

like image 840
Seong Lee Avatar asked Oct 11 '13 00:10

Seong Lee


People also ask

How do I center text vertically in bootstrap?

In Bootstrap 5, if we want to vertically align an <div> element in the center, we can do this by applying the class align-items-center on the containing element of that div.

How do I center a row vertically in bootstrap?

One way to vertically center is to use my-auto . This will center the element within it's flexbox container (The Bootstrap 4 . row is display:flex ). For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column.

How do I vertically center a div in bootstrap 4?

Since Bootstrap 4 . row is now display:flex you can simply use align-self-center on any column to vertically center it... or, use align-items-center on the entire .


3 Answers

In your HTML add a class that will handle the checkbox margin:

 <div class="container-fluid">
  <div class="row-fluid">
    <div class="span3">
      <label>label 1</label>
      <input type="text" />
    </div>
    <div class="span3">
      <label>label 2</label>
      <input type="text" />
    </div>
    <div class="span3 checkbox">
        <input type="checkbox" />test description
    </div>
  </div>
</div>

and in your CSS:

input[type="checkbox"] {
 // i just remove this part..
}
.checkbox {
  margin: 30px 0 0 0;
}

Don't put the margin on the checkbox, but on the parent div. Check this jsFiddle.

Hope this helps

like image 144
Vainglory07 Avatar answered Nov 10 '22 01:11

Vainglory07


Try to always use something like this:

<div class="span3">
    <label for="checkbox" class="checkbox">
        <input type="checkbox" id="checkbox" class="checkbox">test description
    </label>
</div>

http://jsbin.com/itAdAWA/1/edit

like image 32
Giovanni Silveira Avatar answered Nov 10 '22 03:11

Giovanni Silveira


How about putting a <label> before the checkbox like this? ..

<div class="container-fluid">
    <div class="row-fluid">
      <div class="span3">
        <label>label 1</label>
        <input type="text">
      </div>
      <div class="span3">
        <label>label 2</label>
        <input type="text">
      </div>
      <div class="span3">
        <label>test</label>
        <input type="checkbox">
      </div>
    </div>
  </div>

Bootply: http://bootply.com/86998

like image 2
Zim Avatar answered Nov 10 '22 03:11

Zim