Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap form-horizontal vertical alignment of checkboxes without label text

I have changed from Bootstrap 3.0.0 to 3.2.0 this morning because I needed some of the new features for my web application. Everything seemed to work as expected until I observed an issue with the vertical alignment of checkboxes in a .form-horizontal form.

An example is available at http://www.bootply.com/AYN64feYze. The markup for this minimum example is:

<div class="form-horizontal">
    <div class="form-group">
      <label class="col-sm-2 control-label">With label text</label>
          <div class="col-sm-10">
            <div class="checkbox">
              <label>
                  <input type="checkbox"> label text
              </label>
            </div>
          </div>
    </div>
  <div class="form-group">
      <label class="col-sm-2 control-label">Without label text</label>
          <div class="col-sm-10">
            <div class="checkbox">
              <label>
                  <input type="checkbox">
              </label>
            </div>
          </div>
    </div>
</div>

If a checkbox has no following text it is shifted below the row it should appear in.

Is there a solution to this problem? Since I already have the leading label I do not need a following text for my checkboxes. My current workaround is adding text to the <label> that contains the <input type="checkbox"> and use the background color as the font color to hide the text.

Thank you for your help.

like image 281
Thylossus Avatar asked Jul 23 '14 13:07

Thylossus


People also ask

How do I align a checkbox horizontally?

Set the checkbox horizontally by including the data-type = "horizontal" to the fieldset. You can select the checkbox button more than one at a time.

How do I make checkboxes align?

Method 1: By making the position of checkbox relative, set the vertical-align to the middle can align the checkboxes and their labels. Here, we have made the position of the checkbox relative to the label. So the checkboxes are aligned according to the label.

How can I customize a bootstrap checkbox?

To create a custom checkbox, wrap a container element, like <div>, with a class of . custom-control and . custom-checkbox around the checkbox.

How do I change the position of a checkbox in HTML?

Float the checkbox to the right and the label to the left, and set the label position to absolute. If you just put the label text to the right of the box without a label, it will not cause the checkbox to be checked when clicked.


2 Answers

I'm not sure if this will affect the rest of your form layout, but the issue seems to be resolved if you change the display attribute of <label> (currently set to inline-block) to:

label{
    display:inline;
}

Here's an updated Bootply. Hope this helps! Let me know if you have any questions.

like image 196
Serlite Avatar answered Sep 18 '22 05:09

Serlite


This worked for me:

<div class="form-group">
    <div class="col-lg-3">
        <label class="pull-right" for="MyCheckBox">My Checkbox</label>
    </div>
    <div class="col-lg-9">
        <input type="checkbox" name="MyCheckBox">
    </div>
</div>    

First, I had to remove the <div class='checkbox'> element. I then made the following changes to the checkbox label element:

  1. Place the label in its own column div <div class="col-lg-3"></div>
  2. Remove class="control-label"
  3. Add class="pull-right".

I ended up with a checkbox that aligned with the other inputs horizontally and with its label vertically.

like image 35
Nathan Agersea Avatar answered Sep 18 '22 05:09

Nathan Agersea