HomeGuideLabGlossary

Setting main direction

Flex item boxes grow and shrink in the main direction

The heart of Flexbox layout is the ability for the flex item boxes to say if they’d like to grow or shrink inside the space offered to them by the flex container. This growing and shrinking happens in one direction. That direction is called the main direction.

Main direction12Flex boxes please come this way3

The flex container sets the main direction with

The value of the property set on the element whose box is a flex container determines the main direction.

Main directionflex-direction

The initial value of is row. row means “the direction in which words go in a row of text”. If you’re writing in English, that direction is left to right. If you’re writing in Arabic, that direction is right to left.

flex-direction: rowBla bla bla we are words in a row of textDirection of words in a row of textMain direction321

Think main and cross, forget horizontal and vertical

Flexbox does not care about terms from the physical world like top, left, width, or height. It doesn’t even care about terms from a writing-mode-relative world like inline, block, or inline-start.

tophorizontalinlineIGNOREDIGNOREDIGNOREDMainCross

All that Flexbox cares about are Flexbox-world terms like main, cross, and main-start.

cross-startmain-startmain-endcross-endMain directionCross direction

The reason is that sometimes you want a bunch of boxes to be stacked in one direction or another, but you want to use the terms no matter which direction you choose.

Main directionCross directionMain directionCross directionflex-direction: rowflex-direction: column

For example, imagine you want to lay out a list of 3 cards. The list is your flex container, and each card is a flex item. You can have the list insert a gap between the cards. You can distribute the cards inside the list. You can have the cards say if they want to grow. None of this requires knowing anything about the physical world, or the direction of writing.

Main directionCross directiongap: 10pxflex-direction: rowflex-grow: 1

What row and column mean

Direction of words in a row of textrowWord word word wordflex itemflex itemflex itemMain direction
Row of textRow of textRow of textMain directionflex itemflex itemflex itemcolumnThe direction in which rows stack in a column

values mean different things depending on the direction of writing

writing-mode sets the inline dimension (horizontal or vertical) and the block direction (top to bottom, left to right, right to left).

Words go horizontally, rows stack top to bottomWord word word wordWord word word wordWords go vertically, rows stack right to leftWord word word wordWord word word wordwriting-mode: horizontal-tbwriting-mode: vertical-rl

direction sets the inline direction (logical left to right, logical right to left).

Words go left to rightWord word word wordWords go right to leftWord word word worddirection: ltrdirection: rtl

sets the main direction and the cross dimension.

flex-direction: rowMainCross