【问题标题】:Center Form using Twitter Bootstrap使用 Twitter Bootstrap 的中心表单
【发布时间】:2012-05-08 08:00:24
【问题描述】:

我正在使用 Twitter 的 Bootstrap 构建一个表单(由 django 提供)。我想让表单居中在页面上,但我遇到了一些问题。

这是我的 HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>My Form</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="/bootstrap/css/bootstrap.css" rel="stylesheet">
    <style>
      body {
        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
      }
    </style>
    <link href="/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">

    <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>

  <body>
    <div class="container">
        <div class="row">
            <div class="span12" style="text-align:center; margin: 0 auto;">
                <form class="form-horizontal" method="post" action="/form/">
                    <fieldset>
                        <legend>Please login</legend>
                        <div class="control-group">
                            <label class="control-label" for="id_username">Username</label>
                            <div class="controls">
                                <input name="username" maxlength="100" placeholder="Enter your username..." type="text" class="input-large" id="id_username" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="id_password">Password</label>
                            <div class="controls">
                                <input name="password" maxlength="100" placeholder="Enter your password..." type="password" class="input-large" id="id_password" />
                            </div>
                        </div>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>

    <!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="/bootstrap/js/jquery.js"></script>
    <script src="/bootstrap/js/bootstrap-transition.js"></script>
    <script src="/bootstrap/js/bootstrap-alert.js"></script>
    <script src="/bootstrap/js/bootstrap-modal.js"></script>
  </body>
</html>

我已经排除了一些额外的 django 逻辑({% csrf_token %}、一些 if 语句等),但这应该会重现问题。

实际的输入元素(用户名和密码文本框)像我预期的那样居中,但标签仍然位于页面的最左侧。我是否在某处遗漏了标记,还是必须覆盖一些控制标签 CSS?

【问题讨论】:

    标签: forms html css twitter-bootstrap


    【解决方案1】:

    您需要将表单包含在容器中,引导程序已经附带了一个容器类 width:940px,因此您可以将所有内容包装在里面,它会自动居中,如下所示:

    <div class="container">
        <div class="row">
            <div class="span12">
                <form class="form-horizontal" method="post" action="/form/">
                    <fieldset>
                        <legend>Please login</legend>
                        <div class="control-group">
                            <label class="control-label" for="id_username">Username</label>
                            <div class="controls">
                                <input name="username" maxlength="100" placeholder="Enter your username..." type="text" class="input-large" id="id_username" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="id_password">Password</label>
                            <div class="controls">
                                <input name="password" maxlength="100" placeholder="Enter your password..." type="password" class="input-large" id="id_password" />
                            </div>
                        </div>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>
    

    【讨论】:

    • 我忘了提到我已经将所有东西都包装在“容器”类中,而且我仍然得到相同的结果。我只是没有将它包含在我的(修改后的)示例中。我已经编辑了我的原始评论以反映这一点(我尝试将我拥有的所有内容复制/粘贴到测试页面中,但它仍然没有居中)。
    • @Nitzle 是的,因为控制组浮动到左侧,您只需降低 span 标记容器宽度并将其居中即可轻松获得所需的结果,例如这个:jsfiddle.net/PSMwt/1
    • 这似乎确实有效,但是我也通过添加 text-align:center;边距:0 自动;到 "span12" 包装器,并添加 "width: 400px; margin: 0 auto; 到表单标签,像这样:jsfiddle.net/uvNVY
    • @Nitzle 对,它的方法几乎相同,span6 类的宽度是460px
    • 由于此表单居中,我遇到了另一个问题。我已经包含了一个复选框,并且该复选框现在似乎将自身定位得太靠近“标签”,如下所示:jsfiddle.net/VuaYz/2 有什么想法吗?
    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多