Fullstack CourseLearn by building
Back to week 1

Topic

Flexbox & Grid alignment

Definition

Flexbox is a one-dimensional layout model for aligning items along a main axis and cross axis; CSS Grid is a two-dimensional layout model that places items into rows and columns with explicit tracks and alignment controls.

In simpler words

Flex is for a row or a column of items (nav, toolbars, card rows). Grid is for full page or card grids with rows and columns together. Use justify-* and align-* to distribute and center; prefer gap over margin hacks.

Flex direction, wrapping, justify-content, align-items, gap; Grid tracks and simple alignment; choosing flex vs grid.

After this you can

  • Center a card, space a toolbar, and build a two-column layout with the correct model.
  • Explain the trade-off to a teammate using a small example.
  • Name at least one common bug pattern for this topic.

Understand Flexbox & Grid alignment

Flex direction, wrapping, justify-content, align-items, gap; Grid tracks and simple alignment; choosing flex vs grid.

Start by identifying which value or browser behavior changes. Then describe the UI from that current input instead of editing the DOM as a separate source of truth.

Flexbox & Grid alignment in code

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.dashboard {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 16px;
  align-items: start;
}

Read the example from data and control flow to the resulting UI. Keep the component boundary small.

Apply Flexbox & Grid alignment

Keep rendering as a calculation. Put user-triggered changes in event handlers, preserve UI memory in state, and reserve external synchronization for Effects or the server-state layer.

Name values by their UI meaning, test the loading and error path when data is remote, and avoid keeping two editable copies of the same value.

Ask before adding code: is this local UI memory, shared client state, or Nest-owned server state?

Where bugs hide

Definition

High-bug areas are places where a small API misuse looks correct but produces stale UI, duplicate work, or silent failures.

In simpler words

Each mistake below shows Wrong vs Right code — compare them side by side.

When something misbehaves, match the symptom to a pattern below before rewriting the feature.

Prefer fixing the ownership or update path over adding another Effect or sync step.

Mistake: Margins instead of gap

// Wrong
.row > * { margin-right: 12px; }
.row > *:last-child { margin-right: 0; }

// Right
.row {
  display: flex;
  gap: 12px;
}

gap spaces flex/grid items without last-child margin tricks.

Mistake: Using flex for a full 2D page grid

// Wrong
.page {
  display: flex;
  flex-wrap: wrap;
}
.page > * { width: 33%; }

// Right
.page {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

Wrapping flex with percentage widths is fragile. Grid owns two-dimensional track sizing.

Mistake: Confusing justify-content with align-items

// Wrong
.center {
  display: flex;
  /* main axis is row by default */
  align-items: center; /* cross axis only */
}

// Right
.center {
  display: flex;
  justify-content: center; /* main axis */
  align-items: center;     /* cross axis */
}

justify-content distributes along the main axis; align-items aligns on the cross axis.

Live playground

Flexbox & Grid alignment sandbox

Change one input at a time and predict the next render.

No interactive demo for course-css-flexbox-grid yet — use the code samples and Wrong vs Right examples above.

Keep in mind

  • Keep the formal definition in mind; it explains which tool belongs where.
  • Prefer one source of truth over synchronized copies of the same value.
  • When behavior surprises you, trace: input → update → render → committed UI.
  • Study the Wrong vs Right examples in “Where bugs hide” before you merge.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥ 80% (enforced by the API) to save progress.

Checking your session…

10 questions · concept 3 · syntax 3 · practical 2 · logic 2

Concept1. What is Flexbox primarily for?
Syntax2. Which rule centers items on both axes in a flex container?
Practical3. You need a toolbar with space between left and right groups. Best approach?
Logic4. When should you prefer Grid over Flex?
Concept5. What does gap do in flex/grid?
Syntax6. Which creates a two-column grid?
Practical7. A flex row wraps awkwardly with width:33% children. Better fix?
Logic8. justify-content vs align-items on a default row flex container?
Concept9. What does flex-wrap: wrap allow?
Syntax10. Which sets the flex main axis to column?