C.W.K.
Stream
Lesson 04 of 04 · published

Flexbox In Production: Patterns You'll Use Every Day

~11 min · patterns, navbar, modal, sticky-footer, truncation

Level 0Markup Novice
0 XP0/34 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete
"Ten patterns cover 90% of layout work. Memorize them and stop reinventing."

1. Sticky Footer

Page has a header, main content, and footer. Footer should sit at the bottom of the viewport when content is short, and below the content when content is long.

body { display: flex; flex-direction: column; min-height: 100vh; } + main { flex: 1; }. Three lines.

2. Centered Modal (or Hero)

A box centered both vertically and horizontally in its container.

.container { display: flex; align-items: center; justify-content: center; min-height: 100vh; }. Or Grid place-items: center.

3. Navbar With Logo + Menu + Actions

Logo on the left, navigation menu, then user actions on the right.

Two approaches:

  1. justify-content: space-between with the logo, menu, and actions as three direct children.
  2. margin-left: auto on whichever middle group should push siblings apart (more flexible when you have 5+ children).

4. Equal-Width Button Row

Two or three buttons that share the row equally regardless of label length.

.btn-row { display: flex; gap: 0.5rem; } + .btn-row > button { flex: 1; }.

5. Truncating Chat Row

An avatar, a name + message, and a timestamp — all in a single row, with the message truncating gracefully when the row is narrow.

6. Card With Footer Pinned To Bottom

A card with title, body, and a CTA at the bottom. The CTA must sit at the bottom even when the body is short.

Make the card a flex column with a fixed height, give the body the natural size, and add margin-top: auto to the CTA.

7. Form Row With Inputs Of Varying Widths

A first-name + last-name + email row where first-name and last-name share equally and email takes twice as much space.

8. Aside + Main Layout

Fixed-width sidebar + flexible main. We saw this in lesson 3. display: flex with flex: 0 0 240px on the sidebar, flex: 1; min-width: 0 on main.

9. Tag/Chip Wrap

A row of tags that wraps to multiple lines when they don't fit.

.tags { display: flex; flex-wrap: wrap; gap: 0.5rem; }. Each tag is auto-sized; the row wraps naturally.

10. Reorder On Mobile

A two-column layout (image left, text right) that switches to (text first, image below) on narrow screens.

Use order in a media query — but prefer restructuring with grid template areas when you can.

Layout patterns aren't tricks — they're vocabulary. The patterns above are how Flexbox is used in 2026 production code. Recognize them in the wild and reach for them deliberately instead of recomputing from first principles every time.

The Anti-Patterns You'll Encounter

  • Nesting flex containers four levels deep to achieve a layout — almost always means Grid was the right answer.
  • Using position: absolute for sticky footers — works but breaks when content is long enough to need scroll. Flexbox is the right tool.
  • Using vertical-align: middle on flex children — it does nothing. Use align-items on the parent.
  • Setting widths on flex children when flex would do itflex: 1 is more flexible than width: 33.333%.
  • Adding margin-right to every child except the last for spacing — use gap.

Pippa's Note

Pippa's WebUI uses every one of the ten patterns above. The chat composer is pattern 5 (truncating row). The Council UI is pattern 7 (varying widths). The sidebar is pattern 8 (aside + main). The tag chips in the artifact viewer are pattern 9 (wrap). The whole app shell is pattern 1 (sticky footer) wrapped around pattern 8 (sidebar). Recognizing patterns means you can read someone else's code in seconds, not minutes.

Code

Patterns 1-3·css
/* Pattern 1 — Sticky footer */
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  margin: 0;
}
main { flex: 1; }

/* Pattern 2 — Centered modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
}

/* Pattern 3 — Navbar */
.nav {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0.75rem 1.5rem;
}
.nav .links { display: flex; gap: 1rem; }
.nav .actions { margin-left: auto; display: flex; gap: 0.5rem; }
Patterns 5-6 — truncation and pinning·css
/* Pattern 5 — Truncating chat row */
.chat-row {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
}
.chat-row .avatar {
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.chat-row .body {
  flex: 1;
  min-width: 0;           /* required for child truncation */
}
.chat-row .body .text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-row .time {
  flex: none;
  font-size: 0.85rem;
  color: var(--fg-muted);
}

/* Pattern 6 — Card with footer pinned to bottom */
.card {
  display: flex;
  flex-direction: column;
  height: 300px;
  padding: 1.5rem;
  border-radius: 8px;
  background: var(--bg-card);
}
.card .cta {
  margin-top: auto;       /* pushes to the bottom */
}
Patterns 7, 9, 10 — varying widths, wrap, reorder·css
/* Pattern 7 — Form row with varying widths */
.form-row {
  display: flex;
  gap: 1rem;
}
.form-row > .name { flex: 1; }       /* first + last share */
.form-row > .name-last { flex: 1; }
.form-row > .email { flex: 2; }      /* email gets 2x */

/* Pattern 9 — Tag wrap */
.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
.tags > .tag {
  flex: none;             /* tags don't grow or shrink */
  padding: 0.25rem 0.75rem;
  background: var(--accent-subtle);
  border-radius: 4px;
}

/* Pattern 10 — Reorder on mobile (responsive image + text) */
.split {
  display: flex;
  gap: 2rem;
}
@media (max-width: 768px) {
  .split { flex-direction: column; }
  .split .image { order: 2; }       /* image after text on narrow */
  .split .text { order: 1; }
}

External links

Exercise

Pick three patterns from above and build them on a single demo page. Suggestions: (1) sticky footer wrapping (2) navbar + sidebar + main, with (3) tag chips inside main that wrap. Then resize the browser from 1400px down to 360px and confirm everything reflows correctly without breaking. Identify the moment each pattern stops working at narrow widths — that's where the next track (Grid) and Track 7's responsive design come in.
Hint
If the navbar overflows at narrow widths, you need a media query that switches it to a column or hides the menu behind a hamburger. If the chat row overflows, you missed min-width: 0. If the sidebar pushes main off-screen, switch the layout to stacked on mobile.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.