Flexbox: margin
Auto margins on flex items have an effect very similar to auto margins in block flow:
-
During calculations of flex bases and flexible lengths, auto margins are treated as ‘0’.
-
Prior to alignment via ‘justify-content’ and ‘align-self’, any positive free space is distributed to auto margins in that dimension.
-
Overflowing elements ignore their auto margins and overflow in the end/foot direction.
Note that, if free space is distributed to auto margins, the alignment properties will have no effect in that dimension because the margins will have stolen all the free space left over after flexing.
Demos
margin: auto;
// horizontal and vertical centering
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Necessitatibus temporibus?
margin-left: auto at second item
// a possibility to group the flex-items
Lorem ipsum dolor sit amet,
Lorem ipsum dolor sit amet,
Lorem ipsum dolor sit amet,
margin-left: auto at second and fourth item
// here: create three groups
Lorem ipsum dolor sit amet,
Lorem ipsum dolor sit amet,
Lorem ipsum dolor sit amet,
Lorem ipsum dolor sit amet,
Lorem ipsum dolor sit amet,
margin-top: auto at last item
// here: create two groups | flex-direction is column
Lorem ipsum dolor
Lorem ipsum dolor
Lorem ipsum dolor
margin-top: auto at last item with align-items:flex-start
// here: create two groups | flex-direction is column
// same result as above - the align-items property has no effect because margin-top: auto has stolen the free space
Lorem ipsum dolor
Lorem ipsum dolor
Lorem ipsum dolor
No margin-top: auto at last item with align-items:flex-start
// align-items property works
Lorem ipsum dolor
Lorem ipsum dolor
Lorem ipsum dolor