Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertically align label and input in Bootstrap 3?

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>
like image 794
fat fantasma Avatar asked Aug 29 '13 19:08

fat fantasma


People also ask

How do I vertically align an item in bootstrap?

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 . row to vertically center align all col-* in the row...

How do I center vertically in Bootstrap 3?

Another way to vertically center is to use my-auto . This will center the element within its container. For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column. Since Bootstrap 4 .

How do you align inputs and labels?

We specify the margin-bottom of our <div> element. Then, we set the display of the <label> element to "inline-block" and give a fixed width. After that, set the text-align property to "right", and the labels will be aligned with the inputs on the right side.


2 Answers

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> 
like image 184
ajiang Avatar answered Sep 30 '22 17:09

ajiang


I'm sure you've found your answer by now, but for those who are still looking for an answer:

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>
like image 43
Bogac Avatar answered Sep 30 '22 18:09

Bogac