Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning items within a Bootstrap 4 .card using Flexbox

I'm using Bootstrap 4, and have a div with the card class on it, which has a fixed height. This is what I'm trying to get:

Wireframe

The code I'm using is

<div class="card">
    <div class="card-block d-flex flex-column">
        <h2 class="card-title h4 align-self-start">Top Left</h2>
        <div class="align-self-center">
            Middle Middle
        </div>
        <aside class="align-self-end">Bottom Right</aside>
    </div>
</div>

but the lines are all squashed together, presumably because the card-block is not expanding to the full height of the card.

FAIL

Is there a way to get the card-block to fill the card, or otherwise get this to display correctly?

like image 597
John Y Avatar asked May 31 '26 02:05

John Y


1 Answers

You can use justify-content-between on the card-block..

<div class="card">
    <div class="card-block d-flex flex-column">
        <h2 class="card-title h6 align-self-start flex-grow d-flex">Top Left<br>Top Left</h2>
        <div class="align-self-center flex-grow d-flex">
            Middle Middle
        </div>
        <aside class="align-self-end flex-grow d-flex align-items-end">Bottom Right</aside>
    </div>
</div>

.flex-grow {
    flex: 1 0 0;
}

Demo: https://www.codeply.com/go/5dIgA0ul2q

Update 2018 Bootstrap 4 (stable) card-block is now card-body

like image 109
Zim Avatar answered Jun 02 '26 18:06

Zim