A practical CSS Grid pattern for wrapping dashboard cards

A practical CSS Grid pattern for wrapping dashboard cards

●3 ●56
calendar_today • schedule2 min read
— Originally published at chriseugenerodriguez.com

A dashboard card layout needs a different response from a table. Cards are usually independent pieces of information, so they can move to another row when there is not enough horizontal space. The goal is to reduce the number of columns before each card becomes too narrow to scan.

Prerequisites: a container that holds repeated card elements, a chosen minimum readable width for one card, and CSS Grid support. Start by deciding what “readable” means for the actual card content. In this example, that floor is 180px, but a card with longer labels, controls, or localized text may need more.

The implementation decision is to use a repeating Grid column definition that combines auto-fit with a minimum-to-flexible track range. auto-fit lets Grid calculate how many minimum-width tracks fit in the container. The minimum protects the card from compression. The flexible maximum lets the tracks that do fit divide any remaining space evenly.

The expected result is a layout that changes its track count naturally. Six cards can appear in four columns on a wide container, then two columns when the available inline size is smaller, and finally one column once a second minimum-width track plus the gap no longer fits. The DOM does not need to change, and the transition follows the actual grid math rather than a hand-picked screen breakpoint.

One limitation is important: this is an outer-grid solution only. It cannot fix card contents that refuse to shrink, such as long unbroken text or oversized embedded controls. Handle those inside the card separately. Also choose another layout when a strict matrix, table-style comparison, or guaranteed cross-row column alignment is a product requirement.

Working example

export const MIN_CARD_WIDTH = 180;

const defaultCards = [
  'Revenue',
  'Active users',
  'Open tickets',
  'Conversion',
  'Deployments',
  'Uptime',
];

function escapeHtml(value) {
  return String(value)
    .replaceAll('&', '&')
    .replaceAll('<', '&lt;')
    .replaceAll('>', '&gt;')
    .replaceAll('"', '&quot;')
    .replaceAll("'", '&#39;');
}

export function dashboardHtml(cards = defaultCards) {
  const cardMarkup = cards
    .map(
      (title, index) => `
        <article class="card" data-card="${index}">
          <h2>${escapeHtml(title)}</h2>
          <p>Dashboard metric ${index + 1}</p>
        </article>`,
    )
    .join('');

  return `<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
      * { box-sizing: border-box; }
      html, body { margin: 0; }
      body {
        font-family: system-ui, sans-serif;
        background: #e2e8f0;
      }
      .dashboard {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(${MIN_CARD_WIDTH}px, 1fr));
        gap: 16px;
        width: 100%;
      }
      .card {
        min-height: 72px;
        padding: 12px;
        border: 1px solid #94a3b8;
        border-radius: 8px;
        background: white;
      }
      h2 { margin: 0; font: 600 16px system-ui, sans-serif; }
      p { margin: 8px 0 0; font: 14px system-ui, sans-serif; }
    </style>
  </head>
  <body>
    <main class="dashboard" aria-label="Dashboard cards">${cardMarkup}</main>
  </body>
</html>`;
}

Read the full article: https://chriseugenerodriguez.com/blog/grid-auto-fit-minmax-dashboard

Want more #css deep dives?

Join 6,134+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

Tailwind CSS for Beginners: From Zero to Your First Component

muhammadfarhan.dev - Jul 21

Dashboard Operasional Armada Rental Mobil dengan Python + FastAPI

Masbadar - Mar 12

Layout Grids in CSS: Designing with Millimeter Precision Before Writing Code

Joemetry - Sep 22

Common CSS Mistakes Beginners Make (and How to Fix Them)

muhammadfarhan.dev - Aug 25

Python for commerce students, from a former bio kid

rahuldk1105 - Oct 30
chevron_left
1.2k Points • 59 Badges
Santa Monica, CA • chriseugenerodriguez.com
44Posts
0Comments
11Connections
10 years fe

Related Jobs

View all jobs →

Commenters (This Week)

4 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!