HTML & CSS - Mastery / Flexbox Mastery

Justify-content

🧑‍⚖️ Justify Content

The justify-content property controls how flex items are aligned along the main axis — the direction items are laid out (row, column, etc.).

👉 It deals with horizontal spacing in row direction, and vertical spacing in column direction.


🧭 Main Axis Reminder

  • flex-direction: row → main axis is horizontal

  • flex-direction: column → main axis is vertical

justify-content affects item distribution and spacing along the main axis.


Master code block to be used further:

<style>
   /*All the changes are made here further*/
    .flex-container {
      display: flex;
      background-color: lightgrey;
      padding: 10px;
    }
    .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>

🧱 Syntax

justify-content: flex-start | center | flex-end | space-between | space-around | space-evenly;

🔹 Property Values Explained

🔸 flex-start (default)

  • Items align at the start of the main axis.

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

🔸 center

  • Items are centered along the main axis.

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

🔸 flex-end

  • Items align at the end of the main axis.

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

🔸 space-between

  • First item is at start, last at end, space between items.

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

🔸 space-around

  • Equal space around items (left + right).

  • So, items have more space between them than at ends.

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

🔸 space-evenly

  • Equal space between items and at the ends.

.flex-container {
  display: flex;
  justify-content: space-evenly;
}

Try applying all justify-content property values while using flex-direction: column.


🧱 Justify-Content Playground


✅ Summary

  • justify-content aligns items horizontally or vertically, based on the main axis.

  • Use it to center, push, or space items across the layout.

  • Try combining it with flex-direction and align-items for full control.