问题 Bootstrap 3:如何在列内使用表单组?


我正在使用Bootstrap 3,我有一个相当标准的页面布局:左边一个宽列(.col-md-8),包含纯文本,右侧有一个较窄的列(.col-md-4),包含一个表格。

反过来,每个表单字段都包含在一个 .form-group

我的第一次尝试.form-groups 溢出了包含列的左右边缘。 (确保JSFiddle预览框架至少与Bootstrap的sm断点一样宽。)我添加了一个粉红色的背景div来显示框中的 .form-groups  应该 呆在里面。

我的第二次尝试,我补充说 .container 里面的 .col-md-4,然后包裹每个 .form-group 里面的 .row   一个 .col-md-4

这样做,但......这是正确和首选的方式吗?这似乎是一个非常多的额外的,样板标记,以实现应该有点自然发生的事情。

Bootstrap文档相当不错,但它们掩盖了像这样的一些“大图”的东西。也许这些东西对于那些已经熟悉响应式CSS的人来说是显而易见的,但对于像我这样的初学者来说这可能会让人感到困惑。

这是我第一次尝试的代码:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>
  <script type='text/javascript' src='//code.jquery.com/jquery-2.1.0.js'></script>
  <script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
  <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="/css/result-light.css">
</script>
<head>
<body>
  <div class="container">

    <h1>Broken version</h1>
    <h2>Don't forget to expand the width of this preview window. Otherwise you'll just see Boostrap's xs (vertically stacked) layout.</h2>

    <div class="row">

        <div class="col-md-8">

            <div style="background-color: orange;">
                <p>This column will be filled with text. Lorem ipsum dolor sit amet...</p>
            </div>

        </div> <!-- .col-md-8 -->

        <div class="col-md-4">

            <div style="background-color: pink;">

                <form role="form" class="form-horizontal">

                    <div class="form-group">
                        <label class="control-label" for="name">Name</label>
                        <input class="form-control" type="text" name="name" id="name">                    
                    </div>

                    <div class="form-group">
                        <label class="control-label" for="email">Email</label>
                        <input class="form-control" type="email" name="email" id="email">                    
                    </div>

                    <button type="submit">Submit</button>

                </form>

            </div> <!-- pink background div -->

        </div> <!-- .col-md-4 -->

    </div> <!-- .row -->

</div> <!-- .container -->

</body>

</html>

这是我的第二次可能更正的尝试的代码:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>

  <script type='text/javascript' src='//code.jquery.com/jquery-2.1.0.js'></script>
  <script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
  <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="/css/result-light.css">
</head>
<body>
  <div class="container">

    <h1>Corrected (?) version</h1>
    <h2>Don't forget to expand the width of this preview window. Otherwise you'll just see Boostrap's xs (vertically stacked) layout.</h2>

    <div class="row">

        <div class="col-md-8">

            <div style="background-color: orange;">
                <p>This column will be filled with text. Lorem ipsum dolor sit amet...</p>
            </div>

        </div> <!-- .col-md-8 -->

        <div class="col-md-4">

            <div style="background-color: pink;">

                <div class="container">

                    <form role="form" class="form-horizontal">

                        <div class="row">
                            <div class="form-group col-md-4">
                                <label class="control-label" for="name">Name</label>
                                <input class="form-control" type="text" name="name" id="name">                    
                            </div>
                        </div>

                        <div class="row">
                            <div class="form-group col-md-4">
                                <label class="control-label" for="email">Email</label>
                                <input class="form-control" type="email" name="email" id="email">                    
                            </div>
                        </div>

                        <button type="submit">Submit</button>

                    </form>

                </div> <!-- .container -->

            </div> <!-- .pink background div -->

        </div> <!-- .col-md-4 -->

    </div> <!-- .row -->

</div> <!-- .container -->

</body>

</html>

4648
2017-10-10 16:43


起源

您不需要使用.form-horizo​​ntal,因此不要因为这不是.form水平情况。查看.form-horizo​​ntal示例的文档,这不是您使用该类的地方。在表单上不使用任何类,并且看到您不需要.container将.form-group上的负边距清零 - Christina
如果你想要一些底部边距,你可以使用.form-group而不是.row。 - Tobi G.


答案:


之前的两个答案都缺乏阅读Bootstrap文档。

您不需要使用 .form-horizontal 所以不要因为这不是一个 .form-horizontal 情况。看看文档 .form-horizontal 例子,这不是您使用该类的地方。在表单上不使用任何类,然后您不需要.container将负边距归零 .form-group

http://jsfiddle.net/fr6p90ar/6/

和没有你的一样 .form-horizontal 并且没有嵌套的.container(阅读文档)。

这个:

<div class="form-group col-md-4">

就像在.row上放一个列类一样,它会让事情变得非常糟糕。

只需使用:

<div class="form-group">

14
2017-10-10 17:14



如果你缩小窗口恰到好处,你的版本仍会显示文本输入在粉红色区域上突出显示... - Birgit Martinelle
是的,这是错误的链接: jsfiddle.net/fr6p90ar/6 他的榜样设置非常错误。 - Christina
再来一次: jsfiddle.net/fr6p90ar/6 - Christina
呸;无关紧要的 .form-horizontal 是我的整个问题。不敢相信我错过了。这不是没有阅读文档的问题;我没注意到这只是一个愚蠢的错误。抱歉;我最近一直在工作很长时间。关于 .form-group.col-md-4 问题:谢谢你的澄清,但同样,我 有 阅读文档。我很高兴从文档中可以看出这一点,但这不适合我 - 我在极端时间压力下第一次学习Bootstrap和响应式设计。谢谢你的帮助! - greenie2600
当你意识到99.99%的问题都是简单的错误并且你学会缩进,评论并保持非常干净的代码时,每个人都会创建它们但是你会减少。时间缝合可以节省九个等等。 col-X-4无论走到哪里都是33.333%,它总是增加排水沟。当某些东西是全宽度,嵌套在某个地方或不在某个地方时,你不需要任何列类。甚至很少需要col-X-12。 - Christina


这应该有帮助(来自boostrap doc: http://getbootstrap.com/css/#forms-horizo​​ntal 

使用Bootstrap的预定义网格类来对齐标签和组   通过将.form-horizo​​ntal添加到水平布局中来控制表单   形成。这样做会改变.form-groups以表现为网格行,因此不需要   为.row。

所以考虑到这一点你可以清理你的HTML很多 - 你也可以删除容器。这是你的表单的一点点清洁版的小提琴: http://jsfiddle.net/fr6p90ar/2/

<div class="col-md-4"  style="background-color: pink;">

                    <form role="form" class="form-horizontal">
                            <div class="form-group">
                                <label class="control-label" for="name">Name</label>
                                <input class="form-control" type="text" name="name" id="name"/>                    

                        </div>


                            <div class="form-group">
                                <label class="control-label" for="email">Email</label>
                                <input class="text form-control" type="email" name="email" id="email"/>                    
                            </div>


                        <button type="submit">Submit</button>

                    </form>

        </div> <!-- .col-md-4 -->

1
2017-10-10 17:02



拉引线的深层链接 getbootstrap.com/css/#forms-horizo​​ntal - subhaze
感谢deeplink - 让我编辑我的帖子,以反映那些没有阅读评论的人 - Birgit Martinelle
请阅读文档,这是一个堆叠的表单,没有类,也不需要.container,包括表单本身的类。 - Christina
我不是建议使用容器 - 这不是我的回答...... - Birgit Martinelle
他真的只需要删除表单上的类,而不是使用嵌套的.container和嵌套的.row - Christina