Lab 03: CSS Grid Advanced

Time: 30 minutes | Level: Practitioner | Docker: docker run -it --rm node:20-alpine sh

Overview

Master advanced CSS Grid: named template areas, auto-placement with dense, minmax()/fit-content()/repeat(), subgrid, named lines, implicit grid, and building a magazine layout.


Step 1: Grid Template Areas

.dashboard {
  display: grid;
  grid-template-areas:
    "header  header  header"
    "sidebar main    main  "
    "sidebar widgets widgets"
    "footer  footer  footer";
  grid-template-columns: 240px 1fr 300px;
  grid-template-rows: 60px 1fr auto 50px;
  min-height: 100vh;
  gap: 0;
}

.dashboard__header  { grid-area: header; }
.dashboard__sidebar { grid-area: sidebar; }
.dashboard__main    { grid-area: main; }
.dashboard__widgets { grid-area: widgets; }
.dashboard__footer  { grid-area: footer; }

/* Period (.) = empty cell */
.sparse-grid {
  display: grid;
  grid-template-areas:
    "logo . nav"
    ".    . .  "
    "hero hero hero";
}

💡 Every row must have the same number of cells. Named areas must be rectangular (no L-shapes).


Step 2: Auto-Placement & Dense Packing


Step 3: minmax(), fit-content(), repeat()

💡 auto-fill vs auto-fit: Use auto-fill when you want empty tracks to hold space. Use auto-fit (more common) when you want items to stretch and fill the row.


Step 4: Named Lines


Step 5: Subgrid


Step 6: Implicit Grid


Step 7: Magazine Layout


Step 8: Capstone — HTML Generation with Grid Demo

📸 Verified Output:


Summary

Feature
Syntax
Use Case

Template areas

grid-template-areas: "a b"

Named layout regions

Auto dense

grid-auto-flow: row dense

Fill gaps in grid

minmax

minmax(200px, 1fr)

Flexible tracks

fit-content

fit-content(300px)

Content-sized column

auto-fill

repeat(auto-fill, ...)

Fixed-size with empty tracks

auto-fit

repeat(auto-fit, ...)

Stretching card grids

Subgrid

grid-template-rows: subgrid

Align across grid items

Named lines

[start] 1fr [end]

Semantic placement

Implicit rows

grid-auto-rows: 120px

Control overflow rows

span

grid-column: span 3

Multi-column/row item

Last updated