HTML & CSS - Mastery / Flexbox Mastery

Flex-wrap

⼓ Flex Wrap

By default, Flexbox tries to fit all flex items into one line. But what if there isn’t enough space?

The flex-wrap property allows items to wrap onto multiple lines, improving responsiveness and preventing layout breaks.


🧱 Master Code block (This code block will be used for further examples)

<style>
    .flex-container {
      display: flex;
      background-color: lightgrey;
      padding: 10px;
      height: max-content;
    }

    .flex-item {
      background: steelblue;
      color: white;
      margin: 5px;
      padding: 100px;
      text-align: center;
    }
</style>

<div class="flex-container">
  <div class="flex-item">1</div>
  <div class="flex-item">2</div>
  <div class="flex-item">3</div>
  <div class="flex-item">4</div>
  <div class="flex-item">5</div>
  <div class="flex-item">6</div>
  <div class="flex-item">7</div>
  <div class="flex-item">8</div>
  <div class="flex-item">9</div>
  <div class="flex-item">10</div>
</div>
flex-wrap: nowrap | wrap | wrap-reverse;

It is applied to the flex container, not the individual items.


🔹 Single vs. Multi-line Layouts

Value

Description

nowrap (default)

All items stay on one line (may overflow container).

wrap

Items wrap onto multiple lines (from top to bottom).

wrap-reverse

Like wrap, but lines stack in reverse order (bottom to top).


🔸 nowrap (Default)

  • Tries to fit everything in a single row/column.

  • Items shrink or overflow if there's not enough space.

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

🔸 wrap

  • Items wrap to the next line when they don’t fit.

  • Great for responsive grids or button rows.

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

🔸 wrap-reverse

  • Items wrap to new lines in reverse (bottom-to-top).

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

🧱 Flex-wrap Playground


📱 Layout Breaking & Responsiveness

❌ Problem without wrap:

  • Items may overflow or shrink too small.

  • Breaks layouts on small screens.

✅ With flex-wrap: wrap:

  • Items wrap into new rows or columns.

  • Enables responsive, content-friendly layouts.


✅ Summary

Value

Behavior

nowrap

All items stay in a single line

wrap

Items wrap to next line (top → bottom)

wrap-reverse

Items wrap in reverse (bottom → top)