Overview
Flex is a layout component built on top of Box that provides an intuitive API for creating flexbox layouts. It simplifies common flexbox patterns and provides convenient props for alignment, spacing, and responsive behavior. Use Flex when you need to:- Create horizontal or vertical layouts
- Align items along a main or cross axis
- Control how flex items grow, shrink, or maintain a basis size
- Build responsive layouts that change direction
Installation
Basic Usage
Props
The HTML element to render. Can be any valid HTML element.
Overrides the default element name to apply unique styles with the Customization Provider.
Sets the display type. Use ‘inline-flex’ for inline flex containers.
Alignment Props
Horizontal alignment of flex items. Maps to justifyContent when vertical is false, and alignItems when vertical is true.
left- Align items to the startcenter- Center itemsright- Align items to the endaround- Distribute items with space aroundbetween- Distribute items with space between
Vertical alignment of flex items. Maps to alignItems when vertical is false, and justifyContent when vertical is true.
top- Align items to the startcenter- Center itemsbottom- Align items to the endstretch- Stretch items to fill
Direction Props
When true, sets flex direction to column (vertical stacking). When false or omitted, items flow horizontally in a row. Supports responsive values.
Flex Item Props
Controls whether flex items should grow to fill available space. Pass true for default growth (1), false for no growth (0), or a number for specific grow factor. Supports responsive values.
Controls whether flex items should shrink when space is limited. Pass true for default shrinking (1), false for no shrinking (0), or a number for specific shrink factor. Supports responsive values.
Sets the initial main size of a flex item before space distribution. Can be a pixel value, percentage, or ‘auto’. Supports responsive values.
Wrapping Props
When true, flex items will wrap to multiple lines. When false or omitted, items stay on one line. Supports responsive values.
Spacing Props
Flex supports all spacing props from Box:Sets margin on all sides using space design tokens.
Sets top margin using space design tokens.
Sets right margin using space design tokens.
Sets bottom margin using space design tokens.
Sets left margin using space design tokens.
Sets horizontal (left and right) margin. Cannot be used with individual margin props.
Sets vertical (top and bottom) margin. Cannot be used with individual margin props.
Sets padding on all sides using space design tokens.
Sets top padding using space design tokens.
Sets right padding using space design tokens.
Sets bottom padding using space design tokens.
Sets left padding using space design tokens.
Sets horizontal (left and right) padding. Cannot be used with individual padding props.
Sets vertical (top and bottom) padding. Cannot be used with individual padding props.
Size Props
Sets width using design tokens. Cannot be used with size prop.
Sets minimum width using design tokens.
Sets maximum width using design tokens.
Sets height using design tokens. Cannot be used with size prop.
Sets minimum height using design tokens.
Sets maximum height using design tokens.
Sets both width and height to the same value. Cannot be used with width or height props.
Examples
Horizontal Row with Center Alignment
Vertical Stack
Space Between Items
Growing and Shrinking Items
Fixed Basis
Wrapping Items
Responsive Direction
Responsive Alignment
Nested Flex Containers
Best Practices
Use Semantic Alignment Props
Flex provides intuitive alignment props (hAlignContent and vAlignContent) that automatically map to the correct flexbox properties based on direction:
Don’t Mix marginX/Y with Individual Margins
The component will log an error if you use marginX/marginY alongside individual margin props:Don’t Mix size with width/height
Similarly, don’t use the size prop with width or height:Use Stack for Simple Stacking
For simple vertical or horizontal stacking with consistent spacing, consider using Stack instead:Leverage Responsive Props
Use responsive arrays to adapt layouts across breakpoints:Accessibility
- Flex is a presentational component and doesn’t have specific accessibility requirements
- Use semantic HTML via the
asprop when appropriate (e.g.,as="nav"for navigation) - Ensure reading order in the DOM matches visual order
- Don’t rely solely on visual layout to convey meaning
Related Components
- Box - Low-level layout primitive
- Stack - Simplified stacking with spacing
- Grid - 12-column grid system
- Media Object - Media object pattern