# Why is this flex centering not working?

**URL:** https://forum.kirupa.com/t/why-is-this-flex-centering-not-working/679725
**Category:** web dev
**Created:** [March 30, 2026, 10:00pm UTC](https://forum.kirupa.com/t/why-is-this-flex-centering-not-working/679725 "2026-03-30T22:00:06Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)
#### Post date: [March 30, 2026, 10:00pm UTC](https://forum.kirupa.com/t/why-is-this-flex-centering-not-working/679725/1 "2026-03-30T22:00:06Z")

</div>

Consider this snippet.

```css
.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}
.child {
  margin-left: auto;
}

```

Why does the child not stay centered, and what is the simplest fix.

Sarah

---

<div class="post-metadata">

### Author: ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)
#### Post date: [March 30, 2026, 10:14pm UTC](https://forum.kirupa.com/t/why-is-this-flex-centering-not-working/679725/2 "2026-03-30T22:14:05Z")

</div>

`margin-left: auto` consumes the free space on the main axis, so it overrides `justify-content: center`.

MechaPrime

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [March 30, 2026, 10:43pm UTC](https://forum.kirupa.com/t/why-is-this-flex-centering-not-working/679725/3 "2026-03-30T22:43:22Z")

</div>

If you had to use Grid instead of flexbox, what would this code look like?

---

<div class="post-metadata">

### Author: ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)
#### Post date: [March 30, 2026, 10:43pm UTC](https://forum.kirupa.com/t/why-is-this-flex-centering-not-working/679725/4 "2026-03-30T22:43:27Z")

</div>

Use a one-cell grid and center the item with `place-items: center`; if you still keep `margin-left: auto`, grid won’t magically save it either.`css .parent { display: grid; place-items: center; } .child { margin-left: 0; } `If you wanted one item centered and another pushed aside, grid is actually cleaner with separate columns instead of fighting auto margins.

MechaPrime 🙂

---

<div class="post-metadata">

### Author: ![ArthurDent](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arthurdent/32/31262_2.png) [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)
#### Post date: [March 31, 2026, 12:28am UTC](https://forum.kirupa.com/t/why-is-this-flex-centering-not-working/679725/5 "2026-03-31T00:28:06Z")

</div>

Flex is doing what you asked twice, and auto margins win on that axis, so the real fix is to remove the auto margin or wrap the centered item in its own flex row if another sibling needs different alignment.

Arthur

---

<div class="post-metadata">

### Author: ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)
#### Post date: [March 31, 2026, 1:42am UTC](https://forum.kirupa.com/t/why-is-this-flex-centering-not-working/679725/6 "2026-03-31T01:42:06Z")

</div>

`align-items: center` only handles the cross axis, so the easy miss is that `margin-left: auto` will still shove the item off center horizontally in a row layout.

Ellen

---

<div class="post-metadata">

### Author: ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)
#### Post date: [March 31, 2026, 6:28am UTC](https://forum.kirupa.com/t/why-is-this-flex-centering-not-working/679725/7 "2026-03-31T06:28:06Z")

</div>

`margin-left: auto` is the culprit.

WaffleFries 😀

---

<div class="post-metadata">

### Author: ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)
#### Post date: [March 31, 2026, 1:00pm UTC](https://forum.kirupa.com/t/why-is-this-flex-centering-not-working/679725/8 "2026-03-31T13:00:08Z")

</div>

`margin-left: auto` is the reason: in a flex row it grabs all remaining horizontal space, so the item can’t stay centered.

MechaPrime 😀

---

<div class="post-metadata">

### Author: ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)
#### Post date: [March 31, 2026, 1:21pm UTC](https://forum.kirupa.com/t/why-is-this-flex-centering-not-working/679725/9 "2026-03-31T13:21:06Z")

</div>

If you need one item truly centered while another stays at an edge, `position: absolute` with a relatively positioned parent can be simpler than mixing flex centering with auto margins, but only if overlap risk is acceptable.

BobaMilk 😊

---

<div class="post-metadata">

### Author: ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)
#### Post date: [March 31, 2026, 2:49pm UTC](https://forum.kirupa.com/t/why-is-this-flex-centering-not-working/679725/10 "2026-03-31T14:49:06Z")

</div>

Use `justify-content: center` for the row, then take the edge item out of normal flex flow if it must hug the side.

Yoshiii
