Search documentation v0.9.0

Flex

Build flexible layouts using CSS Flexbox

Source ↗️ Report Issue ↗️

Use the Flex component to arrange and align content dynamically within a container using CSS Flexbox. To use the Flex component, make sure you don’t have the includeUtilities rule disabled in your setup mixin (this rule is enabled by default):

Utilities must not be disabled in your setup to use the Flex component
@use 'webcoreui/styles' as *;
@include setup((
includeUtilities: false // Remove this option if you want to use the Flex component
));
You can also use CSS utility classes without the Flex component. You can read more in the Layout section.
Preview
1
2
3
How to use Flex in Astro
---
import { Flex } from 'webcoreui/astro'
---
<Flex>
<Box>1</Box>
<Box>2</Box>
<Box>3</Box>
</Flex>
How to use Flex in Svelte
<script>
import { Flex } from 'webcoreui/svelte'
</script>
<Flex>
<Box>1</Box>
<Box>2</Box>
<Box>3</Box>
</Flex>
How to use Flex in React
import React from 'react'
import { Flex } from 'webcoreui/react'
const Component = () => (
<Flex>
<Box>1</Box>
<Box>2</Box>
<Box>3</Box>
</Flex>
)
export default Component

Simply pass your elements or components inside the Flex component to display them in a flex container. By default, the element will be rendered as a div. This can be changed using the element prop:

<Flex element="section">...</Flex> <!-- Rendered as a `section` -->
<Flex element="article">...</Flex> <!-- Rendered as an `article` -->
<Flex element="main">...</Flex> <!-- Rendered as a `main` -->

Gap

The gap between flex items is set to 20px by default. To change this, use the gap prop with one of the following values:

Prop valueDefault value
none 0
xxs 2px
xs 5px
sm 10px
md 15px
default 20px
lg 25px
xl 30px
xl2 35px
xl3 40px
xl4 45px
xl5 50px
none
none
none
xxs
xxs
xxs
xs
xs
xs
sm
sm
sm
md
md
md
default
default
default
lg
lg
lg
xl
xl
xl
xl2
xl2
xl2
xl3
xl3
xl3
xl4
xl4
xl4
xl5
xl5
xl5
How to set gaps
<Flex gap="none" />
<Flex gap="xxs" />
<Flex gap="xs" />
...

You can configure your gaps using the webcore.config.scss configuration file. Read more about how to configure gaps in the CSS configuration documentation.

Direction

To set the direction of your flex container, set the direction property whose value can be one of row, column, row-reverse, or column-reverse:

row (default)
1
2
3
4
column
1
2
3
4
row-reverse
1
2
3
4
column-reverse
1
2
3
4
How to set the direction of the Flex component
<Flex direction="row" /> <!-- Default -->
<Flex direction="column" />
<Flex direction="row-reverse" />
<Flex direction="column-reverse" />

Alignment

To change the horizontal or vertical alignment of your flex items, use the alignment prop with one or both of the following values:

.justify-start
1
2
3
.justify-end
1
2
3
.justify-center
1
2
3
.justify-between
1
2
3
.justify-around
1
2
3
.justify-evenly
1
2
3
For horizontal alignment, set the horizontal property
<Flex alignment={{ horizontal: 'start' }}>...</Flex>
<Flex alignment={{ horizontal: 'end' }}>...</Flex>
<Flex alignment={{ horizontal: 'center' }}>...</Flex>
<Flex alignment={{ horizontal: 'between' }}>...</Flex>
<Flex alignment={{ horizontal: 'around' }}>...</Flex>
<Flex alignment={{ horizontal: 'evenly' }}>...</Flex>

You can pass a vertical property to the object in a similar manner to specify the vertical alignment of your items:

For vertical alignment, set the vertical property
<Flex alignment={{ vertical: 'start' }}>...</Flex>
<Flex alignment={{ vertical: 'end' }}>...</Flex>
<Flex alignment={{ vertical: 'center' }}>...</Flex>
<Flex alignment={{ vertical: 'baseline' }}>...</Flex>
<Flex alignment={{ vertical: 'stretch' }}>...</Flex>

Wrap

Similar to the direction prop, you can set the way elements wrap using the wrap prop. It accepts the following values which directly correspond with the native CSS value: wrap, nowrap, wrap-reverse.

wrap
1
2
3
4
5
nowrap
1
2
3
4
5
wrap-reverse
1
2
3
4
5
How to use the wrap prop with the Flex component
<Flex wrap="wrap" />
<Flex wrap="nowrap" /> <!-- Default -->
<Flex wrap="wrap-reverse" />

Responsiveness

The above properties can also be used responsively. Simply pass an object for the prop that you’d like to change based on media queries and define the following props:

How to use props responsively
<Flex gap={{
default: 'none',
xs: 'xs',
sm: 'sm',
md: 'md',
lg: 'lg'
}} />
How to use props responsively
<Flex direction={{
default: 'row',
xs: 'column',
sm: 'row-reverse',
md: 'column-reverse',
lg: 'row'
}} />
How to use props responsively
<Flex alignment={{
default: {
horizontal: 'start',
vertical: 'start'
},
xs: {
horizontal: 'center',
vertical: 'center'
},
sm: { ... },
md: { ... },
lg: { ... }
}} />
How to use props responsively
<Flex wrap={{
default: 'wrap',
xs: 'nowrap',
sm: 'wrap-reverse'
}} />

Notice that for alignment, you’ll need to pass an object that can have a horizontal and vertical property. All breakpoints are optional, you can create the object as needed.

You can change your breakpoints in your webcore.config.scss configuration file. Read more in the CSS Configurations guide to see examples.

API

type FlexProps = {
element?: string
gap?: Responsive<Gap>
alignment?: Responsive<Alignment>
direction?: Responsive<Direction>
wrap?: Responsive<Wrap>
className?: string
}
type ReactFlexProps = {
Element?: keyof JSX.IntrinsicElements
children: React.ReactNode
} & Omit<FlexProps, 'element'>
type Gap = 'none'
| 'xxs'
| 'xs'
| 'sm'
| 'md'
| 'default'
| 'lg'
| 'xl'
| 'xl2'
| 'xl3'
| 'xl4'
| 'xl5'
type VerticalAlignment = 'center'
| 'start'
| 'end'
| 'baseline'
| 'stretch'
type HorizontalAlignment = 'center'
| 'start'
| 'end'
| 'between'
| 'around'
| 'evenly'
| 'stretch'
type Direction = 'row'
| 'column'
| 'row-reverse'
| 'column-reverse'
type Wrap = 'wrap'
| 'nowrap'
| 'wrap-reverse'
type Responsive<T> = T | {
default?: T
xs?: T
sm?: T
md?: T
lg?: T
}
type Alignment = {
horizontal?: HorizontalAlignment
vertical?: VerticalAlignment
}
PropDefaultPurpose
element div Sets the HTML tag used for the flex container.
gap 20px Sets the gap between flex items.
alignment start Sets the horizontal and vertical alignment of flex items.
direction row Sets the direction of flex items.
wrap nowrap Sets the wrapping of flex items.
className - Pass additional CSS classes for the element.

Request improvement for this page