Full answer
In a flex row, items stretch to the height of the tallest item by default. The property align-items has the initial value stretch, so cards are already equal in height, as long as no one has set align-items to another value and the cards have no fixed height. If cards are not equal, check for align-items: flex-start or center, a fixed height on the card, or cards that sit in different rows because they wrapped.
Steps for a neat card row:
- Put the cards in a flex container with
display: flex; gap: 16px; flex-wrap: wrap;. - Give each card
flex: 1 1 200px. This means it can grow, can shrink, and starts at 200px. On a narrow screen the row wraps. - Make each card a flex column,
display: flex; flex-direction: column, so that its inner parts can be spread out. - Push the last part, such as a button, to the bottom with
margin-top: auto. Then the buttons line up at the bottom of all cards.
On the last wrapped row, a flex card grows to fill the row, so it may be wider than the rest. If you need equal widths on every row, use grid with repeat(auto-fill, minmax(200px, 1fr)) instead.
Do not set a fixed height on cards to make them equal. It cuts off content when the text grows, or when the user increases the font size.
All three cards have the same height, because they stretch. The buttons sit at the bottom of every card, on one line.
Check the cards with longer text and with a larger browser font size.
<style>
.row { display: flex; gap: 16px; width: 700px; }
.card { flex: 1 1 200px; display: flex; flex-direction: column; border: 1px solid #999; padding: 8px; font: 16px sans-serif; }
.card button { margin-top: auto; }
</style>
<div class="row">
<div class="card" id="a"><h3>Short</h3><p>One line.</p><button id="ba">Join</button></div>
<div class="card" id="b"><h3>Long text</h3><p>This card has much more text so that it takes many lines inside a card which is two hundred pixels wide.</p><button id="bb">Join</button></div>
<div class="card" id="c"><h3>Medium</h3><p>Two lines of text here.</p><button id="bc">Join</button></div>
</div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- align-items defaults to stretch.
- Fixed heights and other align-items values break equal heights.
- Card as a flex column, button with margin-top: auto.
- flex: 1 1 200px lets cards wrap.
- Grid gives equal widths on every row.
Common mistakes
- Setting a fixed height on cards.
- Setting align-items: flex-start and then wondering why heights differ.
- Forgetting margin-top: auto, so buttons sit at different heights.
Follow-ups
- What is the default value of align-items?
- How do you keep equal widths on the last row?
Tips
- Say that stretch is the default, and explain what can break it.
- Add margin-top: auto for the aligned buttons.