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('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"')
.replaceAll("'", ''');
}
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