Lesson 7 of 10
Card Layouts with Flexbox
Card layouts are the most common UI pattern on the web. Use Flexbox to create equal-height cards, responsive card grids, and cards with footers that always stay at the bottom - regardless of content length.
Card Layout Techniques
- Equal-height cards - Flex container stretches all cards to the same height automatically (align-items: stretch is default)
- Footer at bottom - Use flex-direction: column on the card, flex: 1 on the body pushes footer down
- Responsive wrapping - flex-wrap: wrap with flex: 1 1 300px creates auto-responsive grids
- Image + content - Use flex-direction: column for vertical cards, row for horizontal cards
- Card grid gap - Use gap: 20px on the container (no margin hacks needed)
- Limit cards per row - flex: 0 0 calc(33.333% - 16px) for exactly 3 per row
Card Layout Code
/* Responsive card grid */
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
/* Each card is a vertical flex container */
.card {
flex: 1 1 300px; /* Grow, shrink, min 300px */
display: flex;
flex-direction: column;
background: white;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
.card img {
width: 100%;
height: 200px;
object-fit: cover;
}
.card-body {
flex: 1; /* Takes remaining space, pushes footer down */
padding: 20px;
}
.card-footer {
padding: 12px 20px;
border-top: 1px solid #eee;
/* Always at bottom regardless of content length */
}Tip
The 'sticky card footer' pattern (flex-direction: column on the card + flex: 1 on the card body) is one of the most elegant Flexbox solutions. Without it, short-content cards have their buttons floating halfway up - a major design inconsistency in card grids.
Flexbox cards maintain equal heights with headers, expandable bodies, and pinned footers
Common Mistake
Setting a fixed height on the card grid container. Flex item heights are dynamic - they stretch to match the tallest sibling. Setting a fixed height prevents this and breaks the equal-height behavior. Let cards be height: auto and use the flex-direction: column + flex: 1 trick on the card body instead.
Practice Task
Build a card grid where all card footers align: (1) .card-grid with display: flex; flex-wrap: wrap; gap: 20px, (2) .card with display: flex; flex-direction: column; flex: 1 1 280px, (3) .card-body with flex: 1. Add different amounts of text to each card and verify footers align at the bottom.
Quick Quiz
Key Takeaways
- Card layouts are the most common UI pattern on the web.
- Equal-height cards - Flex container stretches all cards to the same height automatically (align-items: stretch is default)
- Footer at bottom - Use flex-direction: column on the card, flex: 1 on the body pushes footer down
- Responsive wrapping - flex-wrap: wrap with flex: 1 1 300px creates auto-responsive grids
Finished this lesson?