I am attempting to develop a grid of Bootstrap 4 cards with the following requirements:
class="row"
, because I don't know how many cards there will be total and I want rows to look good at different screen breakpoints.col-sm-6 col-lg-4
).I've managed to get almost all the way there, but I'm running into a problem: setting class="h-100"
on my cards to ensure that they are all the same height kills the margin from the bottom of each card.
Is there a way to ensure that all cards within any given displayed row are the same height, while preserving a margin at the bottom of them?
HTML Code :
<div class="container"> <div class="row"> <div class="col-md-4 col-sm-6"> <div class="card h-100 mb-4" style="background-color:#ff0000;"> Test card content. </div> </div> <div class="col-md-4 col-sm-6"> <div class="card h-100 mb-4" style="background-color:#00ff00;"> Test card content. Test card content. Test card content. Test card content. </div> </div> <div class="col-md-4 col-sm-6"> <div class="card h-100 mb-4" style="background-color:#0000ff;"> Test card content. Test card content. Test card content. Test card content. Test card content. Test card content. Test card content. Test card content. </div> </div> <div class="col-md-4 col-sm-6"> <div class="card h-100 mb-4" style="background-color:#0f0f0f;"> Test card content. </div> </div> </div> </div>
JSFiddle Demo
To get it to the same height, add Bootstrap class h-100 to all the cards or use CSS height.
They have no margin by default, so use spacing utilities as needed. Below is an example of a basic card with mixed content and a fixed width. Cards have no fixed width to start, so they'll naturally fill the full width of its parent element. This is easily customized with our various sizing options.
If you set the height = 100% the card will always expand to the greatest length of the longest card. The container will constrain all to the same length.
After a little bit of experimentation, this one was easy to solve: I needed to add the mb-4 class to the containing column, not the card itself:
<div class="container"> <div class="row"> <div class="col-md-4 col-sm-6 mb-4"> <div class="card h-100" style="background-color:#ff0000;"> Test card content. </div> </div> <div class="col-md-4 col-sm-6 mb-4"> <div class="card h-100" style="background-color:#00ff00;"> Test card content. Test card content. Test card content. Test card content. </div> </div> <div class="col-md-4 col-sm-6 mb-4"> <div class="card h-100" style="background-color:#0000ff;"> Test card content. Test card content. Test card content. Test card content. Test card content. Test card content. Test card content. Test card content. </div> </div> <div class="col-md-4 col-sm-6 mb-4"> <div class="card h-100" style="background-color:#0f0f0f;"> Test card content. </div> </div> </div> </div>
Hopefully this helps others out who are stuck in my same situation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With