如何在bootstrap中div的中间水平对齐表单

时间:2014-01-02 20:41:13

标签: html css twitter-bootstrap

<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的中间对齐表单。

任何解决方案吗?

4 个答案:

答案 0 :(得分:5)

演示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

答案 1 :(得分:4)

您可以在此处使用属性display获得两个解决方案。

  • 一个inline-block

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

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

如果您对表单有固定的width,则只需添加margin:auto

答案 2 :(得分:1)

给你的表格一个班级

<form role="form" class="myForm">

并添加以下样式

.myForm
{
    width: 480px;
    margin-left: auto;
    margin-right: auto;
}

http://bootply.com/103575

答案 3 :(得分:0)

我会尝试添加

class="center"

<form role="form">

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

并创建样式声明:

.center{margin:0px auto;}

请注意,.center的设定宽度必须小于其容器的宽度。

希望有所帮助。