Flex

The Flex component wraps around a basic div element enhanced with flexbox container properties.

Usage

Import the Flex component from @avocado-ui/react and use like so

Large, white towerLarge, white towerLarge, white tower

Flex Children

The Flex component can solve your basic layout needs. But if you want more control over the flex children, import and wrap the child component with the FlexItem component from avocado. This way, you can use any of the flex item properties.

Take for example, the flex prop used to specify the width of a flex child.

Large, white tower
Large, white tower
Large, white tower

All flex child CSS properties are available as props on the FlexItem component.

Spacing Elements

Use the gap prop to specify space between flex items.

Having uniform spacing between flex items is a tricky challenge. Avocado does the dirty work underneath, so you can focus on making your layouts look exactly how you want them to.

Large, white tower
Large, white tower
Large, white tower

The gap prop receives an number, but converts it to pixels underneath. However, the spacing does not affect the last element.

Flex Direction

Change the main-axis orientation of the flex items with the flexDirection prop on the Flex parent. In the example below, flexDirection is set to column. It's set to the default (row) in the second. See more on the Mozilla Docs.

Possible values are column | column-reverse | row | row-reverse | initial | revert | unset

Large, white towerLarge, white tower

Large, white towerLarge, white tower

Justify Content

Specify how the flex items are arranged along the main axis. Possible values are flex-start, flex-end, center, space-around, space-between, space-evenly. Below is an example with justifyContent set to flex-start.

Flex Item

Large, white tower

Align Items

Control the orientation of flex children on the cross axis with the alignContent prop. Below is an example of how we would center flex items on the cross axis.

Flex Item

Large, white tower

Align Content

alignContent sets the distribution of space between and around content items along a flexbox's cross-axis.

There's a huge confusion between align-items and align-content. I'll avoid going over it here, but you can see What's the difference between align-content and align-items? for more clarity

Possible values are start,end, flex-start, flex-end, center, normal, baseline, first baseline, last baseline, space-between, space-around, space-evenly, stretch, safe, unsafe.

Here's an example of centering all flex items across the whole cross axis.

The most important thing to know about the alignContent prop is that it only works when flex items span multiple rows.

Flex Item

Large, white tower

Flex Wrap

Just like the flex-wrap CSS property, you can specify the behavior of flex items when they exceed the specified container width. Possible values are nowrap, wrap and wrap-reverse.

Below is an example of flexWrap value, nowrap, Notice all elements are squeezed into the column.

Large, white towerLarge, white towerLarge, white towerLarge, white towerLarge, white towerLarge, white towerLarge, white towerLarge, white tower

Here's another example with flexWrap set to wrap. Other flex items wrap and go to the next row when there's no longer space for them.

Large, white towerLarge, white towerLarge, white towerLarge, white towerLarge, white towerLarge, white towerLarge, white towerLarge, white tower

Flex Flow

Shorthand value for flex-direction and flex-wrap. Default value is row nowrap. Possible values are a combination of both properties.

Large, white towerLarge, white towerLarge, white towerLarge, white towerLarge, white towerLarge, white towerLarge, white towerLarge, white tower

Flex Props Table

propertydescriptiontypedefault
flexWrapControl wrapping of flex childrennowrap, wrap, wrap-reversenowrap,
alignContentHow to distribute extra space in the cross-axisflex-start , flex-end , center , space-between , space-around , space-evenly , stretch , start , end , baseline, first baselinenormal
flexFlowShort hand for flex-direction and flex-wrap propertiesnonerow nowrap
justifyContentControls alignment along the main axis`flex-start , flex-end , center , space-between , space-around , space-evenly , start , end , left, right, safeflex-start
alignItemsControls alignment along the cross axisstretch , flex-start , flex-end, center, baseline, first baseline, last baseline, start , end , self-start, self-endstretch
flexDirectionSpecifies the main axis of the flexrow, row-reverse, column, column-reverserow

FlexItem Props Table

propertydescriptiontypedefault
orderControls the order in which they appear in the flex containernumber0
flexGrowDefines the ability for a flex item to grow if necessarynumber0
flexShrinkDefines the ability for a flex item to shrink if necessary.number0
flexBasisSpecifies the size of a flex itemnumberauto
flexShorthand for flex-grow, flex-shrink & flex-basisflex-grow - flex-shrink - flex-basisauto
alignSelfSpecifies the alignment of a flex child along the cross axisauto , flex-start, flex-end, center, baseline, stretchauto