The Ultimate 2-Dimensional Layout System: Columns, Rows, Areas & Responsive Patterns
Every web developer's favorite trick: repeat(auto-fit, minmax(160px, 1fr)) creates a completely responsive grid of cards that wraps automatically at any screen size!
.magic-grid {
display: grid;
/* Automatically creates as many columns as fit (min 160px, max 1fr) */
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
gap: 16px;
}
auto-fit stretches the existing cards across the full width if there is empty room. auto-fill reserves empty column tracks for future elements without stretching existing cards.
Modern UI dashboards and landing pages (like Apple and Linear) use asymmetrical Bento grids with grid-column: span X; and grid-row: span Y;.
Spans 2 columns & 2 rows.
.bento-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 120px;
gap: 14px;
}
.bento-featured { grid-column: span 2; grid-row: span 2; }
.bento-wide { grid-column: span 2; }
.bento-tall { grid-row: span 2; }
Design complete application layouts visually in code using ASCII-like named areas.
The main view automatically fills the remaining horizontal and vertical space allotted by the grid track template.
.dashboard {
display: grid;
grid-template-areas:
"header header header"
"sidebar main main"
"sidebar widget widget"
"footer footer footer";
grid-template-columns: 180px 1fr 1fr;
gap: 12px;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.widget { grid-area: widget; }
.footer { grid-area: footer; }
Shorthand for align-items: center and justify-items: center combined. It centers elements in both axes with a single property!
.container {
display: grid;
place-items: center; /* Centers both horizontally & vertically! */
height: 180px;
}
grid-template-rows: subgrid; allows child cards to inherit their parent's grid row tracks. Notice how all card headers, varying-length bodies, and footers align horizontally across all 3 cards!
.card-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto 1fr auto;
}
.card {
display: grid;
grid-template-rows: subgrid; /* Inherits parent tracks! */
grid-row: span 3; /* Spans header, body, footer */
}