← Back to list

The CSS Axis Trap: The Day I Realized justify-content Doesn’t Mean "Horizontal"

I still laugh thinking about the time early on when I spent 30 minutes shouting at my CSS because justify-content: center was moving my UI…

Aditya Solanke · 2026-05-20 16:38 · 3 claps · 1.9 min read
#web-development #css #tailwind-css #css3 #html
Open on Medium ↗
Wiki topics: 🌐 · Web Development

The CSS Axis Trap: The Day I Realized justify-content Doesn’t Mean "Horizontal"

I still laugh thinking about the time early on when I spent 30 minutes shouting at my CSS because justify-content: center was moving my UI elements vertically instead of horizontally.

I realized I fell into a super common trap: I memorized that justify means 'horizontal' and align means 'vertical.' But the moment you write flex-direction: column, JavaScript/CSS flips the script.

justify-content doesn't care about left-to-right; it only cares about the Main Axis. If your direction is a column, your Main Axis is now top-to-bottom. I had to remind myself that alignment in Flexbox is entirely relative to the axis direction, not the screen borders. It’s a tiny concept, but forgetting it is an easy way to break a layout.

<!-- The Setup: A simple card layout -->
<div class="card-container">
  <button>Login</button>
  <button>Register</button>
</div>
.card-container {
  display: flex;

  /* 1. THE DEFAULT (Row direction) */
  flex-direction: row; 
  /* Main Axis is Horizontal. This centers the buttons left-to-right. */
  justify-content: center; 

  /* ----------------------------------------------- */

  /* 2. THE FLIP (Switching to Mobile/Column layout) */
  flex-direction: column; 

  /* THE BUG: I expected this to still center the buttons horizontally. 
     Instead, it did absolutely nothing because the container had no fixed height, 
     and the Main Axis was now vertical! */
  justify-content: center; 

  /* THE FIX: To center them horizontally now, I needed the Cross Axis. */
  align-items: center; 
}

Why did justify-content stop working?

Because Flexbox doesn’t know what ‘top,' 'bottom,' 'left,' or 'right' mean. It only knows two things: the main axis and the cross axis.

  • By default (flex-direction: row), the Main Axis runs horizontally. So it justify-content moves things left and right.
  • But the second you change to flex-direction: column, the main axis flips vertically. Now, it justify-content controls top-to-bottom spacing and align-items takes over the horizontal alignment.

The “Invisible Height” Ghost Bug

This is the one that causes hair-pulling. You switch to flex-direction: column, you write justify-content: center to center it vertically, and... absolutely nothing happens. Why? Because a standard div only takes up as much height as its content. If the container is already hugging the elements tightly, there is no extra vertical space to justify them into! You have to explicitly give the container a height (like height: 100vh or a fixed value) for that vertical centering to even show up.

/* What felt intuitive but broke: */
.card-container {
  display: flex;
  flex-direction: column;
  justify-content: center; /* Did nothing! No height on container + wrong axis. */
}

/* The actual fix: */
.card-container {
  display: flex;
  flex-direction: column;
  height: 100vh;         /* Give it space to move */
  align-items: center;   /* Centers horizontally on a column layout */
}

The Lesson: Stop memorizing ‘justify = horizontal’. Memorize ‘justify = Main Axis’. It saves a ton of guessing games when building responsive mobile layouts.


메타데이터
post_id
03e5f6ef4d54
slug
the-css-axis-trap-the-day-i-realized-justify-content-doesnt-mean-horizontal-03e5f6ef4d54
url
https://medium.com/@onkmade/the-css-axis-trap-the-day-i-realized-justify-content-doesnt-mean-horizontal-03e5f6ef4d54
canonical_url
https://medium.com/@onkmade/the-css-axis-trap-the-day-i-realized-justify-content-doesnt-mean-horizontal-03e5f6ef4d54
author_url
https://medium.com/@onkmade
status
ok
fetched_at
2026-06-15 20:49:13