DEV Community

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

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 (1)

Collapse
 
hashbyt profile image
Hashbyt •

Great breakdown of Flexbox! Your clear explanations make it so accessible for beginners.