When I first started learning CSS, layouts felt confusing. Then I discovered Flexbox, and everything clicked. Flexbox makes it super easy to align, center, and space elements without using float or position hacks.
Letβs break it down in the simplest way possible π
What Is Flexbox?
Flexbox (Flexible Box Layout) is a CSS layout module that helps you arrange elements in a row or column β and control how they space, wrap, and align.
To use it, you simply declare:
.container {
display: flex;
}
That one line turns your element into a flex container.
Flexbox Direction
Use flex-direction to decide how items are arranged:
.container {
display: flex;
flex-direction: row; /* row | column | row-reverse | column-reverse */
}
row β side by side (default)
column β stacked vertically
Centering Elements (The Magic)
The most common use case β centering elements both horizontally and vertically:
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
- justify-content = horizontal alignment
- align-items = vertical alignment
Thatβs how you center anything with just 3 lines of CSS.
Spacing and Distribution
To space items evenly:
.container {
display: flex;
justify-content: space-between; /* or space-around / space-evenly */
}
This is perfect for navbars or headers.
Wrapping Items
When you have too many items in one line, let them wrap:
.container {
display: flex;
flex-wrap: wrap;
}
Now, your elements will move to a new line instead of squishing.
Want to Learn More?
I share simple, practical tutorials like this every week on LearnWithJoosy.com where I teach web development step-by-step with real-world projects. Join us today and take your carrier to the next level.
Top comments (0)