CSS Flexbox: A Practical Guide to Building Flexible Layouts
Aug 9,2026

If you have worked with CSS for some time, you have probably written code like this:

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

Three CSS properties, and suddenly your content is perfectly centered.

This is one of the reasons Flexbox became so popular. It makes many layout problems that were difficult with older CSS techniques much easier to solve.

But Flexbox is more than just a way to center a <div>.

In this article, we'll understand what CSS Flexbox is, how it works, the most important properties, and how to use it in real-world UI development.


What is CSS Flexbox?

Flexbox, short for Flexible Box Layout, is a CSS layout system designed to arrange elements inside a container.

It is especially useful when you want to:

  • Arrange elements in a row or column

  • Align elements vertically or horizontally

  • Create responsive layouts

  • Distribute available space

  • Make elements grow or shrink automatically

  • Build navigation bars, cards, forms, buttons, and other UI components

The basic idea is simple:

You create a flex container, and its direct children become flex items.

For example:

<div class="container">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>
.container {
  display: flex;
}

That's enough to activate Flexbox.

By default, the children will be arranged horizontally.


Understanding the Flex Container and Flex Items

Consider this:

<div class="container">
  <div class="item">A</div>
  <div class="item">B</div>
  <div class="item">C</div>
</div>
.container {
  display: flex;
}

Here:

.container
   |
   +-- A
   +-- B
   +-- C

.container is the flex container.

A, B, and C are flex items.

Most Flexbox properties are divided into two categories:

Properties for the container

Examples:

display
flex-direction
justify-content
align-items
flex-wrap
align-content
gap

Properties for the items

Examples:

order
flex-grow
flex-shrink
flex-basis
flex
align-self

Understanding this distinction is important because a common mistake is trying to apply a container property to a child element.


The Main Axis and Cross Axis

Before learning Flexbox properties, there is one concept you should understand.

Flexbox works with two axes:

              Main Axis
        -------------------->

       +----------------------+
       |                      |
       |      Flex Items      |
       |                      |
       +----------------------+

              ^
              |
          Cross Axis

The main axis is the direction in which your flex items are arranged.

The cross axis is perpendicular to the main axis.

By default:

flex-direction: row;

So the main axis is horizontal.

If you change it to:

flex-direction: column;

the main axis becomes vertical.

This is important because:

  • justify-content works along the main axis

  • align-items works along the cross axis

This explains one of the most common Flexbox mistakes.


flex-direction

The flex-direction property controls the direction of the flex items.

The default value is:

flex-direction: row;

Example:

.container {
  display: flex;
  flex-direction: row;
}

The items appear like this:

A  B  C

row-reverse

.container {
  display: flex;
  flex-direction: row-reverse;
}

Now:

C  B  A

column

.container {
  display: flex;
  flex-direction: column;
}

Now the items are arranged vertically:

A
B
C

column-reverse

.container {
  display: flex;
  flex-direction: column-reverse;
}

The order becomes:

C
B
A

justify-content

justify-content controls how items are positioned along the main axis.

For example:

.container {
  display: flex;
  justify-content: center;
}

With the default row direction, this centers the items horizontally.

Common values

justify-content: flex-start;
justify-content: flex-end;
justify-content: center;
justify-content: space-between;
justify-content: space-around;
justify-content: space-evenly;

flex-start

justify-content: flex-start;
A B C

Items are placed at the beginning.

flex-end

justify-content: flex-end;
          A B C

Items move to the end.

center

justify-content: center;
       A B C

Items are centered.

space-between

justify-content: space-between;
A          B          C

The first item goes to the beginning and the last item goes to the end.

space-around

justify-content: space-around;

Space is added around each item.

space-evenly

justify-content: space-evenly;

The available space is distributed evenly between and around the items.


align-items

Now let's look at align-items.

This property controls alignment along the cross axis.

For the default row direction, the cross axis is vertical.

.container {
  display: flex;
  align-items: center;
}

This vertically centers the items.

Common values include:

align-items: flex-start;
align-items: flex-end;
align-items: center;
align-items: stretch;
align-items: baseline;

For example:

.container {
  display: flex;
  align-items: center;
}

This is commonly used when you want something like:

Icon   Text   Button

to be vertically aligned.


justify-content vs align-items

This is probably the most important thing to remember about Flexbox.

With:

flex-direction: row;
  • justify-content → horizontal/main axis

  • align-items → vertical/cross axis

For example:

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

This centers items both horizontally and vertically.

But if you change:

flex-direction: column;

the axes change.

Now:

  • justify-content → vertical

  • align-items → horizontal

So don't memorize:

justify = horizontal
align = vertical

Instead, remember:

justify-content works on the main axis.
align-items works on the cross axis.

That rule always works.


The Classic Vertical and Horizontal Centering Problem

One of the most common CSS requirements is:

Put an element exactly in the center of the screen.

Flexbox makes it very easy.

.container {
  min-height: 100vh;

  display: flex;
  justify-content: center;
  align-items: center;
}

HTML:

<div class="container">
  <h1>Hello World</h1>
</div>

That's it.

The heading will be centered horizontally and vertically.

This pattern is useful for:

  • Login pages

  • Loading screens

  • Empty states

  • Error pages

  • Landing page sections

  • Modal content


gap

The gap property adds space between flex items.

Instead of writing:

.item {
  margin-right: 20px;
}

you can use:

.container {
  display: flex;
  gap: 20px;
}

HTML:

<div class="container">
  <div>React</div>
  <div>Next.js</div>
  <div>Node.js</div>
</div>

This gives you:

React    Next.js    Node.js

gap is cleaner because you don't need to manage margins on individual elements.

You can also define different row and column gaps:

.container {
  display: flex;
  gap: 10px 20px;
}

The first value is the row gap and the second is the column gap.


flex-wrap

By default, Flexbox tries to keep all items on one line.

For example:

.container {
  display: flex;
}

If there are too many items, they may become squeezed.

You can allow items to move to another line using:

.container {
  display: flex;
  flex-wrap: wrap;
}

For example:

A  B  C  D
E  F  G  H

This is useful for responsive layouts such as:

  • Product cards

  • Tags

  • Buttons

  • Categories

  • Skill lists


flex-wrap: nowrap

This is the default:

flex-wrap: nowrap;

All items try to remain on a single line.


flex-wrap: wrap-reverse

You can also use:

flex-wrap: wrap-reverse;

Items wrap in the opposite cross-axis direction.

It's less common in everyday UI development, but useful to know.


flex-flow

flex-flow is a shorthand for:

flex-direction
flex-wrap

Instead of:

.container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

you can write:

.container {
  display: flex;
  flex-flow: row wrap;
}

align-content

align-content is often confused with align-items.

The difference becomes important when you have multiple flex lines.

For example:

.container {
  display: flex;
  flex-wrap: wrap;
  align-content: center;
}

align-content controls the spacing between multiple rows or columns created by wrapping.

On the other hand:

align-items

controls the alignment of items within a flex line.

A simple way to remember:

align-items → alignment of items
align-content → alignment of multiple flex lines

If your flex container has only one line, align-content usually won't have a visible effect.


Flex Item Properties

So far, we've mostly talked about the container.

Now let's look at properties that belong to individual flex items.


flex-grow

flex-grow determines how much an item can grow when extra space is available.

Example:

.item {
  flex-grow: 1;
}

Suppose we have:

<div class="container">
  <div class="item">A</div>
  <div class="item">B</div>
  <div class="item">C</div>
</div>
.item {
  flex-grow: 1;
}

The available space is distributed among the items.

You can also give different values:

.item1 {
  flex-grow: 1;
}

.item2 {
  flex-grow: 2;
}

.item3 {
  flex-grow: 1;
}

The second item gets twice as much of the available extra space compared with the first and third items.


flex-shrink

flex-shrink determines how much an item can shrink when there isn't enough space.

Example:

.item {
  flex-shrink: 1;
}

The default is:

flex-shrink: 1;

If you don't want an item to shrink:

.item {
  flex-shrink: 0;
}

This can be useful for elements such as icons or buttons where you don't want the item to become smaller than expected.


flex-basis

flex-basis defines the initial size of a flex item before the remaining space is distributed.

Example:

.item {
  flex-basis: 200px;
}

This tells Flexbox to start the item at approximately 200px along the main axis.

For example:

.item {
  flex-basis: 30%;
}

can be useful when building flexible layouts.


The flex Shorthand

Instead of writing:

.item {
  flex-grow: 1;
  flex-shrink: 1;
  flex-basis: 0;
}

you can write:

.item {
  flex: 1;
}

This is extremely common in real-world development.

For example:

.container {
  display: flex;
}

.item {
  flex: 1;
}

Now the available width is distributed equally between the items.

This is useful for creating equal-width columns.


Creating Equal-Width Cards

Suppose you have three cards:

<div class="cards">
  <div class="card">Card 1</div>
  <div class="card">Card 2</div>
  <div class="card">Card 3</div>
</div>

You can use:

.cards {
  display: flex;
  gap: 20px;
}

.card {
  flex: 1;
}

Now all three cards share the available space equally.

This pattern is common in dashboards and landing pages.


order

The order property allows you to change the visual order of flex items.

HTML:

<div class="container">
  <div class="item item1">A</div>
  <div class="item item2">B</div>
  <div class="item item3">C</div>
</div>

CSS:

.item1 {
  order: 3;
}

.item2 {
  order: 1;
}

.item3 {
  order: 2;
}

The visual result becomes:

B C A

The HTML order doesn't change. Only the visual order changes.

Be careful when using this for important content because visual order and DOM order can affect accessibility and keyboard navigation.


align-self

Sometimes you want one particular item to have different alignment from the others.

That's where align-self is useful.

Example:

.container {
  display: flex;
  align-items: center;
}

.special-item {
  align-self: flex-start;
}

The other items remain centered, while .special-item moves to the start of the cross axis.


A Real-World Navigation Bar

Let's build a simple navigation bar.

HTML:

<nav class="navbar">
  <div class="logo">MyApp</div>

  <div class="nav-links">
    <a href="#">Home</a>
    <a href="#">About</a>
    <a href="#">Contact</a>
  </div>

  <button>Login</button>
</nav>

CSS:

.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
}

.nav-links {
  display: flex;
  gap: 20px;
}

Here we are using Flexbox at two levels.

The navbar itself is a flex container:

.navbar {
  display: flex;
}

And the navigation links are another flex container:

.nav-links {
  display: flex;
}

This is very common in real applications.


Flexbox in Responsive Design

Flexbox becomes even more useful when building responsive layouts.

For example, you may want cards to appear in a row on desktop and a column on smaller screens.

.cards {
  display: flex;
  gap: 20px;
}

.card {
  flex: 1;
}

@media (max-width: 768px) {
  .cards {
    flex-direction: column;
  }
}

Desktop:

+--------+  +--------+  +--------+
| Card 1 |  | Card 2 |  | Card 3 |
+--------+  +--------+  +--------+

Mobile:

+--------+
| Card 1 |
+--------+

+--------+
| Card 2 |
+--------+

+--------+
| Card 3 |
+--------+

This is one of the simplest ways to make layouts responsive.


Flexbox vs CSS Grid

A common question is:

Should I use Flexbox or Grid?

The answer depends on the layout you're building.

Flexbox

Flexbox is mainly designed for one-dimensional layouts.

Think:

Row
----
A B C

or:

Column
------
A
B
C

It is great for:

  • Navigation bars

  • Buttons

  • Form controls

  • Toolbars

  • Aligning content

  • Small component layouts

CSS Grid

Grid is designed for two-dimensional layouts.

Think:

A  B  C
D  E  F
G  H  I

It is great for:

  • Page layouts

  • Dashboards

  • Complex card grids

  • Two-dimensional structures

You don't have to choose one and forget the other.

In real applications, Flexbox and Grid are often used together.


Common Flexbox Mistakes

1. Forgetting display: flex

This won't work:

.container {
  justify-content: center;
}

You need:

.container {
  display: flex;
  justify-content: center;
}

2. Confusing justify-content and align-items

Remember the axes.

With:

flex-direction: row;
justify-content → main axis
align-items     → cross axis

Don't memorize them as simply horizontal and vertical.


3. Using align-content when you need align-items

For a single row of items:

align-items: center;

is usually what you want.

align-content becomes useful when you have multiple flex lines created using wrapping.


4. Forgetting flex-wrap

If your items are overflowing on smaller screens, consider:

flex-wrap: wrap;

instead of forcing everything into one line.


5. Using margins for everything

Instead of:

.item {
  margin-right: 20px;
}

consider:

.container {
  display: flex;
  gap: 20px;
}

It usually results in cleaner CSS.


A Simple Flexbox Cheat Sheet

Container properties

display: flex;

flex-direction: row;
flex-direction: column;

justify-content: center;

align-items: center;

flex-wrap: wrap;

gap: 20px;

Item properties

flex: 1;

flex-grow: 1;

flex-shrink: 1;

flex-basis: 200px;

align-self: center;

order: 1;

If you're comfortable with these properties, you can solve most everyday Flexbox layout problems.


When Should You Use Flexbox?

Use Flexbox when your primary requirement is to arrange and align elements along one direction.

For example:

Logo   Navigation   Login

or:

Icon   Text   Button

or:

Card   Card   Card

or:

Label
Input
Button

If your layout is primarily about rows or columns, Flexbox is often a great choice.

If you need precise control over rows and columns at the same time, CSS Grid may be a better fit.


Final Thoughts

Flexbox is one of those CSS features that looks simple at first but becomes incredibly powerful once you understand how its layout model works.

The most important thing isn't memorizing every property.

Instead, understand these concepts:

  1. display: flex creates the flex container.

  2. flex-direction determines the main axis.

  3. justify-content works along the main axis.

  4. align-items works along the cross axis.

  5. flex-wrap allows items to move onto multiple lines.

  6. gap provides consistent spacing.

  7. flex: 1 is useful for distributing available space.

  8. Flexbox is mainly designed for one-dimensional layouts.

Once these concepts become familiar, many CSS layout problems become much easier to solve.

And the next time you find yourself adding five different margins just to align three elements, stop for a second.

There is a good chance Flexbox can do it more cleanly.