简体   繁体   English

Bootstrap Modal中的中心形式

[英]center form in Bootstrap Modal

My Question is, how to center the complete form ( input fields with labels ) in the modal. 我的问题是,如何在模式中将完整的表格( 带有标签的输入字段 )居中。

Here is also the fiddle: http://jsfiddle.net/beernd/reh0ookq/ but the snippet here should also work. 这也是小提琴: http : //jsfiddle.net/beernd/reh0ookq/,但是这里的代码段也应该起作用。

 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form name="useradd" class="form-horizontal" role="form" method="post"> <div class="modal show" id="useradd_modal"> <div class="modal-dialog modal-lg"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> <h4 class="modal-title text-center text-danger">Adding a user</h4> </div><!-- /.modal-header --> <div class="modal-body"> <div class="form-group"> <label class="col-sm-2 col-md-2 control-label">Login-Name:</label> <div class="col-sm-4 col-md-4"> <input class="form-control" type="text" placeholder="Login-Name" value="" name="ua_loginname" required="required" /> </div> </div> <div class="form-group"> <label class="col-sm-2 col-md-2 control-label">Firstname:</label> <div class="col-sm-3 col-md-3"> <input class="form-control" type="text" placeholder="Firstname" value="" name="ua_fname" required="required"/> </div> </div> </div><!-- /.modal-body --> <div class="modal-footer"> <input class="btn btn-md btn-danger pull-left" data-dismiss="modal" type="submit" value="User add"/> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div><!-- /.modal-footer --> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal --> </form> 

I've tried many other solutions like adding style="text-align: center;" 我尝试了许多其他解决方案,例如添加style="text-align: center;" and adding to the inputs style="margin 0 auto;" 并在输入中添加style="margin 0 auto;" Like this 像这样

or added an id with display: inline-block; 或添加带有display: inline-block; and class with text-align: center but all other solutions didn't worked for me. 并使用text-align: center上课text-align: center但所有其他解决方案均不适用于我。

Use: 使用:

class="text-center"

See: [1]: http://jsfiddle.net/reh0ookq/1/ 参见: [1]:http://jsfiddle.net/reh0ookq/1/

I have used it on the entire form, as well as one input 我已经在整个表格以及一个输入中使用了它

The above only works if you want just text/labels 以上仅适用于仅需要文本/标签的情况

The below will fix the entire input. 下面将修复整个输入。

jsfiddle jsfiddle

The issue is the bootstrap columns system. 问题是引导列系统。 If you want a column to be centered, you can give it the class "center-block" and then extend the column to be total for that row (12 is the max). 如果要使列居中,则可以为其指定“中心块”类,然后将该列扩展为该行的总数(最大值为12)。 If you want the label to appear next to the input in full screen, you will have to nest another row inside the center-block column div and handle further columns inside that. 如果希望标签全屏显示在输入旁边,则必须将另一行嵌套在中心块列div中,并在其中进一步处理列。

If you're looking to center the actual inputs, you need some offsets. 如果要使实际输入居中,则需要一些偏移量。 There isn't a col-*-* class that makes it appear in the center of the row, but you can use offsets as such: 没有col-*-*类使它出现在行的中心,但是您可以这样使用偏移量:

Updated Bootply with Labels 使用标签更新了Bootply

<div class="container">
  <div class="panel panel-info">
    <div class="panel-heading">
      <h4>Pretend this is a Modal</h4>
    </div>
    <div class="panel-body">
      <div class="container-fluid">
        <div class="row">
          <div class="col-xs-2 col-xs-offset-2">
            <label>Login-Name</label>
          </div>
          <div class="col-xs-4">
            <input class="form-control" name="test1" placeholder="Login-Name" type="text">
          </div>
        </div>
        <div class="row">
          <div class="col-xs-2 col-xs-offset-2">
            <label>First Name</label>
          </div>
          <div class="col-xs-4">
            <input class="form-control" name="test2" placeholder="Firstname" type="text">
          </div>
        </div>
        <hr>
        <div class="row">
          <div class="col-xs-2">
            <label>Login-Name</label>
          </div>
          <div class="col-xs-10">
            <input class="form-control" name="test1" placeholder="Login-Name" type="text">
          </div>
        </div>
        <div class="row">
          <div class="col-xs-2">
            <label>First Name</label>
          </div>
          <div class="col-xs-10">
            <input class="form-control" name="test2" placeholder="Firstname" type="text">
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

EDIT Labels added, 2 layouts proposed. 添加了EDIT标签,建议了2种布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM