Skip to content
TheChatArchiveThe Chat Archive
o3-2025-04-16· OpenAIHugging Face LMArena Human Preference 140K

I have a flex div with three elements: <> <--> <--->. I want the middle element to be…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-flash-lite-preview-06-17-thinking’s answer.
  1. User

    I have a flex div with three elements: <> <--> <--->. I want the middle element to be centered and the other two to be to the sides. justify-content: space-between almost does the thing except the middle element is not centered and I haven't figured out the way to center it

  2. Assistant

    justify-content: space-between distributes the free space between every pair of flex-items, so with three items you get two equal gaps – that doesn’t guarantee the second item will sit on the real centre line of the container.

    A simple Flexbox trick is to give the middle item auto margins on both sides.
    Auto margins “absorb” the remaining free space before justify-content is applied, so they push the middle element to the exact centre while the first and last items stay hard left / hard right.

    HTML

    <div class="bar">
      <span class="left"> &lt; </span>
      <span class="mid"> &lt;--&gt; </span>
      <span class="right"> &lt;---&gt; </span>
    </div>
    

    CSS

    .bar{
      display:flex;          /* main axis = horizontal row */
      align-items:center;    /* (optional) vertical centring */
      /* justify-content: flex-start;  ← default, nothing else needed */
    }
    
    .mid{
      margin-left:auto;      /* absorb remaining space on the left  */
      margin-right:auto;     /* absorb remaining space on the right */
    }
    
    /* purely for demo */
    .bar span{padding:.4rem .8rem;background:#e8e8e8;border:1px solid #ccc;}
    

    Result

    |<------------------- container ------------------->|
    <                    <--->                     --->
    

    • The first item appears at the start edge (left).
    • The last item naturally follows the middle one, so it ends up at the end edge (right).
    • Because both margins on .mid are auto, whatever space is left after laying out the three items is split evenly, so the middle item’s centre aligns with the container’s centre.

    No extra wrappers, no absolute positioning – just plain Flexbox.

  3. User

    No, horizontal auto-margins behave the same way the space-between does: the middle item is centered between two lateral, but not centered relative to container

  4. Assistant

    You only need two things ‑ and neither of them is justify-content: space-between.

    1. make the container a flexbox
    2. give the middle item margin-left:auto; margin-right:auto;

    That’s it – the middle item will sit exactly on the centre-line of the container, while the first item stays hard left and the last item hard right.

    Demo

    <div class="row">
      <span class="item"> < </span>
      <span class="item mid"> <---> </span>
      <span class="item"> <----> </span>
    </div>
    
    .row{
      display:flex;          /* flex container */
      align-items:center;    /* (optional) vertical centring */
      /*  DO NOT keep `justify-content: space-between` here  */
    }
    
    .mid{
      margin-left:auto;      /* eat free space to the left  */
      margin-right:auto;     /* …and to the right           */
    }
    
    /* just to make the boxes visible */
    .item{
      padding:.4rem .8rem;
      background:#e8e8e8;
      border:1px solid #aaa;
    }
    

    Why it works
    • Flex lays out the three items in their source order.
    • The two auto margins on the middle element absorb all the remaining horizontal space, splitting it 50 / 50.
    • That puts the middle element’s midpoint on the exact midpoint of the container.
    • With no space left over, the first item is forced to the far left and the last item to the far right.

    Common pitfall
    If you keep justify-content: space-between, the browser first gives the two gaps between the three items an equal share of the free space — after that there’s nothing left for the auto margins, so the middle element ends up between the outer ones instead of at the real centre.
    So just drop (or override) justify-content.

    Works in all modern browsers, no extra wrappers, no absolute positioning.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.