Code Reference

Card Layout

CSS · Example / how-to

Card Layout

CSS · Example / how-to


📋 Overview

Lay out a responsive card grid with CSS Grid, consistent gaps, and a flexible media + body structure inside each card.

🔧 Core concepts

PieceRole
grid-template-columnsAuto-fit columns
minmaxFluid card width
Flex column cardMedia on top, body grows
Aspect ratioStable image boxes

💡 Examples

<section class="card-grid">
  <article class="card">
    <img class="card-media" src="/img/a.jpg" alt="" />
    <div class="card-body">
      <h2>Title A</h2>
      <p>Short description.</p>
      <a href="/a">Read more</a>
    </div>
  </article>
  <article class="card">
    <img class="card-media" src="/img/b.jpg" alt="" />
    <div class="card-body">
      <h2>Title B</h2>
      <p>Short description.</p>
      <a href="/b">Read more</a>
    </div>
  </article>
</section>
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
  gap: 1.25rem;
}

.card {
  display: flex;
  flex-direction: column;
  border: 1px solid #ddd;
  border-radius: 0.5rem;
  overflow: hidden;
  background: #fff;
}

.card-media {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  display: block;
}

.card-body {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 1rem;
  flex: 1;
}

.card-body a {
  margin-top: auto;
}

⚠️ Pitfalls

  • Fixed pixel columns break on small phones — prefer minmax + auto-fit.
  • Missing alt on meaningful images hurts accessibility; decorative images use alt="".
  • Uneven text lengths: push CTAs with margin-top: auto in a flex column.

On this page