CSS Flexbox is a one-dimensional layout system for arranging items in rows or columns. Here's a complete guide.
Basic Flexbox#
Flex Direction#
Flex Wrap#
Justify Content (Main Axis)#
Align Items (Cross Axis)#
Align Content (Multiple Lines)#
Gap#
Flex Item Properties#
Align Self#
Order#
Centering#
Navigation Bar#
Card Layout#
Holy Grail Layout#
Equal Height Columns#
Responsive Flexbox#
Sticky Footer#
Media Object#
Form Layout#
Mastering Margin Auto#
Best Practices#
Container:
✓ Use gap for spacing
✓ Set flex-wrap for responsive
✓ Use min-height not height
✓ Consider overflow
Items:
✓ Use flex shorthand
✓ Set flex-shrink: 0 for fixed
✓ Use margin-top: auto for push
✓ Avoid fixed widths
Responsive:
✓ Mobile-first approach
✓ Use flex-basis with calc
✓ Change direction at breakpoints
✓ Test at various widths
Avoid:
✗ Nesting too deeply
✗ Using for 2D layouts (use Grid)
✗ Forgetting flex-shrink
✗ Fixed heights that overflow
Conclusion#
Flexbox excels at one-dimensional layouts - rows or columns. Use justify-content for main axis alignment, align-items for cross axis, and gap for spacing. The flex shorthand controls how items grow and shrink. Combine with CSS Grid for complete layout control. For two-dimensional layouts, Grid is often more appropriate.