Lesson 26 +20 XP

Cheatsheet: Flex & Grid

Flex & Grid Cheatsheet

A quick reference for the most useful layout utilities.

Flex container

GoalClasses
Enable flexflex / inline-flex
Row layoutflex-row
Column layoutflex-col
Wrap itemsflex-wrap
Center (main axis)justify-center
Center (cross axis)items-center
Space items evenlyjustify-between, justify-around, justify-evenly
Gapgap-4, gap-x-2, gap-y-6

Flex items

GoalClasses
Fill remaining spaceflex-1
Never shrinkshrink-0
Grow to fillgrow
Auto sizeflex-auto
No flex behaviorflex-none
Reorderorder-first, order-last, order-2
Align selfself-center, self-end

Grid container

GoalClasses
Enable gridgrid / inline-grid
3 equal columnsgrid-cols-3
2 equal rowsgrid-rows-2
Custom templategrid-cols-[200px_1fr]
Gapgap-4
Column flowgrid-flow-col
Dense packinggrid-flow-row-dense

Grid items

GoalClasses
Span 2 columnscol-span-2
Span all columnscol-span-full
Span 2 rowsrow-span-2
Start at line 2col-start-2
End at line 4col-end-4
Center in celljustify-self-center, self-center

Common patterns

  • Perfect center: flex items-center justify-center
  • Card grid: grid grid-cols-1 md:grid-cols-3 gap-6
  • Sidebar layout: grid grid-cols-[240px_1fr]
  • Auto-fit cards: grid-cols-[repeat(auto-fit,minmax(240px,1fr))]
  • Footer with gap: flex justify-between items-center

TL;DR

  • flex for 1D, grid for 2D layouts.
  • Centering: flex items-center justify-center.
  • Columns: grid-cols-<n>; spans: col-span-<n>.
  • Use gap instead of margins.