---
title: FlexGrid
---

import LibraryInfo from '@theme/LibraryInfo'
import { Feedback, PropsTable } from '../../src/components'
import { FlexGrid, FlexGridRow, FlexGridCol } from '@telus-uds/components-web'
import { FlexGrid as BaseDocs } from 'docs/@telus-uds/components-base'

# FlexGrid

<LibraryInfo />

```jsx live
<FlexGrid>
  <FlexGridRow>
    <FlexGridCol xs={3}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>1/4</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={3}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>2/4</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={3}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>3/4</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={3}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>4/4</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
</FlexGrid>
```

## Introduction

A mobile-first flexbox grid based on the "12-column grid" pattern.

[Follow the appropriate instructions](importing-components) to add this component in to your app.

## Guidance

:::info
FlexGridCol and FlexGridRow can also be used with dot notation. E.g. `<FlexGrid.Row />` and `<FlexGrid.Col />`.
This is useful when you want to import the FlexGrid component without importing the subcomponents separately,
:::

Use FlexGrid to build grid layouts based on divisions of 12: imagine the screen width as being divided into
12 slices: each FlexGrid row contains one or more columns which occupy a given number of those 12 slices.

A FlexGrid is built using a `<FlexGrid>` outer container which contains one or more `<FlexGridRow>`s, then
each row contains one or more `<FlexGridCol>` columns, each of which claims a number of the 12 slices
available for the overall row. The number of slices claimed may vary by viewport, allowing grids to be created
that change shape based on the viewport.

None of the FlexGrid components depend on being direct children of each other and all may contain non-FlexGrid
children, so FlexGrid may be used with other layout and spacing components such as
[Divider](divider), [Spacer](spacer), [Box](box)
and [StackView](stack-view).

On the web, if you would like to reset the `zIndex` to `auto` for `FlexGrid`, `FlexGridRow`, and `FlexGridColumn`, you can use `forceZIndex` flag available in the `themeOptions` prop on the `ThemeProvider` and pass its value as `true`

See below for detailed documentation on [each of the subcomponents](#subcomponents).

```jsx live
<FlexGrid outsideGutter={false}>
  <FlexGridRow>
    <FlexGridCol xs={12} md={4}>
      <Typography variant={{ size: 'small' }}>Three 4-slice columns</Typography>
    </FlexGridCol>
    <FlexGridCol xs={12} md={4}>
      <Typography variant={{ size: 'small' }}>...that collapse to 12-slice full width</Typography>
    </FlexGridCol>
    <FlexGridCol xs={12} md={4}>
      <Typography variant={{ size: 'small' }}>...when used on narrow screens.</Typography>
    </FlexGridCol>
  </FlexGridRow>
  <FlexGridRow>
    <FlexGridCol xs={12}>
      <Divider space={2} />
    </FlexGridCol>
  </FlexGridRow>
  <FlexGridRow>
    <FlexGridCol xs={4} sm={3} lg={2}>
      <Typography variant={{ size: 'small' }}>Narrow column</Typography>
    </FlexGridCol>
    <FlexGridCol xs={8} sm={9} lg={10}>
      <Typography variant={{ size: 'small' }}>
        ...then a wider column that claims more space on wider screens.
      </Typography>
    </FlexGridCol>
  </FlexGridRow>
</FlexGrid>
```

### Alternatives

For simpler or more flexible grid layouts, consider using [StackView](stack-view).
Many grid layouts can be achieved by using `<StackView>` columns inside `<StackView direction="row">`
rows, and StackView directions may be made conditional on viewport (for example,
`<StackView direction={{ xs: 'column', md: 'row' }} />`).

For table grids where columns have the same width across different rows and where assisted-technology users
may better understand the content based on column and row headers, consider [Table](table)
(web only).

For grids where exactly 6 small captioned images follow a fixed responsive "waffle" pattern, consider
[WaffleGrid](waffle-grid) (web only).

### Accessibility

Reversing the order of content using `*Reverse` props should be done sparingly as it creates a disconnect
between the order of items seen visually on the screen and the order in which items are navigated by
keyboard navigation and by screen readers. This may make pages harder to navigate or more confusing
for sighted users using keyboard navigation or partially-sighted users who use screen readers as an
assistance rather than a replacement to scanning the screen visually.

#### `Tag` prop for semantic HTML

By default, FlexGrid renders a `<div>` on web and a `View` with no accessibility role on native apps.

To render a HTML attribute with a semantic meaning on web, pass a string naming a HTML semantic layout tag
to the `tag` prop. An `accessibilityRole` may also be passed if a role is needed in native apps.
Heading tags (`h1`, `h2`, `h3`, `h4`, `h5`, `h6`) are by default mapped to "heading" role in native apps.

See the `tag` entry in the [props table](#props) for the full list of supported tags.

```jsx live
<FlexGrid tag="footer">
  <FlexGridRow>
    <FlexGridCol xs={12} md={6}>
      <StackView tag="aside" space={2}>
        <Typography>Some text in a "footer".</Typography>
        <Typography>This column also contains an "aside".</Typography>
      </StackView>
    </FlexGridCol>
    <FlexGridCol xs={12} md={6}>
      <StackView tag="nav" space={1}>
        <Link href="/">Some links</Link>
        <Link href="/">Inside of a "footer"</Link>
        <Link href="/">Containing a "nav"</Link>
      </StackView>
    </FlexGridCol>
  </FlexGridRow>
</FlexGrid>
```

### Platform considerations

FlexGrid may be used in both web and native apps.

The prop `horizontalAlign` behaves somewhat differently between platforms:

- On web, text elements inside a column or row with the `horizontalAlign` prop applied will inherit left, right or
  center text alignment corresponding to that `horizontalAlign` prop, unless they either have explicitly set
  `textAlign` styles themselves or they have an ancestor below the FlexGrid that sets `textAlign`.
- In native apps, text elements do **not** inherit `textAlign` from the `horizontalAlign` FlexGrid element.

## Subcomponents

A FlexGrid consists of:

- One [FlexGrid](#flexgrid-1) container
- One or more [FlexGridRow](#flexgridrow) rows
- One or more [FlexGridCol](#flexgridcol) columns

## `FlexGrid`

All instances of FlexGrid should be within one FlexGrid container. This sets the grid's outer container
and provides all columns and rows with the `gutter` prop value.

### `gutter`

By default, 16px of spacing is provided between all columns in all rows. The amount of spacing here is
fixed and cannot be overridden; however, passing `false` to the `gutter` prop removes this default spacing
from all columns in all rows.

```jsx live
<FlexGrid gutter={false}>
  <FlexGridRow>
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>1/3</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>2/3</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>3/3</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
</FlexGrid>
```

### `outsideGutter`

The prop `outsideGutter` controls whether the 16px gutter should also be applied either side of the
FlexGrid row's content. Passing `outsideGutter={false}` while keeping `gutter={true}` applies the
spacing only between FlexGrid rows.

Do not set both `outsideGutter={false}` and `gutter={false}`; this will overcompensate and result
in the FlexGrid's rows being wider than its parent.

### `limitWidth`

By default, the FlexGrid sets the maximum width of the overall container to the maximum width of the
current viewport, at viewports `sm` or above. To disable this behaviour and set no max width on the
outer container at any viewport, set `limitWidth` to false.

### Content Min Width

The `contentMinWidth` prop allows you to set a minimum width for the content within the `FlexGrid`. This prop accepts responsive values for different viewports, enabling you to control the layout more effectively.
You need to set the `limitWidth` prop to `false` for this to take effect, as it will otherwise override the `contentMinWidth`

- Use `full` to make the content take the full width of the container
- Use `max` to make the content take the maximum content width defined in the `BaseProvider`
- Use a number (in pixels) to set a specific minimum width for the content

```jsx live
<FlexGrid
  contentMinWidth={{ xs: 'full', sm: 'max', md: 'max', lg: 400, xl: 500 }}
  limitWidth={false}
>
  <FlexGridRow>
    <FlexGridCol xs={12} md={12} xl={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography variant={{ size: 'small' }}>First column</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={12} md={12} xl={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography variant={{ size: 'small' }}>Second column</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={12} md={12} xl={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography variant={{ size: 'small' }}>Third column</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
</FlexGrid>
```

### Reverse props (`xsReverse`, `smReverse`...)

"Reverse" props may be set on the outer container for each viewport (`xsReverse`, `smReverse`,
`mdReverse`, `lgReverse`, `xlReverse`). These control the order of the FlexGrid's rows.

Pass one of these props as `true` to reverse the rows at this viewport and above, and pass one
as `false` to stop reversing at this viewport or above.

```jsx live
<FlexGrid xsReverse={false} smReverse={true} xlReverse={false}>
  <FlexGridRow>
    <FlexGridCol xs={12}>
      <Typography>At different viewports</Typography>
    </FlexGridCol>
  </FlexGridRow>
  <Divider space={4} />
  <FlexGridRow>
    <FlexGridCol xs={12}>
      <Typography>These rows do</Typography>
    </FlexGridCol>
  </FlexGridRow>
  <Divider space={4} />
  <FlexGridRow>
    <FlexGridCol xs={12}>
      <Typography>Reorder themselves</Typography>
    </FlexGridCol>
  </FlexGridRow>
</FlexGrid>
```

## `FlexGridRow`

Within the `FlexGrid` container, there should be one or more `FlexGridRow` rows.

### horizontalAlign, verticalAlign and distribute props

These props set the flex alignment behaviours of the immediate children of the row - i.e.
each `FlexGridCol` wrapper, and any elements placed in the row not inside a `FlexGridCol`.

These props take a string and are not responsive - it is not expected that flex rows change
their alignment styles based on the current viewport.

`horizontalAlign` and `distribute` apply horizontally and usually only have an effect when
there is leftover space (i.e. when the columns total does not add up to `12`). `verticalAlign`
applies vertically and usually only has an effect when columns in a row have different heights.

**`horizontalAlign`** values map to `justifyContent` and `textAlign` styles:

- `horizontalAlign="start"` maps to `{ justifyContent: 'flex-start', textAlign: 'left' }`
- `horizontalAlign="center"` maps to `{ justifyContent: 'center', textAlign: 'center' }`
- `horizontalAlign="end"` maps to `{ justifyContent: 'flex-end', textAlign: 'right' }`

Note that on native apps, due to React Native's limited text style inheritance, text within a
FlexGrid.Row will not inherit these `textAlign` values and alignment should still be set on
text components inside the row such as [Typography](typography).

```jsx live
<FlexGrid>
  <FlexGridRow horizontalAlign="start">
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>horizontalAlign</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>"start"</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
  <Divider space={4} />
  <FlexGridRow horizontalAlign="center">
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>horizontalAlign</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>"center"</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
  <Divider space={4} />
  <FlexGridRow horizontalAlign="end">
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>horizontalAlign</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>"end"</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
</FlexGrid>
```

**`distribute`** values set the distribution of any leftover space and also map to `justifyContent`:

- `distribute="around"` distributes space evenly before, after and between each item. It maps to `{ justifyContent: 'space-around' }`
- `distribute="between"` distributes space evenly between items, leaving the first and last items tight against the left and right
  edges of the Row. It maps to `{ justifyContent: 'space-between' }`

If both `horizontalAlign` and `distribute` props are set, `distribute` has priority and
the final `justifyContent` style of the rendered will be based on the `distribute` value
not the `horizontalAlign` value.

```jsx live
<FlexGrid>
  <FlexGridRow distribute="between">
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>distribute</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>"between"</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
  <Divider space={4} />
  <FlexGridRow distribute="around">
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>distribute</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>"around"</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
</FlexGrid>
```

**`verticalAlign`** values map to `alignItems` styles:

- `verticalAlign="top"` maps to `{ alignItems: 'flex-start' }`
- `verticalAlign="middle"` maps to `{ alignItems: 'center' }`
- `verticalAlign="bottom"` maps to `{ alignItems: 'flex-end' }`

```jsx live
<FlexGrid>
  <FlexGridRow verticalAlign="top">
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} horizontal={2} vertical={3}>
        <Typography>verticalAlign</Typography>
        <Typography>"top"</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} horizontal={2} vertical={1}>
        <Typography variant={{ size: 'micro' }}>Example</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
  <Divider space={4} />
  <FlexGridRow verticalAlign="middle">
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} horizontal={2} vertical={3}>
        <Typography>verticalAlign</Typography>
        <Typography>"middle"</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} horizontal={2} vertical={1}>
        <Typography variant={{ size: 'micro' }}>Example</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
  <Divider space={4} />
  <FlexGridRow verticalAlign="bottom">
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} horizontal={2} vertical={3}>
        <Typography>verticalAlign</Typography>
        <Typography>"bottom"</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={4}>
      <Box variant={{ background: 'light' }} horizontal={2} vertical={1}>
        <Typography variant={{ size: 'micro' }}>Example</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
</FlexGrid>
```

### Reverse props (`xsReverse`, `smReverse`...)

"Reverse" props may be set on the outer container for each viewport (`xsReverse`, `smReverse`,
`mdReverse`, `lgReverse`, `xlReverse`). These control the order of the columns within this row.

Pass one of these props as `true` to reverse the columns at this viewport and above, and pass one
as `false` to stop reversing at this viewport or above.

## `FlexGridCol`

Within each `FlexGridRow` row, there should be one or more `FlexGridCol` columns.

### `xs`, `sm`, `md`, `lg` and `xl`

These props take number values, where the number indicates how many twelths of the available width this
column tries to fill. For example, `3` means one quarter of the available width, `4` means a third, `6`
means half, `9` means three quarters and `12` means the column tries to fill the whole width.

This applies at and above the viewport specified, until another prop is specified. For example,
`<FlexGridCol xs={12} md={6} />` occupies the full width at viewports `xs` and `sm`, and occupies
half the available width at viewports `md`, `lg` and `xl`.

If the values for all the columns in a row exceed `12`, the content of that row will
wrap onto a new line. A common use of this prop is to create layouts that take up one
row on wide screens and which collapse down to multiple rows on narrower screens.

```jsx live
<FlexGrid gutter={false}>
  <FlexGridRow>
    <FlexGridCol xs={12} md={6} xl={4}>
      <Box right={{ xs: 0, md: 2 }} bottom={{ xs: 2, md: 0 }}>
        <Box variant={{ background: 'light' }} space={2}>
          <Typography variant={{ size: 'small' }}>First column</Typography>
        </Box>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={12} md={6} xl={4}>
      <Box right={{ xs: 0, xl: 2 }} bottom={{ xs: 2, xl: 0 }}>
        <Box variant={{ background: 'light' }} space={2}>
          <Typography variant={{ size: 'small' }}>Second column</Typography>
        </Box>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={12} md={12} xl={4}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography variant={{ size: 'small' }}>Third column</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
</FlexGrid>
```

### Offset props (`xsOffset`, `xsOffset`...)

Offset props may be used to leave an amount of space empty before a column at and above the given
viewport (`xsOffset`, `smOffset`, `mdOffset`, `lgOffset`, `xlOffset`).

Giving a column an offset prop gives similar visual output to preceding a column with an empty
column that has the equivalent responsive prop. For example, `<FlexGridCol xsOffset={3} mdOffset={6}>`
is similar to preceding a column with an empty column `<FlexGridCol xs={3} md={3} />`.

```jsx live
<FlexGrid>
  <FlexGridRow>
    <FlexGridCol xs={6} md={3}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography variant={{ size: 'small' }}>One</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={6} md={3}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography variant={{ size: 'small' }}>Two</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={6} xsOffset={6} md={3} mdOffset={3}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography variant={{ size: 'small' }}>Skip a few</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
  <Spacer space={4} />
  <FlexGridRow>
    <FlexGridCol xs={6} md={3} xsOffset={6}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography variant={{ size: 'small' }}>Ninety-nine</Typography>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={6} md={3}>
      <Box variant={{ background: 'light' }} space={2}>
        <Typography variant={{ size: 'small' }}>One hundred</Typography>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
</FlexGrid>
```

### `horizontalAlign` prop

`FlexGridCol`'s `horizontalAlign` prop is quite different to `FlexGridRow`'s. It sets the text alignment
of the wrapper (not `justifyContent`), and it is a responsive prop: it may take a string (`'left'`, `'center'`
or `'right'`) or an object keyed by viewports (e.g. `{ xs: 'left', lg: 'right' }`).

Note that because this works using HTML text inheritance rules, it has no effect on child elements that
already have a text alignment specified in their own styles, and it has no effect in native apps.

```jsx live
<FlexGrid>
  <FlexGridRow>
    <FlexGridCol xs={4} horizontalAlign="left">
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>Left</Typography>
        <Button variant={{ size: 'small' }} onPress={() => {}}>
          Left
        </Button>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={4} horizontalAlign="center">
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>Center</Typography>
        <Button variant={{ size: 'small' }} onPress={() => {}}>
          Left?
        </Button>
      </Box>
    </FlexGridCol>
    <FlexGridCol xs={4} horizontalAlign="right">
      <Box variant={{ background: 'light' }} space={2}>
        <Typography>Right</Typography>
        <Button variant={{ size: 'small' }} onPress={() => {}}>
          Left!
        </Button>
      </Box>
    </FlexGridCol>
  </FlexGridRow>
</FlexGrid>
```

## Props

### FlexGrid

<PropsTable base={BaseDocs} />

### FlexGridCol

<PropsTable base={BaseDocs.Col} />

### FlexGridRow

<PropsTable base={BaseDocs.Row} />

## Feedback

<Feedback component="FlexGrid" />
