45

How to vertically align label and input in Bootstrap 3? I would like both the label and input on same line. I have played around with different classes and custom CSS but nothing seems to work.

The JSfiddle

<div class="form-group">
    <div class="row">
        <div class="col-xs-3">
            <label for="class_type"><h2><span class=" label label-primary">Class Type</span></h2></label>
        </div>
        <div class="col-xs-2">
            <select name="class_type" id="class_type" class="  form-control input-lg" style="width:200px" autocomplete="off">
                <option >Economy</option>
                <option >Premium Economy</option>
                <option >Club World</option>
                <option >First Class</option>
            </select>
        </div>
     </div>
</div>
madth3
  • 7,275
  • 12
  • 50
  • 74
fat fantasma
  • 7,483
  • 15
  • 48
  • 66

5 Answers5

47

The bootstrap 3 docs for horizontal forms let you use the .form-horizontal class to make your form labels and inputs vertically aligned. The structure for these forms is:

<form class="form-horizontal" role="form">
  <div class="form-group">
    <label for="input1" class="col-lg-2 control-label">Label1</label>
    <div class="col-lg-10">
      <input type="text" class="form-control" id="input1" placeholder="Input1">
    </div>
  </div>
  <div class="form-group">
    <label for="input2" class="col-lg-2 control-label">Label2</label>
    <div class="col-lg-10">
      <input type="password" class="form-control" id="input2" placeholder="Input2">
    </div>
  </div>
</form>

Therefore, your form should look like this:

<form class="form-horizontal" role="form">
    <div class="form-group">
        <div class="col-xs-3">
            <label for="class_type"><h2><span class=" label label-primary">Class Type</span></h2></label>
        </div>
        <div class="col-xs-2">
            <select id="class_type" class="form-control input-lg" autocomplete="off">
                <option>Economy</option>
                <option>Premium Economy</option>
                <option>Club World</option>
                <option>First Class</option>
            </select>
        </div>
    </div>
</form>
ajiang
  • 1,702
  • 2
  • 12
  • 12
  • 1
    Your second example has the same result as mine. I would like everything on one line and aligned properly. Here is the jsfiddle to your example:http://jsfiddle.net/fatfantasma/6xH5s/. Any other suggestions? – fat fantasma Aug 29 '13 at 20:41
  • For me, what worked was to add `form-horizontal` to my `
    ` which is probably frowned on but it worked for me! This was required for me since I was using `form-inline`. So my final solution looked like this `
    `
    – hvaughan3 Feb 11 '16 at 16:12
  • form-horizontal worked for me and saved me from adding css to individual labels and input boxes. Thanks! – Murtuza K Jul 18 '20 at 18:40
27

When input-lg is used, margins mismatch unless you use form-group-lg in addition to form-group class. Its example is in docs:

<form class="form-horizontal">
  <div class="form-group form-group-lg">
    <label class="col-sm-2 control-label" for="formGroupInputLarge">Large label</label>
    <div class="col-sm-10">
      <input class="form-control" type="text" id="formGroupInputLarge" placeholder="Large input">
    </div>
  </div>
  <div class="form-group form-group-sm">
    <label class="col-sm-2 control-label" for="formGroupInputSmall">Small label</label>
    <div class="col-sm-10">
      <input class="form-control" type="text" id="formGroupInputSmall" placeholder="Small input">
    </div>
  </div>
</form>
isherwood
  • 58,414
  • 16
  • 114
  • 157
Bogac
  • 3,596
  • 6
  • 35
  • 58
19

None of these solutions worked for me. But I was able to get vertical centering by using <div class="form-row align-items-center"> for each form row, per the Bootstrap examples.

MidnightJava
  • 1,927
  • 2
  • 18
  • 38
12

The problem is that your <label> is inside of an <h2> tag, and header tags have a margin set by the default stylesheet.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Blowsie
  • 40,239
  • 15
  • 88
  • 108
  • How would I go about removing that margin if possible? – fat fantasma Aug 29 '13 at 20:49
  • Ok, Just learning this stuff. I modified the h2 tag so it adjusts the margin-top css. Thanks for pointing me in the correct direction. Updated fiddle http://jsfiddle.net/fatfantasma/6xH5s/1/ – fat fantasma Aug 29 '13 at 22:57
2

This works perfectly for me in Bootstrap 4.

<div class="form-row align-items-center">
   <div class="col-md-2">
     <label for="FirstName" style="margin-bottom:0rem !important;">First Name</label>
 </div>
 <div class="col-md-10">
    <input type="text" id="FirstName" name="FirstName" class="form-control" val=""/>
  /div>
</div>