2

I'm trying to vertically (and horizontally) center a div (height and width unknown) in a Bootstrap column. I can horizontally center the div by setting text-align to center. However I am struggling to vertically center the div. Here is a snippet of my code:

<div class="row">
    <div class="col-md-8 col-md-offset-2 col-xs-12">
        <div style="text-align: center"> <!--this works-->
            <p>Content goes here</p>                
        </div>
    </div>
</div>

Thanks in advance!

Zanon
  • 29,231
  • 20
  • 113
  • 126
rmg.n3t
  • 951
  • 2
  • 10
  • 16
  • Also have a look at [Vertical align with bootstrap 3](http://stackoverflow.com/questions/20547819/vertical-align-with-bootstrap-3) – Hashem Qolami Sep 19 '14 at 21:35

2 Answers2

0

If you're using Twitter Bootstrap and you have the Bootstrap 3, they added <div class="center-block">...</div>.

Reference: Bootstrap v3.1.1.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
henrymason
  • 109
  • 5
-1

You may want to give this a shot:

<div class="row">
   <div class="col-md-8 col-md-offset-2 col-xs-12 Outer">
      <div class="Inner>
         <p >Content goes here</p>                
      </div>
   </div>
</div>


/* CSS Code */

.Outer {
   border: 1px solid black;  /* Just so you can see it centered vertically */
   display: table-cell;
   height: 100%;
   min-height: 15em;    /* Set to height you need */
}
.Inner > p {
   vertical-align: middle;
   text-align: center;
   margin: 0 auto;
}

Keep in mind that the browser will automatically recognize the resolution width; however, the same functionality doesn't apply to height. You need to set a specified height on the outer element in order for the inner content to have something to vertically align itself to. Hope this helps!

  • Hey thank you very much. However it doesn't work. The paragraph get centered horizontally but it still remains at the top vertically. – rmg.n3t May 27 '14 at 23:08