CodeNewbie Community 🌱

Cover image for CSS Flexbox Made Simple: Everything You Need
Joseph Jossy
Joseph Jossy

Posted on

CSS Flexbox Made Simple: Everything You Need

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;
}
Enter fullscreen mode Exit fullscreen mode

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 */
}
Enter fullscreen mode Exit fullscreen mode
  • 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;
}
Enter fullscreen mode Exit fullscreen mode
  • 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 */
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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)