Published: April 2026 · 12 min read · Design Tools
CSS Flexbox has transformed how we build web layouts. Gone are the days of float hacks and clearfix workarounds. Flexbox provides a clean, intuitive way to align, distribute, and reorder elements — whether you're building a simple navigation bar or a complex responsive dashboard. This guide covers every Flexbox property with practical examples you can use today.
Flexbox (Flexible Box Layout Module) is a CSS layout model designed for one-dimensional layouts — arranging items in a single row or column. Unlike traditional block or inline layouts, Flexbox gives you precise control over how space is distributed, how items align, and how they respond to different screen sizes.
Flexbox works on two levels: the flex container (parent) and flex items (children). Set display: flex on the parent, and all direct children become flex items that respond to Flexbox properties.
display: flex
These properties are applied to the parent element to control the overall layout behavior:
Controls the direction items are laid out:
.container { display: flex; flex-direction: row; /* Default: left to right */ /* flex-direction: column; */ /* Top to bottom */ /* flex-direction: row-reverse; */ /* flex-direction: column-reverse; */ }
Distributes space along the main axis (horizontal by default):
.container { display: flex; justify-content: flex-start; /* Default: items packed to start */ /* justify-content: flex-end; */ /* Items packed to end */ /* justify-content: center; */ /* Items centered */ /* justify-content: space-between; */ /* Equal space between */ /* justify-content: space-around; */ /* Equal space around */ /* justify-content: space-evenly; */ /* Equal space everywhere */ }
Aligns items along the cross axis (vertical by default):
.container { display: flex; align-items: stretch; /* Default: items stretch to fill */ /* align-items: flex-start; */ /* Items align to top */ /* align-items: flex-end; */ /* Items align to bottom */ /* align-items: center; */ /* Items centered vertically */ /* align-items: baseline; */ /* Items align by text baseline */ }
Controls whether items wrap onto multiple lines:
.container { display: flex; flex-wrap: nowrap; /* Default: single line, items shrink */ /* flex-wrap: wrap; */ /* Items wrap to next line */ /* flex-wrap: wrap-reverse; */ /* Wrap in reverse order */ }
Sets spacing between flex items without margins:
.container { display: flex; gap: 1rem; /* Equal gap in all directions */ /* gap: 1rem 2rem; */ /* Row gap 1rem, column gap 2rem */ }
These properties are applied to individual children to control their sizing and alignment:
The holy trinity of Flexbox sizing:
.item { flex-grow: 1; /* How much the item should grow (default: 0) */ flex-shrink: 1; /* How much the item should shrink (default: 1) */ flex-basis: auto; /* Initial size before growing/shrinking */ } /* Shorthand: flex: grow shrink basis */ .item { flex: 1; /* flex: 1 1 0% — grow equally, shrink equally */ flex: 0 0 200px; /* Don't grow, don't shrink, fixed 200px */ flex: 2; /* Grow at 2x rate compared to flex: 1 items */ }
Override the container's align-items for a specific item:
align-items
.container { display: flex; align-items: center; } .special-item { align-self: flex-end; /* This one goes to the bottom */ }
Change the visual order of items without modifying HTML:
.item-1 { order: 3; } /* Appears last */ .item-2 { order: 1; } /* Appears first */ .item-3 { order: 2; } /* Appears middle */
The most common Flexbox use case — centering anything:
.center-container { display: flex; justify-content: center; align-items: center; min-height: 100vh; }
.navbar { display: flex; justify-content: space-between; align-items: center; padding: 1rem 2rem; } .nav-logo { flex-shrink: 0; } .nav-links { display: flex; gap: 1.5rem; }
.card-grid { display: flex; flex-wrap: wrap; gap: 1.5rem; } .card { flex: 1 1 300px; /* Grow, shrink, min 300px wide */ max-width: calc(33.333% - 1rem); /* Max 3 per row */ }
.page { display: flex; flex-direction: column; min-height: 100vh; } .page-header { /* ... */ } .page-content { flex: 1; } /* Takes all available space */ .page-footer { /* Sticks to bottom */ }
.media-object { display: flex; align-items: flex-start; gap: 1rem; } .media-object img { flex-shrink: 0; width: 80px; height: 80px; } .media-body { flex: 1; }
.columns { display: flex; gap: 1rem; } .columns > * { flex: 1; } /* All children equal width */
gap
flex: 1
flex-basis: 100%
order
min-height: 100vh
CSS Flexbox (Flexible Box Layout) is a one-dimensional layout model for arranging items in rows or columns. Use it when you need to distribute space dynamically, align items along an axis, or reorder elements within a container. It's ideal for navigation bars, card layouts, centering content, form layouts, and any component that flows in a single direction. Think "content-first" — Flexbox sizes based on content, not fixed grids.
Apply three properties to the parent container: display: flex, justify-content: center, and align-items: center. This centers the child element both horizontally and vertically. The complete CSS is: .parent { display: flex; justify-content: center; align-items: center; min-height: 100vh; } — this works for any element, regardless of its size.
justify-content: center
align-items: center
.parent { display: flex; justify-content: center; align-items: center; min-height: 100vh; }
justify-content controls alignment along the main axis (horizontal by default in a row layout), distributing space between items. align-items controls alignment along the cross axis (vertical by default). If you change flex-direction to column, these roles swap: justify-content becomes vertical alignment and align-items becomes horizontal alignment.
justify-content
flex-direction
column
Absolutely, and you should. Flexbox and Grid are complementary tools, not competing alternatives. Use CSS Grid for two-dimensional page-level layouts (header, sidebar, main content, footer) and Flexbox for one-dimensional component layouts within those grid cells (navigation items, card content, form rows, media objects). This combination gives you the best of both worlds.
Visual CSS Grid layout builder with drag-and-drop placement and responsive controls.
Create beautiful CSS gradients with a visual editor and export clean code.
Design layered box shadows with real-time preview for depth and elevation.
← Back to Blog