Skip to content
Astera Technology

Push a trailing flex item with an auto margin

Allocate free space before a trailing item while keeping source order unchanged.

Result begins 240px from the bar start in the left-to-right fixture.

Download complete case notes and answer keys ↓

Inspect the working example

Source and Result each occupy 60px in a 300px flex bar. The trailing item uses margin-inline-start:auto, absorbing the remaining 180px before it.

Inputs and native observations
CaseInput or inspectionResult and meaning
First itemMeasure start offset0px
Trailing item300−60Start offset 240px
Free margin300−60−60180px before the trailing item
Download editable CSV ↓

Follow the browser mechanism

Auto margins absorb positive free space before main-axis alignment. The logical inline-start side maps through direction; the default fixture is LTR. Mirroring direction in a private test does not imply that Chinese content should use RTL.

Centering the pair is a different task

The alternative centers both items together instead of reserving free space before the trailing one.

Counterexample source; inspect separately
<style>body{margin:8px}</style>
<style>.bar{display:flex;width:300px;justify-content:center}.bar p{margin:0;width:60px}</style><section class="bar"><p>Source</p><p>Result</p></section>

The two starts are 90px and 150px, rather than 0px and 240px.

Try the repair

If each item becomes 80px but the bar stays 300px, where does the trailing item start?

Show the worked answer

At 220px from the start. The free margin is 140px: 300−80−80.

  • Measure item bounds and the resolved auto margin.
  • Keep DOM source order unchanged when checking alignment.

Original example files

Related resources