Back to blog

Mastering layout: why inline-flex buttons do not center and how to fix without regressions

9/26/2025 · 1 min · Development

Share

This is a common production bug: the button looks almost centered, but text shifts when width changes, icons are added, or labels wrap.

Root cause

With inline-flex, child alignment is controlled by:

text-center does not distribute flex children on the main axis.

Correct base fix

<button class="inline-flex items-center justify-center gap-2 w-64 px-8 py-3">
  <span>Learn more</span>
</button>

Icon-safe pattern

<button class="inline-flex items-center justify-center gap-2 w-64 px-8 py-3">
  <svg class="h-4 w-4 shrink-0" aria-hidden="true"></svg>
  <span class="leading-none">Continue</span>
</button>

Responsive-safe width strategy

Prefer flexible sizing when labels vary by locale:

<button class="inline-flex items-center justify-center min-w-40 max-w-full px-6 py-3">
  Confirm action
</button>

Debug checklist

Final takeaway

If a component uses flex, alignment must be solved on flex axes, not text flow. justify-center + items-center plus icon/width discipline is the reliable fix.

CC BY-NC

This post is licensed under CC BY-NC.

Comments

Join the discussion below.