Introduction
1 . What is Flexbox and Why Use It?
2 . Setting Up a Flex Container
3 . Flex Direction
4 . Justify Content (Main Axis Alignment)
5 . Align Items (Cross Axis Alignment)
6 . Align Self (Individual Item Alignment)
7 . Flex Wrap
8 . Gap (Space Between Items)
9 . Order (Reordering Items)
10 . Flex Grow
11 . Flex Shrink
12 . Flex Basis
13 . The Flex Shorthand
14 . Align Content (Multi-line Alignment)
15 . Nested Flex Containers
16 . Responsive Navigation Bar with Flexbox
19 . Building a Pricing Table
20 . Common Flexbox Pitfalls
Flexbox (Flexible Box Layout Module) is one of the most important tools in modern CSS for creating responsive and adaptable layouts without relying on floats, positioning hacks, or complex grid systems.
It was designed to arrange elements predictably even when their size is unknown, making it ideal for building interfaces that work across devices and screen sizes.
Flexbox is a 1D layout system (rows or columns) that makes distributing space and aligning items easier.
It adapts to different screen sizes without extra media queries in many cases.
Eliminates the need for float hacks.
.container {
display: flex;
}
Use display: flex; to activate Flexbox for a container.
All its direct children become flex items.
.container {
display: flex;
background: #eee;
}
Controls the main axis direction:
row (default)
row-reverse
column
column-reverse
.container {
display: flex;
flex-direction: column;
}
Aligns items along the main axis:
flex-start, flex-end, center, space-between, space-around, space-evenly
.container {
display: flex;
justify-content: space-between;
}
Aligns items along the cross axis.
Values: flex-start, flex-end, center, baseline, stretch
.container {
display: flex;
align-items: center;
}
.item {
align-self: flex-end;
}
Controls whether items wrap to the next line:
nowrap (default)
wrap
wrap-reverse
.container {
flex-wrap: wrap;
}
.container {
display: flex;
gap: 20px;
}
.item-1 {
order: 2;
}
.item-2 {
order: 1;
}
.item {
flex-grow: 2; /* grows twice as fast as items with flex-grow: 1 */
}
.item {
flex-shrink: 0; /* prevents shrinking */
}
.item {
flex-basis: 200px;
}
.item {
flex: 1 0 150px;
}
Works when items wrap.
Aligns multiple lines in the container.
.container {
align-content: space-around;
}
.parent {
display: flex;
}
.child {
display: flex;
flex-direction: column;
}
<nav class="nav">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
</nav>
.nav {
display: flex;
justify-content: space-between;
}
## 17 . Vertical Centering Without Hacks
.container { display: flex; justify-content: center; align-items: center; }
## 18 . Equal Height Columns
- Flexbox automatically stretches items in a row to equal height.
``
.columns {
display: flex;
}
.pricing {
display: flex;
justify-content: space-around;
}
.card {
flex: 1;
margin: 10px;
}
Forgetting flex-wrap can cause overflow.
Mixing margin auto and justify-content may cause unexpected spacing.
Not understanding flex-basis defaults.
##Conclusion
Flexbox revolutionized CSS layouts by removing the need for float-based systems and giving us a powerful, flexible, and intuitive layout mechanism. By mastering properties like flex-direction, justify-content, align-items, and flex, you can build responsive, modern layouts with minimal code and fewer media queries.
Senior Frontend Engineer
Mark is a passionate software developer and author with expertise in JavaScript and Python. He enjoys simplifying complex programming concepts and sharing practical coding tips.