CSS Flexbox is one of the most powerful layout tools in modern web development. If you've ever struggled with aligning elements or creating responsive designs, Flexbox is here to help. In this beginner-friendly guide, you'll learn everything you need to know to start using Flexbox today.
What is CSS Flexbox?
Flexbox (Flexible Box Layout) is a CSS layout model that makes it easy to design flexible and responsive layouts. Instead of using floats or positioning, Flexbox lets you control how elements are arranged, aligned, and distributed in a container.
Why Use Flexbox?
- Easy alignment - Center elements vertically and horizontally with ease
- Responsive design - Layouts adapt automatically to different screen sizes
- Simple syntax - Cleaner code compared to old CSS techniques
- Flexible ordering - Change element order without touching HTML
Getting Started with Flexbox
To use Flexbox, you need a flex container (parent element) and flex items (child elements). Here's the basic setup:
<!-- HTML -->
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
<!-- CSS -->
.container {
display: flex;
}
That's it! By adding display: flex to the container,
all child elements become flex items that you can easily control.
Essential Flexbox Properties
1. flex-direction
Controls the direction of flex items:
.container {
display: flex;
flex-direction: row; /* Default: left to right */
}
/* Other values */
flex-direction: row-reverse; /* Right to left */
flex-direction: column; /* Top to bottom */
flex-direction: column-reverse; /* Bottom to top */
2. justify-content
Aligns items along the main axis (horizontally by default):
.container {
display: flex;
justify-content: center; /* Center items */
}
/* Other values */
justify-content: flex-start; /* Align to start */
justify-content: flex-end; /* Align to end */
justify-content: space-between; /* Equal space between */
justify-content: space-around; /* Equal space around */
justify-content: space-evenly; /* Perfect equal spacing */
3. align-items
Aligns items along the cross axis (vertically by default):
.container {
display: flex;
align-items: center; /* Center vertically */
}
/* Other values */
align-items: flex-start; /* Align to top */
align-items: flex-end; /* Align to bottom */
align-items: stretch; /* Stretch to fill (default) */
align-items: baseline; /* Align text baselines */
4. flex-wrap
Controls whether items wrap to new lines:
.container {
display: flex;
flex-wrap: wrap; /* Items wrap to next line */
}
/* Other values */
flex-wrap: nowrap; /* All items on one line (default) */
flex-wrap: wrap-reverse; /* Wrap in reverse order */
Practical Examples
Example 1: Center Everything Perfectly
.center-container {
display: flex;
justify-content: center; /* Horizontal center */
align-items: center; /* Vertical center */
height: 100vh; /* Full viewport height */
}
<div class="center-container">
<h1>Perfectly Centered!</h1>
</div>
Example 2: Responsive Navigation Menu
.nav {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem;
}
.nav-links {
display: flex;
gap: 2rem;
}
<nav class="nav">
<div class="logo">MyLogo</div>
<div class="nav-links">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
</nav>
Example 3: Card Grid Layout
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
}
.card {
flex: 1 1 300px; /* Grow, shrink, base width */
padding: 1.5rem;
border: 1px solid #ddd;
border-radius: 8px;
}
<div class="card-grid">
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
</div>
Common Flexbox Mistakes to Avoid
-
Forgetting
display: flex- The container must have this property -
Confusing axes - Remember:
justify-contentis for the main axis,align-itemsis for the cross axis -
Not using
gap- Usegapproperty instead of margins for spacing - Overthinking it - Start simple and add complexity as needed
Quick Reference Cheat Sheet
/* Container Properties */
display: flex;
flex-direction: row | column;
justify-content: center | space-between | space-around;
align-items: center | flex-start | flex-end;
flex-wrap: wrap | nowrap;
gap: 1rem; /* Spacing between items */
/* Item Properties */
flex: 1; /* Grow to fill space */
align-self: center; /* Override container alignment */
order: 2; /* Change visual order */
Next Steps
Now that you understand Flexbox basics, practice by building:
- A responsive navigation bar
- A photo gallery grid
- A card-based layout
- A footer with multiple columns
The best way to learn Flexbox is by doing. Start with simple layouts and gradually increase complexity. Before you know it, Flexbox will become your go-to tool for responsive design!
Pro tip: Use browser DevTools to experiment with Flexbox properties in real-time. Modern browsers have excellent Flexbox debugging tools.