[英]How to align bootstrap form at the center of the web page
I'm using bootstrap styling for form. 我正在使用引导样式作为表单。 I want this form to be displayed at the center of the page but I'm not able to do it even though I tried
margin 0 auto
in css . 我希望此表单显示在页面的中心,但是即使我尝试在CSS中使用
margin 0 auto
,也无法执行此操作。 Can anyone help. 谁能帮忙。
This is the code: 这是代码:
<!doctype html> <html> <body> <h2>Form</h2> <br> <form class="form-horizontal inputForm"> <div class="form-group"> <label class="control-label col-sm-2" for="name">Name  </label> <div class="col-sm-2"> <input class="form-control" type="text"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="email">Email  </label> <div class="col-sm-2"> <input class="form-control" type="email"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button class="btn btn-primary">Sign Up</button> </div> </div> </form> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="styles.css" rel="stylesheet"> </body> </html>
Screenshot of my browser displaying the above form: 我的浏览器的屏幕截图显示了以上形式:
If you don't like to center body
, put the form in a div and center that div. 如果您不想将
body
居中,请将表单放在div中并将该div居中。
body { text-align: center; border: 1px solid;} form { margin: auto; width: 200px; border: solid blue; display: block;}
<!doctype html> <html> <body> <h2>Form</h2> <br> <form class="form-horizontal inputForm"> <div class="form-group"> <label class="control-label col-sm-2" for="name">Name  </label> <div class="col-sm-2"> <input class="form-control" type="text"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="email">Email  </label> <div class="col-sm-2"> <input class="form-control" type="email"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button class="btn btn-primary">Sign Up</button> </div> </div> </form> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="styles.css" rel="stylesheet"> </body> </html>
Try this. 尝试这个。 I've wrapped the form with
我用
<div class="container">
<div class="row">
<div class="col-md-4 mx-auto"></div>
</div>
</div>
.mx-auto centers the column automatically and inside the column you can add your form that has .col-3 and col-9, in my example. .mx-auto将列自动居中,在本例中,您可以添加具有.col-3和col-9的表单。 But adjust it according to how you need the label and input to be shown.
但是请根据需要显示标签和输入的方式进行调整。
Here is the CodePen . 这是CodePen 。
Edit: I've realized you're using Bootstrap 3, not 4. In that case, you can try something like this, but it needs a bit of work on mobile. 编辑:我已经意识到您正在使用Bootstrap 3,而不是4。在这种情况下,您可以尝试这样的操作,但是它需要在移动设备上进行一些工作。 CodePen .
CodePen 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.