17

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.

6 Answers 6

17

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

Sign up to request clarification or add additional context in comments.

Comments

9

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

2 Comments

And I just noticed, you are using Bootstrap, but none of the actual classes you should be using to set up your form... Work on that... .control-group, .control-label, etc...
I had label, input[type="checkbox"] and span element in one line. Adding class="checkbox" to the input and class="form-inline" to the parent div did the trick.
2

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

1 Comment

Out of the answers given your answer and Giovanni's above are the only 2 that truely make bootstrap since.
2

I just solved this exact problem in bootstrap 3, by simply limiting the height of inline checkboxes to 12 pixels. They are by default 40px, I don't know why !

<div class="checkbox-inline">
    <label>
        <input type="checkbox" checked="checked" />
        <span>My correctly aligned check-box</span>
    </label>
</div>

add this in your css file (I personally have a css file named bootstrap-custom.css):

/*
Checkboxes in inline forms are misaligned because for an unknow reason they inherit a height of 40px !
This selector limit the height of inline checkboxes to 12px which is the perfect value to align them to
the other widgets in an inline form.
*/
.radio-inline, .checkbox-inline {
    max-height: 12px;
}

Comments

1

Not ideal solution but change your code to ...

<div class="span5">
    <input type="checkbox">test description</input>
  </div>

and set the margin-top on that. I will result as you want - better.

Comments

1

Bootstrap v5+

Desktop view

Mobile view

<!-- mt-md-4 pt-md-3 this apply margin and padding only for desktop -->

<div class="col-md-3 mb-3 md-mt-4 md-pt-3">
          <input class="form-check-input" type="checkbox" value="" id="flexCheckDefault">
              <label class="form-check-label" for="flexCheckDefault">
                Default checkbox
              </label>
        </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.