HTML & CSS - Mastery / Flexbox Mastery

Align-content

⁑ Align Content

The align-content property controls space between multiple lines of flex items along the cross axis (when wrapping is enabled).

🔸 It only has an effect when flex-wrap: wrap (or wrap-reverse) is used and there is more than one row/column of items.


<style>
    .flex-container {
      display: flex;
      flex-wrap: wrap; /* Allow wrapping */
      height: 400px;
      gap: 10px;
      background-color: lightgrey;
      padding: 10px;
    }

    .flex-item {
      background: steelblue;
      color: white;
      margin: 5px;
      padding: 50;
      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>

🧱 Syntax

align-content: flex-start | flex-end | center | space-between | space-around

🧭 Works Only With Multi-line Layouts

Requirement

Needed?

Multiple lines of items

✅ Yes

flex-wrap: wrap

✅ Yes

Single-line flex

❌ No effect


🔄 Cross Axis Refresher

  • For flex-direction: row → cross axis is vertical

  • For flex-direction: column → cross axis is horizontal


🔍 Difference Between align-items and align-content

Property

Affects

Use Case

align-items

Positioning of individual items within each line

For aligning items inside each row

align-content

Spacing between rows or columns

For controlling vertical/horizontal space between lines

🔔 If there's only one row, align-content has no effect.


🔹 Property Values Explained

🔸 flex-start

Lines are packed to the start of the cross axis.

.flex-container {
  display: flex;
  align-content: flex-start;
}

🔸 flex-end

Lines are packed to the end of the cross axis.

.flex-container {
  display: flex;
  align-content: flex-end;
}

🔸 center

Lines are packed toward the center of the container.

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

🔸 space-between

Lines are evenly distributed, first at the top, last at the bottom.

.flex-container {
  display: flex;
  align-content: space-between;
}

🔸 space-around

Equal space above and below each row.

.flex-container {
  display: flex;
  align-content: space-around;
}

🧱 Align-Content Playground


✅ Summary

  • Use align-content only when items wrap onto multiple lines.

  • Combine it with flex-wrap and height/width for best results.