Html 如何将引导程序形式的输入与输入组插件对齐?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21340126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:40:44  来源:igfitidea点击:

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

htmlcsstwitter-bootstrapalignment

提问by Patryk

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

我有一个非常简单的 Bootstrap 3 表单,当我不使用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/

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

回答by danivek

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."

不要将输入组与其他组件混合使用。(参见:http: //getbootstrap.com/components/#input-groups

不要将表单组或网格列类直接与输入组混合。相反,将输入组嵌套在表单组或与网格相关的元素中。”

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"

所以你不能在同一个班级中将“col-sm-4”与“input-group”混用。您必须创建 2 个 div 类,第一个使用“col-sm-4”,另一个使用“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

更新的小提琴

回答by Felix

It's because .input-grouphas default

这是因为.input-group有默认

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

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

所以你div会拉伸到全宽,.col-sm-4默认样式为:

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

更新的小提琴

回答by cplee70

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

我发现我还需要包括: float: left 。所以,CSS是:

.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.

如果没有,当我从 v3.0.2 升级到 v3.0.3 时,我的多列行会损坏。

--cp

--cp

回答by ErcanE

.input-group[class*="col-"] is not useful if you use

.input-group[class*="col-"] 如果你使用

<fieldset>

Here is solution!

这里是解决方案!

.makeHorizontal{  
float:left;
padding-left:20px;
}