I have a flex div with three elements: <> <--> <--->. I want the middle element to be…
- 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-betweenalmost does the thing except the middle element is not centered and I haven't figured out the way to center it - Assistant
justify-content: space-betweendistributes 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 beforejustify-contentis 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"> < </span> <span class="mid"> <--> </span> <span class="right"> <---> </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.midareauto, 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.
- 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
- Assistant
You only need two things ‑ and neither of them is
justify-content: space-between.- make the container a flexbox
- 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 twoautomargins 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 keepjustify-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.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.