Zum Hauptinhalt springen
← 🎨 CSS as a design system·Modul B3 · Lektion 1
AufgabeBuild the simplest 2x2 grid. .grid uses display: grid, grid-template-columns: 1fr 1fr, gap: 12px. Four .cell divs inside, each padding 16px, background #eef.

Warum Grid: 2D-Layout vs. 1D von Flex

75 XP7 min
Theorie

Flex handles ONE axis at a time

Flex is for "items in a row" or "items in a column." The PERPENDICULAR axis is whatever the items decide (their content + cross-axis align).

.row { display: flex; gap: 16px; }      /* row of items; each gets its own height */

If you want 4 items in a 2x2 grid where every cell is exactly the same size: flex makes you do nested flexes or row-wrap with manual sizing. Awkward.

Grid handles BOTH axes

.dashboard {
  display: grid;
  grid-template-columns: 1fr 1fr;     /* 2 equal columns */
  grid-template-rows: auto auto;      /* 2 rows, sized by content */
  gap: 16px;
}

Every cell is a precise box. Items can SPAN multiple rows or columns. The grid stays rectangular no matter what content each cell has.

When to use each

PatternBest tool
Nav bar (logo + links)flex
Vertical stack of cardsflex column
Card row that wrapsflex-wrap OR grid auto-fit
Dashboard (sidebar + topbar + main + footer)grid
Photo gallerygrid
Form (label-input rows aligned in columns)grid
"Holy Grail" layoutgrid

Rule of thumb: if you'd want to draw the layout as a 2D wireframe, use grid. If you'd just list items in a row or column, use flex.

They compose

A grid CELL can contain a flex layout — .dashboard is grid, each panel inside is flex. Use the right tool at each level.

🔒

Anmelden, um zu programmieren

Theorie ist für alle offen. Editor, Live-Preview und Check werden mit 7-Tage-Gratis-Test freigeschaltet — Karte erforderlich, jederzeit kündbar.

Registrieren — Gratis-Test →

Die ersten 10 Lektionen jedes Tracks sind kostenlos. Keine Karte nötig.

← ZurückNächste Lektion →

Bekomme einen Python- oder Web-Tipp pro Tag — per E-Mail

Kurz, handgeschrieben, kein Spam. Abmelden mit einem Klick.