Flexbox (Flexible Box Layout) is a one-dimensional layout model designed for distributing space and aligning items along a single axis — either horizontally or vertically. Unlike CSS Grid which manages two axes simultaneously, Flexbox excels at dynamic layouts where content size is unknown or flexible.
Use Flexbox when you need to align items in a row or column, distribute space dynamically, or center content. It is ideal for navigation bars, card rows, toolbars, and any layout where items need to flex and adapt to available space.
flex-basics.css
CSS
1
.container {
2
display: flex;
3
justify-content: space-between;
4
align-items: center;
5
gap: 16px;
6
}
preview
When to Use Flexbox vs Grid
The common question is not which is better, but which is appropriate for your layout goal.
Scenario
Recommended
Why
Navigation bar
Flexbox
One-dimensional row alignment
Card grid
Grid
Two-dimensional row/column control
Centering a modal
Flexbox
Simple centering on either axis
Holy grail page layout
Grid
Named areas and two-axis control
Toolbar with wrapping
Flexbox
Flex-wrap and dynamic sizing
Masonry / magazine layout
Grid
Explicit placement across rows/columns
Flex Container Properties
The flex container establishes the flex formatting context. All direct children become flex items arranged along the main axis.
.nav-item { flex: 0 1 auto; } /* sized by content, can shrink */
preview
align-self
Overrides the container's align-items for a single item.
align-self.css
CSS
1
.item {
2
align-self: auto | flex-start | flex-end | center | baseline | stretch;
3
}
preview
order
Controls the visual order of flex items. Items with a lower order value appear first. Default is 0.
flex-order.css
CSS
1
.first { order: -1; } /* appears first */
2
.last { order: 1; } /* appears last }
3
.normal { order: 0; } /* default order */
preview
Main Axis vs Cross Axis
Understanding the two axes is fundamental to mastering Flexbox. The main axis is determined by flex-direction, and the cross axis runs perpendicular to it.
flex-direction
Main Axis
Cross Axis
justify-content
align-items
row
Horizontal (→)
Vertical (↓)
horizontal alignment
vertical alignment
column
Vertical (↓)
Horizontal (→)
vertical alignment
horizontal alignment
Flexbox Alignment Deep Dive
Flexbox alignment involves two sets of properties: one for the main axis (justify-content) and one for the cross axis (align-items, align-content, align-self).
The place-items shorthand sets both align-items and justify-items (though justify-items does not apply to flex containers). Similarly, place-content sets both align-content and justify-content.
Flexbox and Grid are complementary, not competing. Use them together: Grid for the overall page structure, Flexbox for components inside grid cells.
Best Practices
◆Use Flexbox for one-dimensional layouts and Grid for two-dimensional layouts
◆Prefer the flex shorthand over setting grow, shrink, and basis individually
◆Use gap instead of margins on flex items for cleaner spacing
◆Set flex-wrap: wrap for responsive rows that adapt to container width
◆Avoid using order to change visual order — it breaks keyboard navigation focus order
◆Use align-self sparingly — prefer consistent alignment on the container
◆Set min-height: 100vh on flex column containers for sticky footer patterns
◆Combine Flexbox and Grid — each excels at different layout problems
✓
best practice
When building a layout, start with Flexbox for the simplest approach. If you find yourself fighting the one-dimensional nature (trying to align across rows), switch to Grid — it is the right signal that you need two-dimensional control.