How to make Bootstrap cards the same height?
- User
How to make Bootstrap cards the same height?
- Assistant
How to make Bootstrap cards the same height?
To make Bootstrap cards the same height, you have several effective methods depending on your layout and Bootstrap version. Below are common approaches:
Bootstrap 4 and 5 use Flexbox, so you can easily make cards in a row have equal height by making their container a flex container.
Example:
<div class="d-flex">
<div class="card flex-fill m-2">
<div class="card-body">
<h5 class="card-title">Card 1</h5>
<p class="card-text">Short content</p>
</div>
</div>
<div class="card flex-fill m-2">
<div class="card-body">
<h5 class="card-title">Card 2</h5>
<p class="card-text">Some longer content that makes this card taller.</p>
</div>
</div>
<div class="card flex-fill m-2">
<div class="card-body">
<h5 class="card-title">Card 3</h5>
<p class="card-text">Medium content length.</p>
</div>
</div>
</div>
d-flex turns the container into a flexbox container.flex-fill makes each card expand equally to fill the row.h-100If you use Bootstrap's grid system, you can combine h-100 to make cards 100% height of their parent row.
Example:
<div class="row">
<div class="col-md-4 d-flex">
<div class="card h-100">
<div class="card-body">
<h5 class="card-title">Card 1</h5>
<p class="card-text">Short content</p>
</div>
</div>
</div>
<div class="col-md-4 d-flex">
<div class="card h-100">
<div class="card-body">
<h5 class="card-title">Card 2</h5>
<p class="card-text">Some longer content that makes this card taller.</p>
</div>
</div>
</div>
<div class="col-md-4 d-flex">
<div class="card h-100">
<div class="card-body">
<h5 class="card-title">Card 3</h5>
<p class="card-text">Medium content length.</p>
</div>
</div>
</div>
</div>
d-flex to the column so it can act as a flex container.h-100 to the card to stretch it to column height.If you prefer CSS Grid, you can make cards in a grid layout equal height:
<div class="card-grid d-grid" style="grid-template-columns: repeat(3, 1fr); gap: 1rem;">
<div class="card">
<div class="card-body">
Card 1 content
</div>
</div>
<div class="card">
<div class="card-body">
Card 2 content longer than Card 1
</div>
</div>
<div class="card">
<div class="card-body">
Card 3 content medium length
</div>
</div>
</div>
d-grid and grid-template-columns create three equal columns.If you must support older browsers and can't use flexbox or grid, you can use JS to set equal heights:
window.onload = function(){
const cards = document.querySelectorAll('.card');
let maxHeight = 0;
cards.forEach(card => {
card.style.height = 'auto'; // reset height
if(card.offsetHeight > maxHeight) {
maxHeight = card.offsetHeight;
}
});
cards.forEach(card => {
card.style.height = maxHeight + 'px';
});
};
d-flex and cards with flex-fill.d-flex and add h-100 to cards.If you specify your Bootstrap version or provide example HTML, I can tailor the solution even further!
Not yet included in a published release.