3

I have a task to make a one page form, it only consist two input form and a save button.

I make it in a col-md-9 align center. but the form only horizontally centered, i want to make it vertically centered too.

<div class="col-md-9 no-float" align="center">
   <form>
         FIRST INPUT HERE
         SECOND INPUT HERE

         BUTTON
   </form>
</div>

and

.col-md-9
        {
            display: table-cell;
            width: 75%;
            height:100vh;
        }
.col-md-9.no-float {
            float: none; 
        }
JON PANTAU
  • 395
  • 4
  • 14
  • Refer this http://stackoverflow.com/questions/8865458/how-to-align-text-vertically-center-in-div-with-css – Venkat.R Jan 02 '16 at 06:35

4 Answers4

0

for horizontal center use margin: 0px auto 0px auto;

and for vertical center vertical-align: center;

if this dost work use padding on top and remove padding from bottom

0

If the form won't change anymore, meaning the height won't change, you don't really need a dynamic solution. Just fiddle around with margin/padding-top in vh units. Make sure it doesn't have 100vh as a height when you do this.

For example:

.col-md-9 {
   height: auto;
   margin-top: 20vh; // or padding
}

// add mediaqueries for more specific stuff

It's not perfect but it works OK.

Otherwise you could do it dynamically with jQuery.

var browserHeight = $(window).height(),
    colHeight     = $('.col-md-9').outerHeight(),
    columnMargin  = (browserHeight / 2) - (colHeight / 2);

    // set margin-top for column        
    $('.col-md-9').css('margin-top', columnMargin);
Jabba Da Hoot
  • 794
  • 2
  • 7
  • 16
0

You can use calc if you know the width and height of your element. In your case, if your form was 200px wide and 30px high, you can set a margin-top: calc(50% - 15px) and margin-left: calc(50% - 100px). In this demo, I used vh and vw instead of percentages. If you want your form elements stacked on top of each other you can place a <br/> at the end of each element as I did in the demo, or you can assign display: block; to each form element.

Note: align attribute is obsolete in HTML5 which I assume your'e using if your'e using Bootstrap.

.col-md-9 {
  width: 100vw;
  height: 100vh;
}
form {
  height: 30px;
  margin-top: calc(50vh - 15px);
  width: 200px;
  margin-left: calc(50vw - 100px);
}
<section class="col-md-9">
  <form>
    <input/>
    <br/>
    <input/>
    <br/>
    <button>Button</button>
  </form>
</section>
zer00ne
  • 41,936
  • 6
  • 41
  • 68
0

This works without having to know the size of the form. 500px is for demo purposes only. Change this to 100% for a normal site.

.container-main {
    display: table;
    width: 100%;
    height: 500px;
}
.container-center {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
.container-center span {
  display: inline-block;
}
<div class="container-main">
 <div class="container-center">
  <span>
   <form>
    <input/>
    <br/>
    <input/>
    <br/>
    <button>Button</button>
   </form>
  </span>
 </div>
</div>
Steve Harris
  • 5,014
  • 1
  • 10
  • 25