twitter-bootstrap 带有 Twitter Bootstrap 的表单水平内的正常(垂直)表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13596253/
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
Normal (vertical) form inside form-horizontal with Twitter Bootstrap
提问by Se Norm
I would like to have a form which has a horizontal layout on the first level, but then within one row there can be a form "inline" which I want to have a vertical (the default) layout. Is there an easy way to achieve this?
我想要一个在第一层有水平布局的表单,但是在一行中可以有一个表单“内联”,我想要一个垂直(默认)布局。有没有简单的方法来实现这一目标?
Note:.form-inlinedoesn't do what I'm looking for, as it doesn't put the inside labels on top of the inputs.
注意:.form-inline不会做我正在寻找的东西,因为它不会将内部标签放在输入的顶部。
So far I have something like this:
到目前为止,我有这样的事情:
<div class="form-horizontal">
<div class="control-group">
<label class="control-label">
outer label
</label>
<div class="controls ### SOMETHING TO CLEAR/OVERRIDE form-horizontal ###">
### INLINE FORM WITH SAME STRUCTURE IS HERE ###
</div>
</div>
</div>
采纳答案by Arnold Daniels
Boostrap can't do this by default, but I've included this in my fork https://github.com/jasny/bootstrap/blob/master/less/jasny-forms.less#L40
Boostrap 默认无法执行此操作,但我已将其包含在我的 fork 中https://github.com/jasny/bootstrap/blob/master/less/jasny-forms.less#L40
Please consider using Jasny's extensions to Bootstrap http://jasny.github.com/bootstrap
请考虑使用 Jasny 的 Bootstrap 扩展http://jasny.github.com/bootstrap
or just use this CSS
或者只是使用这个 CSS
.form-vertical .form-horizontal .control-group > label {
text-align: left;
}
.form-horizontal .form-vertical .control-group > label {
float: none;
padding-top: 0;
text-align: left;
}
.form-horizontal .form-vertical .controls {
margin-left: 0;
}
.form-horizontal .form-vertical.form-actions,
.form-horizontal .form-vertical .form-actions {
padding-left: 20px;
}
.control-group .control-group {
margin-bottom: 0;
}
With the HTML
使用 HTML
<form class="form-horizonal">
# Horizal controls here
<div class="form-vertical">
<div class="control-group">
<label class="control-label">Label</label>
<div class="controls">Something here</div>
</div>
</div>
</form>
回答by David K.
Here is @jasny-arnold-daniels CSS updated for Boostrap 3. In 3 form-groupreplaces control-group, form-controlreplaces control. Also needs @user9645 's change to width. New CSS is:
这是为 Boostrap 3 更新的 @jasny-arnold-daniels CSS。在 3 中form-group替换control-group,form-control替换control。还需要 @user9645 更改宽度。新的 CSS 是:
.form-vertical .form-horizontal .form-group > label {
text-align: left;
}
.form-horizontal .form-vertical .form-group > label {
float: none;
padding-top: 0;
text-align: left;
width: 100%
}
.form-horizontal .form-vertical .form-control {
margin-left: 0;
}
.form-horizontal .form-vertical.form-actions,
.form-horizontal .form-vertical .form-actions {
padding-left: 20px;
}
.form-group .form-group {
margin-bottom: 0;
}
回答by Sia
You do not need to write custom CSS for Bootstrap 3. Instead of putting your class of form-horizontalon the form tag, only put a wrapper div with that class around the form elements that you want to be horizontal (and supporting column-size classes for the horizontal items).
您不需要为 Bootstrap 3 编写自定义 CSS。而不是将您的表单水平类放在表单标签上,只需在您想要水平的表单元素周围放置一个带有该类的包装 div(并支持列大小水平项目的类)。
For example, this would work:
例如,这将起作用:
<form>
<div class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">I'm a horizontal form element</label>
<div class="col-sm-10">
<input type="text" class="form-control">
</div>
</div>
</div>
<div class="form-group">
<label>I'm a vertical form element</label>
<input type="text" class="form-control">
</div>
</form>
This works because the vertical form is automatically used for a form with no class explicitly stated. That 'lack of a class' can be seen in the docs here.
这是有效的,因为垂直表单会自动用于没有明确声明类的表单。可以在此处的文档中看到“缺少课程” 。
回答by zmanc
I was unsure exactly what you were looking for but I tried to guess.
我不确定你在找什么,但我试着猜测。
I made a form with a two vertical lines the second line with multiple form elements.
我做了一个带有两条垂直线的表单,第二行带有多个表单元素。
I used inline-block to do this.
我使用 inline-block 来做到这一点。

