HTML & CSS - Mastery / Flexbox Mastery

Align-items

🔗 Align Items

The align-items property controls how flex items are aligned along the cross axis (the axis perpendicular to the main axis).

It defines the default alignment for all items in the container (unless overridden by align-self on individual items).


🧭 Cross Axis Reminder

flex-direction

Main Axis

Cross Axis

row

horizontal ➡

vertical ⬇

column

vertical ⬇

horizontal ➡

So align-items affects:

  • vertical alignment in a row

  • horizontal alignment in a column


🧱 Syntax

<style>
    .flex-container {
      display: flex;
      background-color: lightgrey;
      padding: 10px;
      height: 200px; /* Needed to visualize stretching */
    }

    .flex-item {
      background: steelblue;
      color: white;
      margin: 5px;
      padding: 20px;
      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>
align-items: flex-start | center | flex-end | stretch;

🔹 Values Explained

🔸 stretch (default)

  • Items stretch to fill the container along the cross axis.

  • Only works if items have no fixed height/width.

.flex-container {
  display: flex;
  align-items: stretch;
}

🔸 flex-start

  • Items align to the start of the cross axis.

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

🔸 flex-end

  • Items align to the end of the cross axis.

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

🔸 center

  • Items align to the center of the cross axis.

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

🧱 Align-items Playground


✅ Summary

  • align-items controls vertical or horizontal alignment depending on main axis.

  • Default is stretch, but you can easily center or align to edges.

  • Use with justify-content for full 2D layout control.