← Back to Overview Chapter 03: Grid

1. The Magic Auto-Fit Grid (Zero Media Queries!)

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!

Robot #1 Robot
Ninja #2 Ninja
Superhero #3 Superhero
Mage #4 Mage
Gem #5 Crystal
Fire #6 Element
.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 vs auto-fill: 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.

2. Bento Box Grid: Spanning Rows & Columns

Modern UI dashboards and landing pages (like Apple and Linear) use asymmetrical Bento grids with grid-column: span X; and grid-row: span Y;.

Stats 99.9% Uptime rate
Status

Live Tracker

Rocket Orbital velocity: Stable
Integration
OpenMoji Library Connected
Puzzle
Security
Vault Protected
256-bit encryption
.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; }

3. Semantic Layouts with grid-template-areas

Design complete application layouts visually in code using ASCII-like named areas.

Dashboard Header | Modern App Bar
Sidebar
  • Overview
  • Analytics
  • Settings
Main Content Area

The main view automatically fills the remaining horizontal and vertical space allotted by the grid track template.

Notification Widget: System is operating normally.
.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; }

4. The 1-Line Center: place-items: center

Shorthand for align-items: center and justify-items: center combined. It centers elements in both axes with a single property!

Sparkles
Centered in 1 line!
display: grid; place-items: center;
.container {
    display: grid;
    place-items: center; /* Centers both horizontally & vertically! */
    height: 180px;
}

5. Subgrid: Nested Subgrids (#3)

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 A (Short Header)
A brief paragraph that only takes one sentence.
Card B (Longer Title Spanning Multiple Lines)
This card has much longer text with detailed explanations, yet the footer below remains aligned with the other cards on the exact same row track!
Card C (Standard)
Balanced description showcasing subgrid alignment.
.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 */
}
Fixed 🏠