如何在Bootstrap中将表单水平居中对齐在DIV中

6
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <form role="form">
                <div class="form-group">
                     <label for="exampleInputEmail1">Email address</label><input type="email" class="form-control" id="exampleInputEmail1" />
                </div>
                <div class="form-group">
                     <label for="exampleInputPassword1">Password</label><input type="password" class="form-control" id="exampleInputPassword1" />
                </div>
                <div class="form-group">
                     <label for="exampleInputFile">File input</label><input type="file" id="exampleInputFile" />
                    <p class="help-block">
                        Example block-level help text here.
                    </p>
                </div>
                <div class="checkbox">
                     <label><input type="checkbox" /> Check me out</label>
                </div> <button type="submit" class="btn btn-default">Submit</button>
            </form>
        </div>
    </div>
</div>

我正在使用bootstrap。

我想要将表单居中对齐在这个 div col-md-12 column 中。

有没有解决方案?


横向还是纵向? - DaniP
4个回答

6

演示:http://bootply.com/103569

可以使用偏移量,例如:

<div class="container">
<div class="row clearfix">
    <div class="col-md-6 col-md-offset-3 column">
        <form role="form">
            <div class="form-group">
                 <label for="exampleInputEmail1">Email address</label><input type="email" class="form-control" id="exampleInputEmail1" />
            </div>
            <div class="form-group">
                 <label for="exampleInputPassword1">Password</label><input type="password" class="form-control" id="exampleInputPassword1" />
            </div>
            <div class="form-group">
                 <label for="exampleInputFile">File input</label><input type="file" id="exampleInputFile" />
                <p class="help-block">
                    Example block-level help text here.
                </p>
            </div>
            <div class="checkbox">
                 <label><input type="checkbox" /> Check me out</label>
            </div> <button type="submit" class="btn btn-default">Submit</button>
        </form>
    </div>
</div>
<div class="row clearfix">
    <div class="col-md-12 column">
    </div>
</div>
</div>

或者
<div class="container">
<div class="row clearfix">
    <div class="col-md-12 column">
    <div class="col-md-6 col-md-offset-3 column">
        <form role="form">
            <div class="form-group">
                 <label for="exampleInputEmail1">Email address</label><input type="email" class="form-control" id="exampleInputEmail1" />
            </div>
            <div class="form-group">
                 <label for="exampleInputPassword1">Password</label><input type="password" class="form-control" id="exampleInputPassword1" />
            </div>
            <div class="form-group">
                 <label for="exampleInputFile">File input</label><input type="file" id="exampleInputFile" />
                <p class="help-block">
                    Example block-level help text here.
                </p>
            </div>
            <div class="checkbox">
                 <label><input type="checkbox" /> Check me out</label>
            </div> <button type="submit" class="btn btn-default">Submit</button>
        </form>
    </div>
    </div>
</div>
</div>

http://getbootstrap.com/css/#grid-offsetting


5

如果使用 display 属性,您可以有两种解决方案。

  • One with inline-block:

    .col-md-12 column {  
       text-align:center;
    }
    .col-md-12 column form {
       display:inline-block;
    }
    
  • Two with table :

    .col-md-12 column form {
       display:table;
       margin:auto;
    }
    

如果你的表单有固定的 width ,那么你只需要添加 margin:auto 即可。


1
给你的表单添加一个类。
<form role="form" class="myForm">

将以下样式添加到其中。
.myForm
{
    width: 480px;
    margin-left: auto;
    margin-right: auto;
}

http://bootply.com/103575


0

我会尝试添加

class="center"

<form role="form">

最终得到

<form role="form" class="center">

并创建样式声明:

.center{margin:0px auto;}

请记住,.center必须具有比其容器宽度小的设置宽度。

希望这可以帮到您。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接