Bootstrap - 如何将包含文本的div居中?

时间:2014-10-30 22:33:17

标签: html css twitter-bootstrap

我希望能够将包含下面文本的div置于页面中间。我虽然可以在bootstrap中使用'center-block'类,但它似乎不起作用:

实时预览: http://codepen.io/anon/pen/tCIlG

Code below:

 <div class="row">
      <div class="col-md-4 center-block text-center">

        I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.

      </div>
    </div>

2 个答案:

答案 0 :(得分:2)

.col-md-4类导致它向左浮动而不是向中心浮动。如果要保持该类的宽度,可以在CSS中覆盖它:

.col-md-4.center-block {
    float: none;
}

更新了codepen:http://codepen.io/sdsanders/pen/lHrvu

答案 1 :(得分:2)

你去吧

<div class="row">
      <div class="col-md-4 col-md-offset-4">

        I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.I want to center the div that this text is in.

      </div>
    </div>