Bootstrap 4 vertical center - equal height cards

2019-07-03 02:15发布

I'm using Bootstrap 4 alpha 6, and attempting to use flexbox to vertically center the content of equal height cards. I've used the h-100 util class to make the cards full height with the columns..

The problem is that I want the content of each card to be aligned in the middle (centered vertically). I tried the use the .align-items-center class in the row, which works to center the cards, but then the cards are no longer equal height...

enter image description here

HTML

<div class="container">
    <div class="row align-items-center bg-faded">
        <div class="col-md-2">
            <div class="card card-block h-100">
                I have a lot of content that wraps on multiple lines..
            </div>
        </div>
        <div class="col-md-6">
            <div class="card card-block h-100">
               I have a line of content.<br>
               And another line here..
            </div>
        </div>
        <div class="col-md-4">
            <div class="card card-block h-100">
                I have a little bit.
            </div>
        </div>
    </div>
</div>

Demo of problem

2条回答
叛逆
2楼-- · 2019-07-03 02:21

One way I've found to solve the centering issue is using the flex-column utility class. Since each card is display: flex, flex-column can be used to set flex-direction: column. Then, justify-content-center vertically aligns the content...

<div class="container">
    <div class="row">
        <div class="col-md-2">
            <div class="card card-block h-100 flex-column justify-content-center">
                I have a lot of content that wraps on multiple lines..
            </div>
        </div>
        <div class="col-md-6">
            <div class="card card-block h-100 flex-column justify-content-center">
               I have a line of content.<br>
               And another line here..
            </div>
        </div>
        <div class="col-md-4">
            <div class="card card-block h-100 flex-column justify-content-center">
                I have a little bit.
            </div>
        </div>
    </div>
</div>

Demo

查看更多
倾城 Initia
3楼-- · 2019-07-03 02:39

Here is a simpler solution for you:

using only justify-content-center utility class because .card has already flex-direction: column property

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
    <code>normal .row with .card inside .col-*</code>
    <h6>Problem is that card content is not centered</h6>
    <div class="row bg-faded">
        <div class="col-md-2">
            <div class="card card-block h-100 justify-content-center align-items-center">
                I have a lot of content that wraps on multiple lines..
            </div>
        </div>
        <div class="col-md-6">
            <div class="card card-block h-100 justify-content-center align-items-center">
               I have a line of content.<br>
               And another line here..
            </div>
        </div>
        <div class="col-md-4">
            <div class="card card-block h-100 justify-content-center align-items-center">
                I have a little bit.
            </div>
        </div>
    </div>
</div>
<hr>
<div class="container">
    <code>.align-items-center.row with .card inside .col-</code>
    <h6>Problem is that cards are no longer full height</h6>
   <div class="row bg-faded">
        <div class="col-md-2">
            <div class="card card-block h-100 justify-content-center">
                I have a lot of content that wraps on multiple lines..
            </div>
        </div>
        <div class="col-md-6">
            <div class="card card-block h-100 justify-content-center">
               I have a line of content.<br>
               And another line here..
            </div>
        </div>
        <div class="col-md-4">
            <div class="card card-block h-100 justify-content-center">
                I have a little bit.
            </div>
        </div>
    </div>
</div>

查看更多
登录 后发表回答