0

I'm trying to center my text in a div (vertically and horizontally), but I am using the bootstrap3 grid system so I can't seem to find an applicable answer. Most answers that I found require a table.

Here is my markup:

<div class="row">

    <div class="col-xs-4">
        This is the text that I want to center...
    </div>


    <div class="col-xs-4">
        This is the text that I want to center...
    </div>


    <div class="col-xs-4">
        This is the text that I want to center...
    </div>

</div>
Rhs
  • 3,037
  • 10
  • 42
  • 78

3 Answers3

1
<div class="row">

    <div class="col-xs-4 text-center" style="vertical-align: middle;">
        This is the text that I want to center...
    </div>


    <div class="col-xs-4 text-center" style="vertical-align: middle;">
        This is the text that I want to center...
    </div>


    <div class="col-xs-4 text-center" style="vertical-align: middle;">
        This is the text that I want to center...
    </div>

</div>
Will
  • 2,570
  • 16
  • 22
0
 <div class="row">

    <div class="col-xs-4" style="text-align:center; vertical-align:middle">
        This is the text that I want to center...
    </div>


    <div class="col-xs-4" style="text-align:center; vertical-align:middle">
        This is the text that I want to center...
    </div>


    <div class="col-xs-4" style="text-align:center; vertical-align:middle">
        This is the text that I want to center...
    </div>

</div>

It is better if you create CSS class- like

   .cAlign{
        text-align:center;
vertical-align:middle
    }
<div class="row">

    <div class="col-xs-4 cAlign">
        This is the text that I want to center...
    </div>


    <div class="col-xs-4 cAlign">
        This is the text that I want to center...
    </div>


    <div class="col-xs-4 cAlign">
        This is the text that I want to center...
    </div>

</div>
Dawood Awan
  • 6,957
  • 10
  • 50
  • 115
0

In 2013, you should consider using the absolute centering method.

Add to your CSS:

.hv-center-wrap {
  position: relative;
}

.hv-center {
  position: absolute;
  overflow: auto;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 400px;
  height: 500px; /* height must be set to work, percentages will work */
}

Add class .hv-center to any element that you want both horizontal and vertical centering. Then wrap the element around an outer div .hv-center-wrap.

(vertical-align: middle will not center the text vertically. That only works for specific elements, not divs.)

Michelle
  • 2,712
  • 2
  • 22
  • 35