我使用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?
解决方法
您需要将表单包含在容器中,引导程序已经带有一个容器类,宽度为: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>