Flexbox Items and Common Patterns
Introduction
After learning Flexbox container properties, the next step is controlling each item precisely. Item-level properties decide how space is shared, which elements grow or shrink, and how individual alignment behaves. This chapter focuses on flex item controls and practical UI patterns you can reuse in real projects.
Prerequisites
- Flexbox container basics (
display: flex, axis concepts) - Understanding of
justify-content,align-items, andgap - Familiarity with responsive sizing basics
Flex Item Sizing Model
Three key properties control item behavior in available space:
flex-growflex-shrinkflex-basis
Shorthand:
.item {
flex: 0 1 auto;
}This is conceptually:
- do not grow (
0) - can shrink (
1) - base size from
auto
flex-grow
flex-grow controls how much an item expands when extra space exists.
.sidebar {
flex-grow: 1;
}
.main {
flex-grow: 2;
}In this case, .main receives roughly twice the extra space of .sidebar.
Use cases:
- proportional column growth
- “primary content area grows more than secondary panel”
flex-shrink
flex-shrink controls how items reduce size when space is limited.
.logo {
flex-shrink: 0;
}Common reason:
- prevent important items (logo, button label) from collapsing too early
Default shrink behavior can be surprising when containers get narrow, so set item constraints intentionally.
flex-basis
flex-basis defines the initial size before grow/shrink distribution.
.card {
flex-basis: 280px;
}This acts like preferred starting width in a row layout.
With wrapping, basis values often define how many items fit per line.
The flex Shorthand
Most practical patterns use shorthand:
.item {
flex: 1;
}flex: 1 is commonly interpreted as:
- grow:
1 - shrink:
1 - basis:
0%
Other useful patterns:
.fixed {
flex: 0 0 auto;
}
.fluid {
flex: 1 1 240px;
}align-self
align-self overrides cross-axis alignment for one specific item.
.toolbar {
display: flex;
align-items: center;
}
.danger-button {
align-self: flex-end;
}Useful for one-off adjustments without changing all siblings.
order
order changes visual order of flex items.
.primary {
order: 2;
}
.secondary {
order: 1;
}Important caution:
- visual order changes, DOM order does not
- keyboard navigation and screen readers still follow DOM order
Use order sparingly and avoid creating accessibility confusion.
Warning
DOM Order Matters
Do not rely on order to fix semantic structure problems. Keep source order logical first.
Pattern 1: Equal-Width Columns
.row {
display: flex;
gap: 1rem;
}
.row > * {
flex: 1;
}Great for simple feature blocks and quick dashboards.
Pattern 2: Fixed Sidebar + Fluid Main
min-width: 0 on the flexible area often prevents overflow issues with long content.
Pattern 3: Wrapping Card List
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 260px;
}Items wrap naturally while keeping a preferred minimum width.
Pattern 4: Toolbar with Push-Right Actions
.toolbar {
display: flex;
align-items: center;
gap: 0.75rem;
}
.toolbar-actions {
margin-left: auto;
display: flex;
gap: 0.5rem;
}Clean pattern for headers and action bars.
Pattern 5: Media Object (Image + Content)
Useful for profile lists, comments, and notification items.
Flex Item Debugging Tips
When items size unexpectedly:
- inspect each item's computed
flex-grow/shrink/basis - verify if wrapping is enabled
- check explicit
width/min-widthconflicts - test
min-width: 0on flexible content containers - use DevTools flex overlay for axis and distribution visualization
Most overflow bugs in flex rows come from unresolved minimum-size constraints.
Tip
Remember min-width: 0
Flexible children with long text can overflow parents unless you explicitly allow shrinking.
Common Mistakes
| Mistake | Why it hurts | Better approach |
|---|---|---|
Using only width and ignoring flex-* | Poor space distribution | Use flex values intentionally |
| Forgetting wrap in responsive rows | Items squash or overflow | Add flex-wrap: wrap where needed |
Overusing order for layout hacks | Accessibility and maintenance risks | Keep semantic DOM order |
| Ignoring min-size behavior | Text/content overflow in flex item | Add min-width: 0 to flexible areas |
| Mixing too many nested flex patterns | Complex debugging | Keep structure simple and componentized |
Mini Exercise
- Build a two-column layout (fixed sidebar + fluid content)
- Add a toolbar with right-aligned action group
- Create a wrapping card list with
flex: 1 1 240px - Force long title text and fix overflow using
min-width: 0 - Test behavior at mobile, tablet, and desktop widths
FAQ
What is the difference between width and flex-basis?
flex-basis is the starting size used by Flexbox before growth/shrink distribution. width is a general sizing property and can interact differently with flex sizing rules.
Why are my flex items shrinking too much?
Default flex-shrink is often 1. Add constraints (flex-shrink: 0, min-width, or wrapping) where needed.
Is flex: 1 always correct?
No. It is useful for equal flexible items, but many layouts need mixed behavior (fixed + fluid, wrapped cards, protected buttons).
Why does long text overflow inside a flex child?
Because some flex items have minimum content sizing behavior. Setting min-width: 0 often allows expected shrinking.
Should I reorder content visually with order for responsive design?
Use sparingly. Prefer logical DOM order first to preserve accessibility and predictable reading/navigation flow.