How to align inputs in bootstrap form with input-group-addons?

80,986

Solution 1

Bootstrap documentation for input groups says :

Don't mix input-group with other components. (see:http://getbootstrap.com/components/#input-groups)

Do not mix form groups or grid column classes directly with input groups. Instead, nest the input group inside of the form group or grid-related element."

So you can't mix "col-sm-4" with "input-group" in the same class. You have to create 2 div class, the first with "col-sm-4" and the other with "input-group"

<div class="col-sm-4">
  <div class="input-group">
    <span class="input-group-addon">$</span>
    <input type="text" class="form-control bfh-number" id="product_price" placeholder="Price" data-min="0" data-max="9999999">
    <span class="input-group-addon">.00</span>
  </div>
</div>

Updated Fiddle

Solution 2

It's because .input-group has default

padding-right: 0;
padding-left: 0;

so your div will stretch to the full width, where as .col-sm-4 has default styles as:

padding-right: 15px;
padding-left: 15px;

So to make it work as expected, you can add this style:

.input-group[class*="col-"] {
    padding-right: 15px;
    padding-left: 15px;
}

Updated Fiddle

Solution 3

I find that I needed to include: float: left as well. So, the css is:

.input-group[class*="col-"] {
    float: left;
    padding-right: 15px;
    padding-left: 15px;
}

If not, my multi-column rows broke when I upgraded from v3.0.2 to v3.0.3.

--cp

Share:
80,986
Patryk
Author by

Patryk

Software Engineer C++/Go/shell/python coder Linux enthusiast Github profiles: https://github.com/pmalek https://github.com/pmalekn

Updated on April 22, 2020

Comments

  • Patryk
    Patryk about 4 years

    I have a very simple form with Bootstrap 3 which I can easily (automatically) align when I don't use input-group-addons.

    After I use them in my form it is impossible to align it (the line with addons is wider because of added addons)

    <form class="form-horizontal" role="form">
    
        <div class="form-group">
          <label for="product_name" class="col-sm-2 control-label">Product name</label>
          <div class="col-sm-4">
            <input type="text" class="form-control" id="product_name" placeholder="Product name">
          </div>
        </div>
    
        <div class="form-group">
          <label for="product_price" class="col-sm-2 control-label">Price</label>
          <div class="col-sm-4 input-group">
            <span class="input-group-addon">$</span>
            <input type="text" class="form-control bfh-number" id="product_price" placeholder="Price" data-min="0" data-max="9999999">
            <span class="input-group-addon">.00</span>
          </div>
        </div>
    
        <div class="form-group">
          <label for="product_description" class="col-sm-2 control-label">Description</label>
          <div class="col-sm-6">
            <textarea class="form-control" id="product_description" placeholder="Description" rows="5"></textarea>
          </div>
        </div>
    
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">Submit</button>
          </div>
        </div>
    </form>
    

    JsFiddle: http://jsfiddle.net/Yzxy3/