Bootstrap 4垂直对齐文本不会以卡片为中心

tea*_*low 7 css flexbox twitter-bootstrap

尝试垂直对齐以下卡片中的文本中心:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">

<div class="row text-center h-100">
  <div class="col-md-3 text-center my-auto">
    <div class="card card-block justify-content-center" style="height: 120px">
      <div class="card-body">
        This is some text within a card body.
      </div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我完全了解这里有许多类似的问题,但是我尝试了各种解决方案,但它们似乎都不适用于我的卡片.

我在父母,卡片和卡片身上尝试了"my-auto".我尝试了"d-flex align-items-center h-100"和"justify-content-center".还试图玩显示属性.我错过了什么?

Nar*_*ali 28

这是一个使用bootstrap 4文档的解决方案flexbox.在这里阅读更多

你需要知道什么,

d-flex - >使元素成为弹性框.

align-items-center - 垂直对齐内容.

justify-content-center - 水平对齐内容.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">

<div class="row text-center h-100">
  <div class="col-md-3 text-center my-auto">
    <div class="card card-block d-flex" style="height: 120px">
      <div class="card-body align-items-center d-flex justify-content-center">
        This is some text within a card body.
      </div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 真好 我错过了卡体内的d-flex,以前只有在类卡中才有。 (2认同)