We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

Box Alignment in Flexbox


The Box Alignment Specification details how alignment works in various layout methods, on this page we explore how Box Alignment works in the context of Flexbox. As this page aims to detail things which are specific to Flexbox and Box Alignment, it should be read in conjunction with the main Box Alignment page which details the common features of Box Alignment across layout methods.

Basic Example

In this example, three flex items are aligned on the Main Axis using justify-content and on the Cross Axis using align-items. The first item overrides the align-items set on the group by setting align-self to center.

The Axes and flex-direction

Flexbox respects the Writing Mode of the document, therefore if you are working in English and set justify-content to flex-end this will align the items to the end of the flex container. 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. Therefore it is easiest to think about the Main and Cross Axis when working in Flexbox.

  • The Main Axis = direction set by flex-direction = justify-content
  • The Cross Axis = runs across the Main Axis = align-content, align-self/align-items

Main Axis Alignment

Cross Axis Alignment

There is no justify-self in Flexbox

On the Main Axis Flexbox deals with our content as a group. The amount of space required to lay out the items in calculated, and the leftover space is then available for distribution. The justify-content property controls how that leftover space is used. Set justify-content: flex-end and the extra space is placed before the items, justify-content: space-around and it is placed either side of the item in that dimension.

This means that a justify-self property does not make sense in Flexbox as we are always dealing with moving this group of items around.

On the Cross Axis align-self makes sense as we potentially have additional space in the flex container in that dimension, in which a single item can be moved to the start and end.

Alignment and Auto Margins

There is a specific use case where we might think that a justify-self property is what we need, and this is when we want to split a set of flex items, perhaps to create a split navigation pattern. For this use case we can use an auto margin. A margin set to auto will absorb all available space in its dimension. This is how entering a block with auto margins works. By setting the left and right margin to auto, both sides of our block try to take up all of the available space and so push the box into the centre.

By setting a margin of auto on one item in a set of flex items all aligned to start, we can create a split navigation. This works well with flexbox and the alignment properties. As soon as there is no space available for the auto margin, the item behaves in the same way as all the other flex items and shrinks to try to fit into the space.

The gap properties

The Box Alignment specification details how the gap properties should work in Flexbox however at the present time no browser has implemented these.

Creating fixed size gaps between items

On the Main Axis, the column-gap property will create fixed size gaps on either side of the item.

On the Cross Axis the row-gap property will create spacing between adjacent flex lines, therefore flex-wrap just also be set to wrap for this to have any effect.

At the time of writing, no browser supports the gap properties in Flexbox. These are a newer addition to the specification, with regard to being specified for flex layout.


CSS Properties

Glossary Entries


External Resources

Document Tags and Contributors

Contributors to this page: rachelandrew
Last updated by: rachelandrew,