Html 使用 Twitter Bootstrap 的中心表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10352866/
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:00:57  来源:igfitidea点击:

Center Form using Twitter Bootstrap

formshtmlcsstwitter-bootstrap

提问by Nitzle

I am using Twitter's Bootstrap to build a form (served by django). I want to have the form centered on the page but I'm running into some problems.

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

Here is my HTML:

这是我的 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>

I've excluded some additional django logic ({% csrf_token %}, some if statements, etc), but this should recreate the problem.

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

The actual input elements (username and password textboxes) are centering like I expected, but the labels remain to the far left of the page. Am I missing markup somewhere or do I have to override some of the control-label CSS?

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

回答by Andres Ilich

You need to include your form inside a container, the bootstrap already comes with a container class that is width:940pxso you can wrap everything inside and it'll center automatically, like so:

您需要将表单包含在容器中,引导程序已经附带了一个容器类,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>