Lesson 10: Flexbox Alignment and Wrapping
🎯 Lesson 10: Flexbox Alignment & Wrapping
Flexbox gives you powerful control over how items align and wrap inside a container. Once you master alignment, you can make your UI dance like a pro.
1. Wrapping with flex-wrap
By default, Flexbox tries to fit all items into one line, shrinking them if needed. Use flex-wrap to let items wrap into multiple lines.
.container {
display: flex;
flex-wrap: wrap;
}
nowrap– default behavior, single linewrap– wrap items to the next linewrap-reverse– wrap in reverse order
2. Cross-Axis Group Alignment: align-content
When items wrap onto multiple lines, align-content controls the spacing between those lines (not individual items).
.container {
display: flex;
flex-wrap: wrap;
align-content: space-around;
}
flex-start– pack lines at the topcenter– center the linesspace-between– even spacing between linesspace-around– spacing around linesstretch– fill the container (default)
3. Individual Item Alignment: align-self
Sometimes, you want one item to break the rules. Use align-self to align a specific item differently from others.
.item-special {
align-self: flex-end;
}
auto– inherits fromalign-itemsflex-start,center,flex-endstretch– fill the container height
⚡ Pro Tip
Flexbox alignment is based on the container’s size and direction. Test with different container heights and directions (row vs column) to really understand how it behaves.
✅ Recap
- Use
flex-wrapto control if items wrap onto new lines align-contentcontrols alignment between wrapped linesalign-selflets you override alignment per item
Up next? You’ll use this to build real layouts, grids, cards, navbars, and more. Flexbox is the backbone. Master it and you’ll laugh in the face of layout bugs.