CSSFlexboxWeb DevelopmentBeginner GuideInteractive Tutorialchaicodechaicodewebdevcohort

CSS Flexbox: A Complete Interactive Guide for Beginners

Piyush Kumar
10 min read
CSS Flexbox: A Complete Interactive Guide for Beginners

Introduction

In the world of web development, arranging elements on a page has always been a challenge. Before Flexbox, developers used floats, tables, and all kinds of positioning hacks to create layouts. These methods were frustrating, hard to maintain, and often broke on different screen sizes.

To solve these problems, CSS introduced a powerful layout system called Flexbox (Flexible Box Layout). In this blog, we will understand what Flexbox is, why it exists, what problems it solves, how the container and item properties work, and how you can use it to build responsive layouts easily.

First, let's understand what is Flexbox and why it is needed.

What is Flexbox and Why It Exists

Flexbox is a one-dimensional layout method in CSS. This means it handles layout in one direction at a time-either as a row (horizontal) or as a column (vertical).

Before Flexbox, if you wanted to center a <div> both horizontally and vertically, you needed complex calculations or hacks. If you wanted items to share space equally, you had to do math with percentages. If you wanted items to wrap nicely on smaller screens, good luck!

Flexbox solves all these problems. It gives us simple, intuitive controls for:

  • Aligning items horizontally and vertically
  • Distributing space between elements automatically
  • Creating responsive layouts without complex calculations
  • Reordering elements visually without changing HTML

That is why Flexbox is used in almost every modern website today.

The Two Key Players in Flexbox

In Flexbox, there are always two main players working together.

Flex Container: This is the parent element. You make any element a flex container by adding display: flex to it.

Flex Items: These are the direct children of the flex container. Once the parent becomes a flex container, all its direct children automatically become flex items.

/* This creates a flex container */
.container {
  display: flex;
}

/* All direct children become flex items automatically */

You can think of it like a bookshelf (container) and books (items). The bookshelf decides how the books are arranged, whether they line up left to right, stack vertically, or spread out evenly.

Getting Started: Your First Flex Container

To enable Flexbox, simply add display: flex to any element. That's it!

.container {
  display: flex;
}

Once you add this single line, all children of this container become flex items, and you can start using all the powerful flexbox properties.

A
B
C

A basic flex container with three items arranged in a row (the default behavior)

Notice how the items automatically line up in a row? That is the default flex-direction: row at work. By default, Flexbox arranges items horizontally from left to right.

Understanding Main Axis vs Cross Axis

Before diving into properties, you need to understand a crucial concept: axes.

Flexbox operates on two axes:

AxisDefault DirectionWhat It Controls
Main AxisHorizontal (left → right)How items are distributed
Cross AxisVertical (top → bottom)How items are aligned

Main Axis vs Cross Axis

The main axis runs in the direction defined by flex-direction. The cross axis is perpendicular to it.

Main Axis (Row)

A
B
C

Main Axis (Column)

A
B
C

Understanding these axes is key because:

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

Container Properties (Parent)

Now let's explore each property that you can apply to the flex container. Each section includes an interactive demo-be sure to play with them!

1. flex-direction

This property defines the direction of the main axis, which determines how flex items are laid out in the container.

flex-direction

Defines the direction of the main axis. Items flow along this axis.

A
B
C
flex-direction: row;

Values explained:

  • row (default): Items are placed left to right
  • row-reverse: Items are placed right to left
  • column: Items are placed top to bottom
  • column-reverse: Items are placed bottom to top
.container {
  display: flex;
  flex-direction: row; /* or row-reverse, column, column-reverse */
}

🧠 Mental Model: Think of flex-direction as deciding whether you're arranging books on a shelf (row) or stacking them on top of each other (column).

2. justify-content

This property aligns items along the main axis. It's how you control the horizontal spacing when using flex-direction: row.

justify-content

Aligns items along the main axis (horizontal for row, vertical for column).

A
B
C
justify-content: flex-start;

Values explained:

  • flex-start (default): Items bunch at the start
  • flex-end: Items bunch at the end
  • center: Items are centered
  • space-between: Equal space between items (no space at edges)
  • space-around: Equal space around each item
  • space-evenly: Equal space everywhere
.container {
  display: flex;
  justify-content: center; /* Or any other value */
}

💡 Use Case: justify-content: center is perfect for centering a navigation menu. space-between is great for footers with elements at each edge.

3. align-items

This property aligns items along the cross axis. When using flex-direction: row, this controls vertical alignment.

align-items

Aligns items along the cross axis (vertical for row, horizontal for column).

A
B
C
align-items: stretch;

Values explained:

  • stretch (default): Items stretch to fill the container
  • flex-start: Items align to the top (for row direction)
  • flex-end: Items align to the bottom
  • center: Items are vertically centered
  • baseline: Items align based on their text baseline
.container {
  display: flex;
  align-items: center; /* Vertical centering! */
}

🎯 The Classic Centering Solution: Combine justify-content: center with align-items: center to perfectly center any element!

4. flex-wrap

By default, flex items try to fit on one line. flex-wrap lets items wrap onto multiple lines when needed.

flex-wrap

Controls whether items wrap to new lines when they overflow the container.

A
B
C
flex-wrap: nowrap;

Values explained:

  • nowrap (default): All items stay on one line (may shrink or overflow)
  • wrap: Items wrap to the next line when needed
  • wrap-reverse: Items wrap to the line above
.container {
  display: flex;
  flex-wrap: wrap; /* Items will wrap to new lines */
}

📱 Responsive Design Tip: Use flex-wrap: wrap for grids that automatically adapt to different screen sizes!

5. gap

The gap property adds space between flex items. It's much cleaner than using margins!

.container {
  display: flex;
  gap: 16px; /* Adds 16px space between all items */
}

With and Without Gap

Compare how gap creates clean spacing between items

Without gap

A
B
C

With gap: 16px

A
B
C

You can also specify different values for row and column gaps:

.container {
  display: flex;
  row-gap: 20px; /* Vertical spacing */
  column-gap: 10px; /* Horizontal spacing */

  /* Or use the shorthand: */
  gap: 20px 10px; /* row-gap column-gap */
}

6. align-content

This property is similar to align-items, but it aligns lines of items rather than individual items. It only has an effect when flex-wrap: wrap is enabled and there are multiple lines.

align-content

Aligns multiple lines of flex items along the cross axis. Only works when flex-wrap is enabled.

A
B
C
D
E
F
G
H
I
align-content: flex-start;

Values explained:

  • flex-start: Lines packed at the start
  • flex-end: Lines packed at the end
  • center: Lines packed at the center
  • space-between: Equal space between lines
  • space-around: Equal space around lines
  • stretch (default): Lines stretch to fill the container
.container {
  display: flex;
  flex-wrap: wrap;
  align-content: center; /* Center all the rows */
}

🎯 Key Difference: align-items aligns items within their row, while align-content aligns the rows themselves within the container.

Item Properties (Children)

Now let's look at properties that apply to individual flex items. These give you fine-grained control over specific items.

1. flex-grow

This property determines how much an item can grow relative to other items when there's extra space in the container.

flex-grow

Determines how much an item can grow relative to other items when there's extra space.

0
0
0
A
B
C
.item-a { flex-grow: 0; }.item-b { flex-grow: 0; }.item-c { flex-grow: 0; }

How it works:

  • Default is 0 (items don't grow beyond their content size)
  • If all items have flex-grow: 1, they share space equally
  • If one item has flex-grow: 2 and others have 1, it gets twice the extra space
.item {
  flex-grow: 1; /* This item will grow to fill available space */
}

🍰 Think of it like cake: If there's leftover cake and three people each have flex-grow: 1, they each get an equal slice. But if one person has flex-grow: 2, they get double!

2. flex-shrink

This property determines how much an item can shrink relative to other items when there's not enough space.

flex-shrink

Determines how much an item can shrink relative to other items when space is limited.

0
0
0
A
B
C
.item-a { flex-shrink: 0; }.item-b { flex-shrink: 0; }.item-c { flex-shrink: 0; }

How it works:

  • Default is 1 (items can shrink equally)
  • flex-shrink: 0 means the item won't shrink (it keeps its size)
  • Higher values mean the item shrinks more than others
.item {
  flex-shrink: 0; /* This item will NOT shrink, even if space is tight */
}

🛡️ Protect important content: Set flex-shrink: 0 on items that should never get smaller, like a logo!

3. flex-basis

This property sets the initial size of a flex item before any growing or shrinking happens.

flex-basis

Sets the initial main size of an item before growing or shrinking.

auto
auto
auto
A
B
C
.item-a { flex-basis: auto; }.item-b { flex-basis: auto; }.item-c { flex-basis: auto; }

Values:

  • auto (default): Uses the item's width/height
  • Any length value: 100px, 50%, 20rem, etc.
  • 0: Item has no initial size (relies entirely on grow/shrink)
.item {
  flex-basis: 200px; /* Start at 200px, then grow/shrink from there */
}

4. The flex Shorthand

Instead of writing three separate properties, use the flex shorthand:

.item {
  flex: 1 1 auto; /* flex-grow flex-shrink flex-basis */
}

/* Common patterns: */
.item {
  flex: 1;
} /* Grow and shrink equally, basis auto */
.item {
  flex: 0 0 200px;
} /* Don't grow/shrink, fixed 200px */
.item {
  flex: 1 0 0;
} /* Grow, don't shrink, start from 0 */

5. order

This property changes the visual order of flex items without modifying the HTML.

order

Changes the visual order of items without changing the HTML order.

0
0
0
A
B
C
.item-a { order: 0; }.item-b { order: 0; }.item-c { order: 0; }

How it works:

  • Default is 0
  • Items are sorted by order value, lowest first
  • Items with the same order value appear in their HTML order
.item-first {
  order: -1; /* Appears first */
}

.item-last {
  order: 99; /* Appears last */
}

⚠️ Accessibility Note: Screen readers still read items in HTML order. Use order for visual adjustments, not to restructure content meaning.

6. align-self

This property overrides the container's align-items for a specific item.

align-self

Overrides the container's align-items for a specific item.

auto
auto
auto
A
B
C
.item-a { align-self: auto; }.item-b { align-self: auto; }.item-c { align-self: auto; }

Values:

  • auto (default): Inherits from container's align-items
  • flex-start, flex-end, center, stretch, baseline: Same as align-items
.container {
  align-items: center; /* All items centered */
}

.special-item {
  align-self: flex-start; /* This one aligns to the top */
}

Full Interactive Playground

Now that you understand all the properties, here's a complete playground where you can experiment with everything at once!

🎮 Flexbox Playground

Combine all the properties and see how they work together. Try creating different layouts!

1
2
3
4
5

Generated CSS

.container {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: stretch;
  flex-wrap: nowrap;
  gap: 8px;
}

Real-World Layout Examples

Let's put everything together with some practical layouts you'll use all the time.

Centering (The Holy Grail!)

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* Full viewport height */
}
A
B
C

Perfect centering with just 3 lines of CSS!

Navigation Bar

.nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}
A
B
C

Logo on the left, links on the right - classic navigation pattern

Card Grid with Wrapping

.grid {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.card {
  flex: 1 1 300px; /* Grow, shrink, min 300px */
}

Footer with Multiple Sections

.footer {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  gap: 32px;
}
A
B
C

Evenly spaced footer sections that wrap on smaller screens

Common Flexbox Patterns Cheat Sheet

Here's a quick reference for the most common flexbox patterns:

What You WantCSS
Center everythingjustify-content: center; align-items: center;
Space items evenlyjustify-content: space-between;
Stack verticallyflex-direction: column;
Allow wrappingflex-wrap: wrap;
Item doesn't shrinkflex-shrink: 0;
Item fills all spaceflex: 1;
Equal-width columnsAll items: flex: 1;
Fixed + flexibleFixed: flex: 0 0 200px; Flexible: flex: 1;

Browser Support

Great news! Flexbox has excellent browser support:

  • ✅ Chrome, Edge, Firefox, Safari (all modern versions)
  • ✅ iOS Safari, Android Chrome
  • ✅ IE 11 (with some limitations and prefixes)

You can confidently use Flexbox in production today!

Summary

Congratulations! 🎉 You now have a solid understanding of CSS Flexbox. Here's what we covered:

Container Properties (Parent)

  • display: flex - Enables flexbox
  • flex-direction - Main axis direction
  • justify-content - Main axis alignment
  • align-items - Cross axis alignment
  • align-content - Multi-line alignment (with wrap)
  • flex-wrap - Allow line wrapping
  • gap - Spacing between items

Item Properties (Children)

  • flex-grow - How much to grow
  • flex-shrink - How much to shrink
  • flex-basis - Initial size
  • flex - Shorthand for grow/shrink/basis
  • order - Visual order
  • align-self - Override alignment

Practice Challenge

Want to test your skills? Try recreating these layouts using Flexbox:

  1. A centered login form
  2. A navigation bar with logo left and links right
  3. A responsive card grid (3 columns on desktop, 1 on mobile)
  4. A sticky footer at the bottom of the page

Further Reading

Happy coding!

Remember: the best way to learn is by doing. Go build something amazing with Flexbox! 🚀