Course contentsShow
← Web Development
Lesson 296 of 2,9825. FlexboxFree lesson

Card Layouts

Build flexible card grids that wrap and distribute space evenly.

Card Layouts

You'll learn how to create flexible, evenly-spaced card grids that automatically wrap to new rows using Flexbox.

What Are Card Layouts?

Card layouts display groups of content "cards" (think product tiles, blog posts, or user profiles) in a grid-like arrangement. With Flexbox, you can make these cards distribute space evenly, wrap to multiple rows naturally, and adapt to different screen sizes—all without guessing exact widths.

The Flexbox Card Pattern

Enable Flexbox on the container and use flex-wrap: wrap to allow cards to flow to new lines. Each card gets a flex-basis value (like 300px) to suggest its preferred size, but you can combine this with flex-grow to fill remaining space or max-width to prevent cards from becoming too wide.

Typical setup:

.card-container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.card {
  flex: 1 1 300px;
  /* Grow and shrink allowed, start at 300px */
  max-width: 400px;
}

This means: "Make each card at least 300px wide, let it grow to fill space, but never wider than 400px."

Why This Works

Think of it like arranging books on shelves. Flexbox checks how many cards fit per row based on container width, wraps extras to the next line, and distributes leftover space evenly. The gap property adds consistent spacing without manual margins.

Key Takeaway: Use display: flex, flex-wrap: wrap, and flex: 1 1 [basis] on cards to create responsive, evenly-distributed card grids that adapt gracefully to any container width.