【问题标题】:Bootstrap 3: How are form-groups meant to be used inside of columns?Bootstrap 3:表单组如何在列内使用?
【发布时间】:2016-08-19 19:40:36
【问题描述】:

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

每个表单字段依次包装在 .form-group 中。

my first attempt 中,.form-groups 溢出到包含列的左右边缘。 (确保 JSFiddle 预览框架至少与 Bootstrap 的 sm 断点一样宽。)我添加了一个粉红色背景 div 以显示 .form-groups 应该留在其中的框。

my second attempt 中,我在.col-md-4 内部添加了一个.container,然后将每个.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>

【问题讨论】:

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

标签: css twitter-bootstrap twitter-bootstrap-3


【解决方案1】:

之前的两个答案都没有阅读 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">

【讨论】:

  • 如果您将窗口缩放得恰到好处,您的版本仍会显示文本输入突出在粉红色区域...
  • 是的,这是错误的链接:jsfiddle.net/fr6p90ar/6 他的示例设置非常错误。
  • 呸;无关的.form-horizontal 是我的全部问题。不敢相信我错过了。这不是没有阅读文档的问题。这只是一个我没有注意到的愚蠢错误。对不起;我最近工作了很长时间。关于.form-group.col-md-4 问题:感谢您的澄清,但我再次已经阅读了文档。我很高兴从文档中这一点对你来说是显而易见的,但这不适合我——我是在极大的时间压力下第一次学习 Bootstrap 和响应式设计。不过,感谢您的帮助!
  • 每个人都会做,但当你意识到 99.99% 的问题都是简单的错误并且你学会了缩进、注释和保持真正干净的代码时,你做的就更少了。及时缝合节省了九个和所有这些。 col-X-4 无论走到哪里都是 33.333%,而且总是增加排水沟。当某些东西是全宽的,是否嵌套在某个地方时,您不需要任何列类。甚至很少需要 col-X-12。
【解决方案2】:

这应该会有所帮助(来自 boostrap 文档:http://getbootstrap.com/css/#forms-horizontal

使用 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 -->

【讨论】:

  • 感谢深层链接 - 让我编辑我的帖子以反映那些不阅读 cmets 的人
  • 请阅读文档,这是一个堆叠的表单,没有类也不需要 .container,包括表单本身的类。
  • 他真的只需要去掉表单上的类,不要使用嵌套的.container和嵌套的.row
  • 您的示例仍然在表单上留下“form-horizo​​ntal”类,这是一个垂直表单。
  • 听着,我知道你很了解 bootstrap,我对此很满意。据我所知,没有“表格垂直”之类的东西 - 有表格内联。如果您想争辩说,仅仅因为表格没有占用完整的 12 列并且是嵌套的 - 并且这不会使其成为水平表格,那么我选择不同意。我知道如何阅读,我在文档中没有看到这样的定义:form-horizo​​ntal 不能包含在父 cols 中。所以 - 不要扔石头和一遍又一遍地编辑你的答案,只需接受其他人也可以解决问题。
猜你喜欢
  • 2020-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 2014-01-28
  • 2013-08-24
相关资源
最近更新 更多