CSS

Mastering CSS Flexbox in 10 Mins 📦

Published on Dec 8, 2025 by MSMAXPRO • 10 min read

CSS Flexbox

Before Flexbox, laying out a website was a nightmare. We used float, position: absolute, and weird hacks like clearfix. It was messy.

Then came Flexbox (Flexible Box Layout). It changed everything. It allows you to align items in rows or columns, handle spacing automatically, and fix alignment issues instantly.

1. The Two Main Players

In Flexbox, there are only two things you need to understand:

  • The Container (Parent): The box that holds everything.
  • The Items (Children): The elements inside the box.
.container {
  display: flex; /* This turns on the magic */
}

2. Controlling the Direction

By default, display: flex puts everything in a row (left to right). But you can change that.

  • flex-direction: row; (Default: Horizontal)
  • flex-direction: column; (Vertical: Top to Bottom)

3. Justify Content (Main Axis Alignment)

This property controls alignment along the main axis (usually horizontal). Think of it as "Left/Right/Center" alignment.

.container {
  display: flex;
  justify-content: center; /* Options: flex-start, flex-end, space-between, space-around */
}

Visual Example (Space Between):

Item 1
Item 2
Item 3

4. Align Items (Cross Axis Alignment)

This controls alignment along the cross axis (usually vertical). This fixes the famous "How do I center a div vertically?" problem.

.container {
  display: flex;
  height: 200px;
  align-items: center; /* Options: flex-start, flex-end, stretch */
}

5. Real World Example: A Navbar

Let's build a classic navigation bar using Flexbox. Logo on the left, Links on the right.

<nav class="navbar">
  <div class="logo">MyLogo</div>
  <ul class="links">
    <li>Home</li>
    <li>About</li>
  </ul>
</nav>

/* CSS */
.navbar {
  display: flex;
  justify-content: space-between; /* Pushes logo left, links right */
  align-items: center; /* Vertically centers them */
  padding: 20px;
}

6. The Flexbox Cheatsheet

Save this for later usage:

  • display: flex; -> Activates Flexbox.
  • justify-content: center; -> Centers horizontally.
  • align-items: center; -> Centers vertically.
  • flex-direction: column; -> Stacks items vertically.
  • flex-wrap: wrap; -> Allows items to go to the next line if space runs out.

Conclusion

Flexbox is powerful for 1-dimensional layouts (rows OR columns). For 2-dimensional layouts (rows AND columns together), you should look into CSS Grid (which we will cover in the next blog!).

Mastered Flexbox?

← Back to All Blogs

Pro Tip

Always remember that the best way to master these concepts is to build something real. Tutorials are great, but the real learning starts when you try to solve bugs on your own!

Diving Deeper: Best Practices

To truly excel and write code that stands the test of time, you need to follow best practices. Professional developers always ensure their code is:

  • Readable: Code is read ten times more than it is written. Use clear variable names.
  • Modular: Break down large functions into smaller, reusable components.
  • Documented: Leave comments explaining why something was done, not what was done.
MSMAXPRO

Written by MSMAXPRO

Professional web developer and security enthusiast crafting modern digital experiences. Follow me for more tutorials and roadmaps.