0

I have a div with an inline-block display so that it wrapps nicely its content. What I am trying to do is to center that div horizontally and vertically using the different technics I already used, the one I read on posts, but nothing working. Hope someone can help. Thank you in advance for your replies. Cheers. Marc..

http://jsfiddle.net/DfDLF/

my html:

<div>
  <input type='text'>
  <input type='text'>
  <input type="submit">
</div> 

my css:

div{
display:inline-block;
padding:5px;
background:grey;
top:50%;
margin-left:auto;
margin-right:auto;}
Marc
  • 9,217
  • 21
  • 67
  • 90

4 Answers4

3
div {

    height: 100px; /* specify width */
    width: 400px; /* specify height */

    position: absolute; /* position to absolute */

    top: 50%; /* push top 50% */
    left: 50%; /* push left 50% */

    margin-top: -50px; /* set margin top to minus half height */
    margin-left: -200px; /* set margin left to minus half width */

    /* your styles.. */
    padding: 5px;
    background: grey;
} 

Set your CSS to this.

2

you cannot center Inline-Block using margin:auto because of its inline behaviour. So you can just use the following body{text-align:center;} and your div will center nicely

Boris D. Teoharov
  • 2,319
  • 4
  • 30
  • 49
1

CSS

#vertical_align {
    height: 800px; /* need */
    width:100%;
    display: table;
    border:1px solid;
    #position: relative;
    overflow: hidden;
}
#vertical_align .container {
    #position: absolute;
    #top: 50%;
    display: table-cell;
    vertical-align: middle;
}
#vertical_align .container .wrap {
    #position: relative;
    width: 100%;
    border:1px solid;
    #top: -50%;
}

HTML

<div id="vertical_align">
   <div class="container">
      <div class="wrap">
          <center>
              Your content
          </center>
      </div>
   </div>
</div>
m1k1o
  • 2,344
  • 16
  • 27
0

Here is one option: http://jsfiddle.net/DfDLF/24/

Steve
  • 340
  • 4
  • 16