HomeGuideLabGlossary

Flexible sizing in main direction, normal and stretchy sizing in cross direction

Sizing flex items is the heart of Flexbox layout

Once the flex container set the ground rules, the fun begins!

display: flexflex-direction: rowjustify-content: centeralign-items: stretchgap: 8pxflex-wrap: nowrap

Order of sizing

  1. First, apply flexible sizing to the flex items to get their sizes in the main direction.
main direction12flex-grow: 1flex-grow: 1
  1. Using the size in the main direction, do an initial sizing of the flex item in the cross direction.
1I need to fitblock-size: autoblock-size: autoCross direction
  1. Then, stretch the flex lines.
1I need to fitalign-content: stretch
  1. Then, stretch the flex items.
1I need to fitalign-self: stretchalign-self: stretch

A flex container sets the stage and sets the main direction with

The main direction is the direction in which flex items will be laid out. And it’s the direction in which all Flexbox-only sizing properties are applied.

main direction1flex-grow: 1flex-basis: 80pxflex-direction: row

The flex items are the “flex boxes”, not the flex container

Remember, the flex container itself is sized according to the rules of whichever layout type it participates in.

1.flex-containerbody.flex-item"1"I decide to lay out the flex container with Flow layoutbody.flex-container.flex-item

The flex items are sized according to the rules of Flexbox layout. The flex items are the flex(ible) boxes, if you will.

1I’m the flexible box.I’m the Flex Box. Get it?!

The three flex item sizing properties in Flexbox.

  1. flex-basis. This lets the flex item say what size it would like to have in the main axis. That size is called the flex base size.
1flex-basis: 80pxI’d like to be 80 pixels in the main direction, please.
  1. flex-grow. This lets the flex item say if it wants to grow its size above its flex base size if given the opportunity.
1flex-grow: 1There’s room to grow? I’d love to do that.
  1. flex-shrink. This says if the flex item is willing to shrink its size below its flex base size if asked to.
1flex-shrink: 1I’ll shrink if I have to.

Both flex-grow and flex-shrink can say not just if, but also at what rate relative to other flex items on a flex line they’re willing to change their size.

12flex-grow: 1flex-grow: 2

Flex items have the final say over their sizing

The flex container provides a stage with rules, but the flex items have the final say over their size.

1flex-shrink: 0I refuse to shrink even though there’s no space on the flex line

In the main axis, flexible sizing is all that matters, and the flex container gets no chance at input at all.

11All I do is provide some available spaceAnd I decide what I do with it!

In the cross axis, the flex container can force flex lines to stretch, and suggest that flex items stretch if they’ll listen.

align-content: stretchalign-items: stretch11align-self: centeralign-self: autoI’ll listenWe have to stretchI won’t