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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20891481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:18:10  来源:igfitidea点击:

How to align the form horizontally in the middle of the div in bootstrap

htmlcsstwitter-bootstrap

提问by user3155047

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

I am using bootstrap.

我正在使用引导程序。

And I want to align the form in the middle of this div col-md-12 column.

我想在这个中间对齐表格div col-md-12 column

Any solutions for this?

对此有任何解决方案吗?

采纳答案by DaniP

You can have two solutions here with the property display.

您可以在这里使用属性有两个解决方案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;
    }
    
  • 一个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;
    }
    

If you have a fixed widthfor the form you only need to add the margin:auto

如果你有一个固定width的表单,你只需要添加margin:auto

回答by Purple Hexagon

Demo http://bootply.com/103569

演示http://bootply.com/103569

Could use an offset like:

可以使用像这样的偏移量:

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

or

或者

<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

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

回答by Ahm3d Said

Give your form a class

给你的表格上课

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

and add the following style to it

并为其添加以下样式

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

http://bootply.com/103575

http://bootply.com/103575

回答by jamesJosephFinn

I would try adding

我会尝试添加

class="center"

to

<form role="form">

ending up with

结束于

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

and creating the style declaration:

并创建样式声明:

.center{margin:0px auto;}

keeping in mind that .centermust have a set width that is less than the width of it's container.

请记住,它.center的设置宽度必须小于其容器的宽度。

Hope that helps.

希望有帮助。