Grid

Flexbox grid system with variable amount of columns
Import

Usage

Grid component provides simple grid system based on flexbox container:

1
2
3
<Grid>
<Grid.Col span={4}>1</Grid.Col>
<Grid.Col span={4}>2</Grid.Col>
<Grid.Col span={4}>3</Grid.Col>
</Grid>

Gutter

Customize spacing between columns with gutter prop on Grid component. Use xs, sm, md, lg, xl values to set spacing from theme.spacing or number to set spacing in px:

<Grid gutter="xl" /> // -> theme.spacing.xl
<Grid gutter={40} /> // -> 40px

Grow

Set grow prop on Grid component to force last row take 100% of container width:

1
2
3
4
5
Gutter
xs
sm
md
lg
xl
<Grid grow>
<Grid.Col span={4}>1</Grid.Col>
<Grid.Col span={4}>2</Grid.Col>
<Grid.Col span={4}>3</Grid.Col>
<Grid.Col span={4}>4</Grid.Col>
<Grid.Col span={4}>5</Grid.Col>
</Grid>

Column offset

Set offset prop on Grid.Col component to create gaps in grid. offset adds left margin to target column and has the same units as span:

1
2
3
<Grid>
<Grid.Col span={3}>1</Grid.Col>
<Grid.Col span={3}>2</Grid.Col>
<Grid.Col span={3} offset={3}>3</Grid.Col>
</Grid>

Multiple rows

Once children columns span and offset sum exceeds columns prop (defaults to 12), columns are placed on next row:

1
2
3
4
<Grid>
<Grid.Col span={4}>1</Grid.Col>
<Grid.Col span={4}>2</Grid.Col>
<Grid.Col span={4}>3</Grid.Col>
<Grid.Col span={4}>4</Grid.Col>
</Grid>

Justify and align

Since grid is a flexbox container, you can control justify-content and align-items properties:

1
2
3
<Grid>
<Grid.Col span={3} style={{ minHeight: 80 }}>1</Grid.Col>
<Grid.Col span={3} style={{ minHeight: 120 }}>2</Grid.Col>
<Grid.Col span={3}>3</Grid.Col>
</Grid>

Responsive columns

Use breakpoint props (xs, sm, md, lg, xl) to make columns respond to viewport changes. You can configure breakpoint values in MantineProvider.

In this example up to md there will be 1 column, from md to lg there will be 2 columns and from lg and up, there will be 4 columns:

1
2
3
4
<Grid>
<Grid.Col md={6} lg={3}>1</Grid.Col>
<Grid.Col md={6} lg={3}>2</Grid.Col>
<Grid.Col md={6} lg={3}>3</Grid.Col>
<Grid.Col md={6} lg={3}>4</Grid.Col>
</Grid>

Change columns count

By default, grid uses 12 columns layout, you can change it by setting columns prop on Grid component. Note that in this case, columns span and offset will be calculated relative to this value.

In this example, first column takes 50% with 12 span (12/24), second and third take 25% (6/24):

1
2
3
<Grid columns={24}>
<Grid.Col span={12}>1</Grid.Col>
<Grid.Col span={6}>2</Grid.Col>
<Grid.Col span={6}>3</Grid.Col>
</Grid>

Wrap Grid.Col component

Grid.Col component depends on context props from Grid component. If you want to enhance it with additional logic – pass all props to child Gird.Col:

// Example Grid.Col component wrapper, used in all demos
import { Grid, Text, useMantineTheme } from '@mantine/core';
function ColWrapper(props: React.ComponentProps<typeof Grid.Col>) {
const theme = useMantineTheme();
const background = theme.colorScheme === 'dark' ? theme.colors.dark[8] : theme.colors.blue[0];
return (
<Grid.Col {...props} style={{ background, padding: theme.spacing.md, ...props.style }}>
<Text
color={theme.colorScheme === 'dark' ? 'gray' : 'blue'}
size="xl"
weight={700}
align="center"
>
{props.children}
</Text>
</Grid.Col>
);
}
// Later
<Grid>
<ColWrapper span={4}>1</ColWrapper>
<ColWrapper span={4}>2</ColWrapper>
<ColWrapper span={4}>3</ColWrapper>
</Grid>;
Build fully functional accessible web applications faster than ever
Feedback
Your feedback is most valuable contribution to the project, please share how you use Mantine, what features are missing and what is done good
Leave feedback