DEV Community

Taha Majlesi Pour
Taha Majlesi Pour

Posted on

Flexbox That Just Makes Sense

🤔 What is Flexbox, Really?

Let’s be honest — CSS layout used to suck. You’d try to center something and end up questioning all your life choices.

Then came Flexbox — a way to align, space, and organize elements like a pro, without weird hacks or magic rituals.

But if you’ve ever looked at display: flex and still felt confused… this is for you.


đź§  The Core Idea

Flexbox is like putting stuff in a row or a column, and giving the container smart rules for how those items should behave.

Here’s the mental image:

Imagine a row of kids in a line.

Some are taller, some are shorter.

Some want more space. Some don’t care.

Flexbox is the teacher that organizes them.


🛠️ Basic Setup

Let’s start with a basic container:

<div class="flex-container">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>
Enter fullscreen mode Exit fullscreen mode

And your CSS:

.flex-container {
  display: flex;
}
Enter fullscreen mode Exit fullscreen mode

Boom. You’re in flex mode.

đź§­ Direction: Row or Column?

By default, flex-direction is row. That means your items will line up horizontally.

But if you want them stacked vertically?

.flex-container {
  display: flex;
  flex-direction: column;
}
Enter fullscreen mode Exit fullscreen mode

Think of flex-direction like telling the kids,

“Line up side-by-side” or “Line up top-to-bottom.”

🎯 Justify Content vs Align Items

This is where most people get lost, so let’s break it down super simply:

✅ justify-content → controls main axis (row/column direction)
Examples:

justify-content: flex-start; /* left/top */
justify-content: center;     /* center */
justify-content: space-between; /* space between items */
Enter fullscreen mode Exit fullscreen mode

✅ align-items → controls the cross axis
Examples:

align-items: flex-start;  /* top */
align-items: center;      /* middle */
align-items: flex-end;    /* bottom */
Enter fullscreen mode Exit fullscreen mode

🔍 Real-World Example: Center Anything

Here’s how you center anything, both vertically and horizontally:

.flex-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* Full screen height */
}
Enter fullscreen mode Exit fullscreen mode

🎉 Works every time. No hacks. No absolute positioning. Just clean flex.

đź§© Bonus: Flexible Items

Want items to grow or shrink?

.item {
  flex: 1; /* Take equal space */
}
Enter fullscreen mode Exit fullscreen mode

Or go deeper with:

.item {
  flex: 2; /* This one takes double space */
}
Enter fullscreen mode Exit fullscreen mode

đź’¬ Final Thoughts

If you’ve been fighting with layout issues, Flexbox is your new best friend.

It’s not just a tool — it’s a mindset. Once you “get it,” you’ll stop googling “how to center a div” at 2AM.

And hey, if you want a follow-up guide on Flexbox vs Grid, or how to use Tailwind’s flex classes, just leave a comment — I got you 👇

🧑‍💻 Written by Taha Majlesi

Top comments (1)

Collapse
 
tahamjp profile image
Taha Majlesi Pour

🙌 Thanks for reading! Follow me for more front-end tips 💡