CSS: Flex Box Layout
xtodo(ai helped)
What's Flex Box Layout
Flexible Box Layout is very useful, flexible, one-dimensional layout. It's very easy to align, distribute, and size items within a container.
Example
To use flexbox, set display: flex (or inline-flex) on the parent element.
<div class="xflexbox"> <div class="xitem">1</div> <div class="xitem">2</div> <div class="xitem">3</div> </div>
.xflexbox { display: flex; border: solid 1px red; } .xitem { border: solid 1px blue; padding: 20px; margin: 6px; }
Browser shows
Result: The three items line up horizontally in a row.
Main Axis vs. Cross Axis
2 important concepts:
- Main axis: The direction items are laid out.
- Cross axis: Perpendicular to the main axis.
Flex Container Properties
flex-direction
Defines the direction of the main axis.
.xflexbox { flex-direction: row; /* default: left to right */ flex-direction: row-reverse; /* right to left */ flex-direction: column; /* top to bottom */ flex-direction: column-reverse; }
flex-wrap
flex-wrap property controls whether flex items are forced into a single line or allowed to wrap onto multiple lines within a flex container.
By default, flex containers use nowrap, which keeps items in one line even if it causes overflow; setting it to wrap allows items to flow onto new lines as needed, aiding responsive layouts without media queries.
The property accepts three primary values:
nowrap-
The default. Items remain in a single line and may overflow the container.
wrap-
Items break into multiple lines, stacking in the direction defined by
flex-direction. wrap-reverse-
Items wrap into multiple lines but stack in the opposite direction of
flex-direction.
.xflexbox { flex-wrap: nowrap; /* default */ flex-wrap: wrap; /* wrap to next line */ flex-wrap: wrap-reverse; }
Shorthand:
flex-flow is a shorthand for
flex-directionflex-wrap
.xflex { flex-flow: row wrap; }
justify-content (Main Axis Alignment)
Distributes space along the main axis.
.xflexbox { justify-content: flex-start; /* default */ justify-content: flex-end; justify-content: center; justify-content: space-between; /* items at edges, equal space between */ justify-content: space-around; /* equal space around items */ justify-content: space-evenly; /* fully equal spacing */ }
align-items (Cross Axis Alignment)
Aligns items along the cross axis.
.xflexbox { align-items: stretch; /* default */ align-items: flex-start; align-items: flex-end; align-items: center; align-items: baseline; }
align-content (Multi-line Cross Axis)
Only works when flex-wrap: wrap is used.
.xflexbox { align-content: stretch; /* default */ align-content: flex-start; align-content: center; align-content: space-between; }
gap (Spacing Between Items)
Modern and clean way to add space.
.xflexbox { gap: 20px; /* all sides */ row-gap: 15px; column-gap: 30px; }
4. Flex Item Properties
flex Shorthand
The most important property for items.
.xitem { flex: 1; /* grow:1, shrink:1, basis:0% */ flex: 2 1 300px; /* grow, shrink, basis */ }
flex-grow: How much the item grows relative to others (default 0).flex-shrink: How much the item shrinks (default 1).flex-basis: Initial size before growing/shrinking (default auto).
align-self (Override alignment for one item)
.xitem { align-self: flex-start; /* overrides container's align-items */ }
order
Changes visual order without changing HTML.
.xitem:nth-child(2) { order: -1; } /* appears first */
Negative values come before 0; positive after.
5. Complete Example: Responsive Navigation + Content
<header class="nav"> <div class="logo">Logo</div> <nav class="menu"> <a>Home</a> <a>About</a> <a>Services</a> <a>Contact</a> </nav> </header> <div class="main-content"> <aside class="sidebar">Sidebar</aside> <main> Content area </main> </div>
.nav { display: flex; align-items: center; justify-content: space-between; padding: 1rem; background: #222; color: white; } .menu { display: flex; gap: 2rem; } .main-content { display: flex; gap: 20px; padding: 20px; } .sidebar { flex: 1 1 250px; /* sidebar stays ~250px */ } main { flex: 3 1 600px; /* content takes more space */ } /* Mobile */ @media (max-width: 768px) { .nav { flex-direction: column; } .main-content { flex-direction: column; } }
6. Common Patterns & Tips
Centered content:
.center-both { display: flex; justify-content: center; align-items: center; height: 100vh; }
Holy Grail Layout (header, footer, sidebar, content): Use flex-direction: column on body and nested flex containers.
Card Grid:
.cards { display: flex; flex-wrap: wrap; gap: 20px; } .card { flex: 1 1 300px; /* grows but won't go below 300px */ }
7. Browser Support & Best Practices
- Flexbox has excellent support (IE11+ with prefixes in very old browsers).
- Prefer
gapover margins for spacing. - Use
flex: 1for equal-width items. - Combine with Grid for two-dimensional layouts (Flexbox is 1D).
- Always test on mobile—Flexbox shines in responsive design.
Quick Reference Cheat Sheet
Container:
display: flex-
flex-direction,flex-wrap,flex-flow -
justify-content,align-items,align-content gap
Items:
flex: grow shrink basisalign-selforder
css flex
.xflex { display: flex; display: inline-flex; flex-direction: row; flex-direction: column; flex-wrap: wrap; flex-wrap: nowrap; flex-wrap: wrap-reverse; flex-basis: 1200px; justify-content: flex-start; align-self: auto normal stretch center start end self-start self-end flex-start flex-end baseline first last safe unsafe; align-items: center; align-content: flex-start; } .xflex2 { flex-grow: 2; flex-shrink: 2; flex: 1 1 300px; flex-basis: 300px; order: 2; }