How to vertically center a text in a column (bootstrap 4)?

一笑奈何 提交于 2019-11-30 22:22:30

问题


How to vertically center a text in a column (bootstrap 4)? The structure is basic, see:

<div class="row">
    <div class="col-md-6">
        <span>Text to center</span>
    </div
</div>

Ty guys!


回答1:


You can add the my-auto class to the parent <div class="col-md-6"> to achieve this. This class sets automatic margins on the y-axis, see the link above for more details.

It would look like this:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  <div class="row">
    <div class="col-xs-6 my-auto">
      <span>Text to center</span>
    </div>
    <div class="col-xs-6">
      <h1>Hello</h1>
      <h1>Hello</h1>
      <h1>Hello</h1>
    </div>
  </div>
</div>

You might be tempted to use vertical alignment utilities but these will only work on inline, inline-block, inline-table, and table cell elements.




回答2:


Use auto-margins (my-auto), or use the flexbox utilities..

<div class="container">
    <div class="row">
        <div class="col-md-6 align-self-center">
            <span>Text to center</span>
        </div>
        <div class="col-md-6">
            <p>
            ..
            </p>
        </div>
    </div>
</div>

http://www.codeply.com/go/oy8H1bUOL1

Also, there are several other Bootstrap vertical centering options explained in my other answer.



来源:https://stackoverflow.com/questions/41840759/how-to-vertically-center-a-text-in-a-column-bootstrap-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!