Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox: A way to align individual element along main axis

Tags:

html

css

flexbox

I was wondering if there is a way to override alignment along main axis for individual element. Something like this:

flexbox challenge

In the picture above, all the elements have align-items: flex-start, except of the last one, which needs to be aligned as flex-end, or space-around.

There is an option to override along cross axis: align-self, but probably not along main axis.

What is bad with doing this with position: absolute: the other elements should respect that special one, and not overlap with it.

What is bad with float is that it works for flex-direction: row only.

like image 628
Dan Avatar asked Mar 16 '15 01:03

Dan


People also ask

How do you align individual elements in a flexbox?

The axes and flex-direction If you are working with flex-direction set to row , this alignment will be in the inline direction. However, in Flexbox you can change the main axis by setting flex-direction to column . In this case, justify-content will align items in the block direction.

How can you center your flex items along the main axis?

To center our box we use the align-items property to align our item on the cross axis, which in this case is the block axis running vertically. We use justify-content to align the item on the main axis, which in this case is the inline axis running horizontally.

How can I align one item right with flexbox?

You can also align a flex item to the right by setting the CSS margin-right property. In our example below, we set the "auto" value of this property on the "align2" class.


1 Answers

Simply set margin-left to "auto" for the 4th element. When aligning items along the main axis, auto margins are the way to go.

like image 69
cssmaniac Avatar answered Oct 24 '22 11:10

cssmaniac