CSS Layout
Flexbox & Grid
Complete visual guide to CSS Flexbox and Grid with all properties, values, and real examples.
📖 5 sections
⏰ 15 min read
✅ Quizzes included
01Flexbox Container
CSSFlex container properties
display: flex | inline-flex

flex-direction: row (default) | column | row-reverse | column-reverse

justify-content (main axis):
  flex-start | flex-end | center
  space-between | space-around | space-evenly

align-items (cross axis, single line):
  stretch (default) | flex-start | flex-end
  center | baseline

align-content (cross axis, multiple lines):
  flex-start | flex-end | center
  space-between | space-around | stretch

flex-wrap: nowrap (default) | wrap | wrap-reverse

gap: 16px       /* both row and column gap */
row-gap: 8px    /* between rows */
column-gap: 16px /* between columns */
02Flexbox Items
CSSFlex item properties
flex-grow: 0 (default)  /* how much to grow */
flex-shrink: 1 (default) /* how much to shrink */
flex-basis: auto        /* initial size */

/* Shorthand */
flex: 0 1 auto   /* default */
flex: 1          /* grow 1, shrink 1, basis 0 */
flex: 1 1 200px  /* grow 1, shrink 1, basis 200px */
flex: none       /* 0 0 auto (no grow or shrink) */

align-self: auto | flex-start | flex-end | center | stretch
  /* overrides container align-items for this item */

order: 0 (default)  /* change visual order, lower = first */

margin: auto  /* pushes item to opposite side (useful trick) */
💡
Most common pattern: .container{display:flex;align-items:center;} to vertically center items. Add justify-content:center; to center both axes.
03Grid Container
CSSGrid container properties
display: grid | inline-grid

/* Define columns */
grid-template-columns: 200px 1fr 1fr;
grid-template-columns: repeat(3, 1fr);
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
grid-template-columns: auto auto auto; /* content-sized */

/* Define rows */
grid-template-rows: 60px 1fr auto;
grid-auto-rows: minmax(100px, auto); /* implicit rows */

/* Named areas */
grid-template-areas:
  "header header header"
  "sidebar main main"
  "footer footer footer";

gap: 24px;
row-gap: 16px; column-gap: 24px;
justify-items: stretch|start|end|center;  /* all cells */
align-items: stretch|start|end|center;     /* all cells */
04Grid Items
CSSGrid item placement
/* By line numbers */
grid-column: 1 / 3;    /* from line 1 to line 3 */
grid-column: 1 / -1;   /* from first to last */
grid-column: span 2;   /* span 2 columns */
grid-row: 1 / 3;

/* By area name */
.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }

/* Self alignment (overrides container) */
justify-self: start|end|center|stretch;
align-self: start|end|center|stretch;

/* Order: lower = earlier in grid */
order: -1;  /* place before other items */
❓ Quiz
What does repeat(auto-fit, minmax(200px, 1fr)) do?
auto-fit creates as many columns as fit, each minimum 200px wide. They expand equally (1fr) to fill available space. Responsive with no media queries!
05Common Layouts
CSSReal-world layout patterns
/* 1. Holy Grail Layout */
body {
  display: grid;
  grid-template:
    "header" 60px
    "nav main aside" 1fr
    "footer" 60px
    / 200px 1fr 200px;
  min-height: 100vh;
}

/* 2. Card Grid */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
}

/* 3. Centered Hero */
.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 80vh;
}

/* 4. Sidebar + Content */
.layout {
  display: grid;
  grid-template-columns: 250px 1fr;
  gap: 0;
}