Skip to content

CSS Flexbox Layout Not Working Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about CSS Flexbox Layout Not Working Fix. We cover key concepts, practical examples, and best practices.

The Problem

You set display: flex on a container but the child elements do not align horizontally, overflow the container, or collapse unexpectedly. Flexbox depends on specific property combinations that are easy to miss.

Quick Fix

Step 1: Verify display flex is applied

The container must have display: flex or display: inline-flex:

/* Wrong — no flex container */
.container {
    width: 100%;
}

/* Right */
.container {
    display: flex;
    width: 100%;
}

Expected output: Children align in a row by default.

Step 2: Set the correct flex-direction

Flexbox defaults to row. Use column for vertical layouts:

.container {
    display: flex;
    /* Wrong for vertical layout — missing direction */
    /* Right */
    flex-direction: column;
}

Expected output: Children stack vertically.

Step 3: Prevent overflow with flex-wrap

By default, flex items shrink to fit. To allow wrapping:

.container {
    display: flex;
    /* Wrong — items overflow on small screens */
    /* Right */
    flex-wrap: wrap;
}
.item {
    flex: 0 0 300px;
}

Expected output: Items exceeding container width wrap to the next line.

Step 4: Align items correctly

Use align-items for cross-axis and justify-content for main-axis alignment:

.container {
    display: flex;
    /* Wrong — items stretch to full height */
    align-items: center;
    justify-content: space-between;
}

Expected output: Items are vertically centered and evenly spaced.

Step 5: Set flex basis for responsive sizing

.item {
    /* Wrong — fixed width ignores flex context */
    width: 200px;
    /* Right */
    flex: 1 1 200px;
}

Expected output: Items grow and shrink relative to the container.

Prevention

  • Always check that display: flex is on the parent, not the children
  • Use flex: shorthand instead of individual width values for responsive layouts
  • Set min-height: 0 on nested flex containers to prevent overflow
  • Test alignment with browser DevTools Flexbox inspector

Common Mistakes with flexbox not working

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. Mixing let bindings with <- bindings in do notation, producing type errors

These mistakes appear frequently in real-world CSS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why are my flex items not wrapping?

Add flex-wrap: wrap to the container. The default nowrap forces items to stay on one line, causing overflow or shrinking.

What is the difference between align-items and align-content?

align-items aligns items within a single row. align-content distributes space between multiple rows (only works with flex-wrap: wrap). Use both when you have wrapped items.

Why does flex shorthand not work as expected?

flex: 1 expands to flex-grow: 1, flex-shrink: 1, flex-basis: 0%. If you want a specific starting size, use flex: 1 1 200px. The shorthand overrides individual flex-grow, flex-shrink, and flex-basis values.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro