Skip to main content

Last updated: March 13, 2026

How to Fix Flexbox Not Working in CSS

Complete troubleshooting guide for flexbox issues. Learn to fix common problems with display flex, alignment, direction, shrinking, and wrapping.

Why Isn't My Flexbox Working?

Flexbox is powerful, but it has specific rules that aren't always intuitive. The most common issues are forgetting display: flex, confusing align-items with justify-content, or running into the min-width: auto gotcha that prevents items from shrinking.

This guide covers the 7 most common flexbox problems and their solutions, with before/after code examples you can copy and adapt.

Common Causes

Before diving into solutions, here are the typical reasons why flexbox doesn't work as expected.

display: flex Not Applied

The most basic issue - you need display: flex on the container, not the children. Check computed styles in DevTools.

Wrong flex-direction

flex-direction: column swaps the axes. What worked horizontally won't work vertically without adjusting your properties.

Confused align-items and justify-content

These control different axes. justify-content = main axis (horizontal by default), align-items = cross axis (vertical by default).

min-width Preventing Shrinking

Flex items have min-width: auto by default, stopping them from shrinking below content width. Set min-width: 0 to fix.

Solutions

1

Solution 1: Apply display: flex to Container

Problem

You're applying flexbox properties but forgot to set display: flex on the parent container, or it's being overridden by another style.

Before (Not Working)

.container {
  /* Missing display: flex! */
  justify-content: center;
  align-items: center;
}

After (Fixed)

.container {
  display: flex; /* Must be present! */
  justify-content: center;
  align-items: center;
}

Why This Works

Flexbox only works when you explicitly set display: flex on the parent container. Properties like justify-content and align-items do nothing without it. Always check DevTools computed styles to ensure display: flex is actually applied and not overridden.

2

Solution 2: Understand flex-direction

Problem

Your items are stacking vertically when you want horizontal, or vice versa. flex-direction controls whether items flow in rows or columns.

Before (Not Working)

.container {
  display: flex;
  /* Default: flex-direction: row (horizontal) */
  justify-content: center; /* Centers horizontally */
  align-items: center; /* Centers vertically */
}

After (Fixed)

/* For vertical layout (column) */
.container {
  display: flex;
  flex-direction: column; /* Items stack vertically */
  justify-content: center; /* Now centers VERTICALLY */
  align-items: center; /* Now centers HORIZONTALLY */
}

/* Note: Axes swap with flex-direction! */

Why This Works

flex-direction: row (default) makes items flow horizontally. flex-direction: column makes them flow vertically. Crucially, this SWAPS what justify-content and align-items do. Main axis = direction of flow, cross axis = perpendicular to flow. When in doubt, use a visual reference.

3

Solution 3: Fix align-items vs justify-content Confusion

Problem

You're using justify-content to center vertically, or align-items to center horizontally. They control different axes.

Before (Not Working)

/* Wrong for centering both ways */
.container {
  display: flex;
  justify-content: center; /* Only centers horizontally */
}
/* Items are horizontally centered but not vertically */

After (Fixed)

/* Correct for centering both ways */
.container {
  display: flex;
  justify-content: center; /* Centers on main axis (horizontal) */
  align-items: center; /* Centers on cross axis (vertical) */
  height: 100vh; /* Need height for vertical centering */
}

Why This Works

Think of it as 'justify = justifying text in a line' (horizontal by default) and 'align = aligning to the top/bottom' (vertical by default). For centering both ways, you need BOTH properties. Also remember: vertical centering requires the container to have a defined height.

4

Solution 4: Fix Flex Items Not Shrinking (min-width Issue)

Problem

Your flex items overflow the container instead of shrinking, or text doesn't wrap. This is the infamous min-width: auto default.

Before (Not Working)

.container {
  display: flex;
}
.item {
  flex: 1; /* Should shrink, but doesn't! */
}
/* Item with long text doesn't shrink below text width */

After (Fixed)

.container {
  display: flex;
}
.item {
  flex: 1;
  min-width: 0; /* Allow shrinking below content width */
}
/* Now item can shrink and text wraps properly */

/* Alternative for all children */
.container {
  display: flex;
}
.container > * {
  min-width: 0; /* Apply to all flex items */
}

Why This Works

Flex items have min-width: auto by default, which prevents them from shrinking below their content's natural width. This causes overflow with long text, images, or nested flexboxes. Setting min-width: 0 tells the flex item it's allowed to shrink smaller than its content, enabling proper wrapping and shrinking. This is one of the most common flexbox gotchas.

5

Solution 5: Understand flex-grow, flex-shrink, flex-basis

Problem

You're using flex: 1 without understanding what it does, or flex items aren't behaving as expected.

Before (Not Working)

.item {
  flex: 1; /* What does this actually do? */
}

After (Fixed)

/* Understanding the shorthand */
.item {
  flex: 1;
  /* Expands to: */
  flex-grow: 1;   /* Grow to fill space */
  flex-shrink: 1; /* Can shrink if needed */
  flex-basis: 0;  /* Start from 0 width */
}

/* Common patterns: */
.grow-only {
  flex: 1 0 auto; /* Grow, don't shrink, auto basis */
}
.no-shrink {
  flex: 0 0 auto; /* Fixed size, no grow/shrink */
}
.grow-shrink-equal {
  flex: 1 1 0; /* Same as flex: 1 */
}

Why This Works

flex is shorthand for flex-grow (how much it grows to fill space), flex-shrink (can it shrink?), and flex-basis (starting size before growing/shrinking). flex: 1 means 'grow to fill space equally, allow shrinking, start from zero width'. flex: 1 0 auto means 'grow but don't shrink, start from content width'. Understanding these helps you fix unexpected sizing.

6

Solution 6: Fix Items Not Wrapping with flex-wrap

Problem

Your flex items are shrinking or overflowing instead of wrapping to a new line.

Before (Not Working)

.container {
  display: flex;
  /* Default: flex-wrap: nowrap */
}
/* Items get squished on narrow screens */

After (Fixed)

.container {
  display: flex;
  flex-wrap: wrap; /* Items wrap to new line */
}
.item {
  flex: 0 0 200px; /* Fixed width, wraps when needed */
  /* Or use min-width: */
  min-width: 200px;
}

Why This Works

By default, flexbox keeps all items on one line (flex-wrap: nowrap), shrinking them to fit. If you want items to move to a new line when the container is too narrow, use flex-wrap: wrap. This is essential for responsive designs where items should maintain a minimum size. Combine with min-width or fixed flex-basis to control when wrapping happens.

7

Solution 7: Align One Item Differently with align-self

Problem

You want one flex item aligned differently from the others, but changing align-items affects all items.

Before (Not Working)

.container {
  display: flex;
  align-items: flex-start; /* All items at top */
}
/* Want one item at bottom, but can't change align-items */

After (Fixed)

.container {
  display: flex;
  align-items: flex-start; /* Most items at top */
}
.special-item {
  align-self: flex-end; /* This one at bottom */
}
/* Or: */
.centered-item {
  align-self: center; /* This one centered */
}

Why This Works

align-self allows individual flex items to override the container's align-items. This is perfect for having one item positioned differently - like a logo on the left, nav items centered, and a button on the right. Valid values: flex-start, flex-end, center, baseline, stretch (same as align-items options).

Quick Troubleshooting Checklist

When flexbox isn't working, run through this checklist to identify the issue.

1

Is display: flex on the parent container?

Check DevTools computed styles - it must be present

2

Are you using the right flex-direction?

row = horizontal (default), column = vertical

3

Are align-items and justify-content on the container?

These properties go on the parent, not the children

4

Did you confuse align-items and justify-content?

justify = main axis (horizontal by default), align = cross axis (vertical)

5

Are items not shrinking? Check min-width.

Set min-width: 0 on flex items to allow shrinking below content width

6

Need items to wrap? Use flex-wrap: wrap

Default is nowrap - items stay on one line and shrink

Understanding Flexbox Axes

The key to flexbox is understanding main axis vs cross axis. These change based on flex-direction.

flex-direction: row (default)

Main Axis
Cross Axis
  • justify-content = horizontal
  • align-items = vertical

flex-direction: column

Main Axis
Cross Axis
  • justify-content = vertical
  • align-items = horizontal

Frequently Asked Questions

Why isn't my flexbox working?

The most common reason is that display: flex is not applied to the parent container, or it's being overridden. Check in DevTools that the container has display: flex in its computed styles. Also verify you're applying alignment properties to the right element - align-items and justify-content go on the flex container, not the children.

What's the difference between align-items and justify-content?

justify-content aligns items along the main axis (horizontally by default), while align-items aligns along the cross axis (vertically by default). If you have flex-direction: column, these axes swap. Remember: justify = main axis, align = cross axis.

Why won't my flex items shrink?

Flex items have min-width: auto by default, which prevents them from shrinking below their content's natural width. To allow shrinking, set min-width: 0 on the flex item. This is the most common flexbox gotcha with text content or images.

Do I need to set flex-wrap for flexbox to work?

No, flex-wrap is optional. By default, flex items stay in a single line (flex-wrap: nowrap) and will shrink to fit. Only use flex-wrap: wrap if you want items to move to a new line when the container is too narrow. Without it, flexbox will work but items might overflow or shrink unexpectedly.

Why isn't gap working in my flexbox?

The gap property for flexbox has excellent support in modern browsers (Chrome 84+, Firefox 63+, Safari 14.1+). If it's not working, you might be using an older browser. For older browser support, use margin on flex items instead (e.g., margin-right for horizontal layouts).