如何使用Bootstrap中心表单?

时间:2015-07-06 21:02:53

标签: html css twitter-bootstrap

我想将包含两个输入(用户名和密码)的表单居中:

<h2 align="center" >Authentication</h2>
<form method="post" action="/signin"  class="form-horizontal" role="form" align="center">
    <div class="form-group" align="center">
        <label class="control-label col-sm-2"  for="username">username<em>*</em></label>
        <div class="col-sm-6">
            <input type="text" name="username" id="username" placeholder="username" required="true" class="form-control"/>
        </div>
    </div>  
    <div class="form-group">
        <label class="control-label col-sm-2" for="password">password<em>*</em></label>
        <div class="col-sm-6">            
            <input type="password" name="password" id="password" required="true" class="form-control"/> 
        </div>
    </div>
    <div class="form-group"> 
        <div class="col-sm-offset-2 col-sm-8">  
            <input type="submit" name="signin" id="signin" value="sign in" class="btn btn-default"/>
         </div>
     </div>     
</form>

但是当我运行页面时,表单不在页面的中心。我在style.css中添加了:

.form-horizontal{
    display:block;
    margin-left:auto;
    margin-right:auto;
}

但这没效果。

4 个答案:

答案 0 :(得分:3)

要在页面上居中div,您需要设置容器的宽度,然后对其应用margin:0 auto;,它将在页面上左右居中。

.form-horizontal{
    display:block;
    width:50%;
    margin:0 auto;
}

如果您想将文本居中,只需将文本中心添加到文本所在的div类中.Bootstrap将完成剩下的工作。

修改1

<div class="contact-form">
    <h2 align="center" >Authentication</h2>
    <form method="post" action="/signin"  class="form-horizontal" role="form" align="center">
        <div class="form-group" align="center">
            <label class="control-label col-sm-2"  for="username">username<em>*</em></label>
            <div class="col-sm-6">
                <input type="text" name="username" id="username" placeholder="username" required="true" class="form-control"/>
            </div>
        </div>  
        <div class="form-group">
            <label class="control-label col-sm-2" for="password">password<em>*</em></label>
            <div class="col-sm-6">            
                <input type="password" name="password" id="password" required="true" class="form-control"/> 
            </div>
        </div>
        <div class="form-group"> 
            <div class="col-sm-offset-2 col-sm-8">  
                <input type="submit" name="signin" id="signin" value="sign in" class="btn btn-default"/>
            </div>
        </div>     
    </form>
</div>

答案 1 :(得分:1)

将整个表单包裹在<div class="container">your form HTML</div>

像这样:http://codepen.io/anon/pen/pJLrOq。代码中心将容器形成到站点的中间。编辑CSS并输入文本类以获得所需的布局。

答案 2 :(得分:1)

Slim Hmidi 你好,你和.form-horizontal的班级很近。

使用此功能将居中表单位于页面中间。

.center {
  position: absolute;
  left: 0;
  right: 0;
  margin: auto;  
}

您现在需要调整表单中的col-sm-xx以填充并在所有屏幕尺寸下正常工作。

这是一个截图,保存表单的蓝色div水平居中。

center form

以下是表单中的类已更改为填充并处理所有屏幕大小的表单。

enter image description here

&#13;
&#13;
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>How to center a form using Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<style>
body {
  padding-top: 50px;
}
.spacer {
  margin-top: 2%;
  margin-bottom: 2%;
}      
.block {
  height: 300px;
  padding-top: 15px;  
  background-color: darkorange;
}  
.block2 {
  min-height: 160px;
  padding-top: 15px; 
} 
.center {
  position: absolute;
/*  top: 0;
  bottom: 0; */
  left: 0;
  right: 0;
  margin: auto;  
}
</style>

</head>

<body>

    <nav class="navbar navbar-inverse navbar-fixed-top ">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand " href="#">Project name</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav navbar-right">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

<div class="container col-lg-12 spacer"></div>
    


    <div class="container col-lg-12 block">
        
        <div class="row col-xs-6 block2 bg-primary center">
            
            <form method="post" action="/signin"  class="form-horizontal" role="form" align="center">
            <div class="form-group" >
                <label class="control-label col-sm-3"  for="username">username<em>*</em></label>
                <div class="col-sm-8 col-xs-12">
                    <input type="text" name="username" id="username" placeholder="username" required="true" class="form-control"/>
                </div>
            </div>  
            <div class="form-group">
                <label class="control-label col-sm-3" for="password">password<em>*</em></label>
                <div class="col-sm-8 col-xs-12">            
                    <input type="password" name="password" id="password" required="true" class="form-control"/> 
                </div>
            </div>
            <div class="form-group"> 
                <div class="col-sm-offset-2 col-sm-8">  
                    <input type="submit" name="signin" id="signin" value="sign in" class="btn btn-default"/>
                </div>
            </div>     
        </form>
        </div>
            
    </div><!-- /.container -->


    <!-- Bootstrap core JavaScript -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    
</body>
</html>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您可以在css中添加text-align: center;