Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align the form horizontally in the middle of the div in 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>

I am using bootstrap.

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

Any solutions for this?

like image 549
user3155047 Avatar asked Jan 02 '14 20:01

user3155047


3 Answers

Demo 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

like image 186
Purple Hexagon Avatar answered Oct 06 '22 14:10

Purple Hexagon


You can have two solutions here with the property 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;
    }
    

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

like image 29
DaniP Avatar answered Oct 06 '22 15:10

DaniP


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

like image 1
Ahm3d Said Avatar answered Oct 06 '22 15:10

Ahm3d Said