Grid 栅格
Material Design 响应式布局的栅格可适应屏幕大小和方向,确保布局在不同尺寸之间的一致性。
Grid 栅格组件 能确保不同布局间的视觉层面的舒适感,同时在众多不同设计中保持灵活性。 Material Design 基于 12 列的网格布局来做到 UI 的响应式。
警告,不应将Grid
与data grid相互混淆,那是一个和布局网格相同的网格 For a data grid head to the DataGrid
component.
<Grid container spacing={2}>
<Grid item xs={8}>
<Item>xs=8</Item>
</Grid>
<Grid item xs={4}>
<Item>xs=4</Item>
</Grid>
<Grid item xs={4}>
<Item>xs=4</Item>
</Grid>
<Grid item xs={8}>
<Item>xs=8</Item>
</Grid>
</Grid>
有断点的栅格
组件可能有多个宽度定义,这会导致组件根据指定的断点大小来布局 大的断点的宽度值会覆盖小的断点的宽度值
举个例子,xs={12} sm={6}
会在视图宽度为 的时候让组件占据视图一半的宽度(6列) 对于较小的视图宽度,则会占据所有的12列
<Grid container spacing={2}>
<Grid item xs={6} md={8}>
<Item>xs=6 md=8</Item>
</Grid>
<Grid item xs={6} md={4}>
<Item>xs=6 md=4</Item>
</Grid>
<Grid item xs={6} md={4}>
<Item>xs=6 md=4</Item>
</Grid>
<Grid item xs={6} md={8}>
<Item>xs=6 md=8</Item>
</Grid>
</Grid>
Spacing 间距
控制子控件之间的间距,可以使用spacing
属性 Spacing可以是包含小数和整型值还有字符串的任意大于0的数 属性值会使用theme.spacing()
助手来转换成css属性
<Grid container spacing={2}>
行、列间距
rowSpacing
和columnSpacing
属性允许你独立指定列间距和行间距 他和CSS Grid里的row-gap
还有 column-gap
属性相同
<Grid container rowSpacing={1} columnSpacing={{ xs: 1, sm: 2, md: 3 }}>
<Grid item xs={6}>
<Item>1</Item>
</Grid>
<Grid item xs={6}>
<Item>2</Item>
</Grid>
<Grid item xs={6}>
<Item>3</Item>
</Grid>
<Grid item xs={6}>
<Item>4</Item>
</Grid>
</Grid>
响应式的值
You can switch the props' value based on the active breakpoint. For instance, we can implement the "recommended" responsive layout grid of Material Design.
<Grid container spacing={{ xs: 2, md: 3 }} columns={{ xs: 4, sm: 8, md: 12 }}>
{Array.from(Array(6)).map((_, index) => (
<Grid item xs={2} sm={4} md={4} key={index}>
<Item>xs=2</Item>
</Grid>
))}
</Grid>
Responsive values is supported by:
columns
columnSpacing
direction
rowSpacing
spacing
- 系统中的所有其它属性
交互式
Below is an interactive demo that lets you explore the visual results of the different settings:
<Grid
container
direction="row"
justifyContent="center"
alignItems="center"
>
自适应布局
The Auto-layout makes the items equitably share the available space. That also means you can set the width of one item and the others will automatically resize around it.
<Grid container spacing={3}>
<Grid item xs>
<Item>xs</Item>
</Grid>
<Grid item xs={6}>
<Item>xs=6</Item>
</Grid>
<Grid item xs>
<Item>xs</Item>
</Grid>
</Grid>
负边距
Set one of the size breakpoint props to "auto"
instead of true
/ a number
to size a column based on the natural width of its content.
<Grid container spacing={3}>
<Grid item xs="auto">
<Item>variable width content</Item>
</Grid>
<Grid item xs={6}>
<Item>xs=6</Item>
</Grid>
<Grid item xs>
<Item>xs</Item>
</Grid>
</Grid>
复杂的栅格
The following demo doesn't follow the Material Design guidelines, but illustrates how the grid can be used to build complex layouts.
Full resolution 1920x1080 • JPEG
ID: 1030114
Remove
嵌套栅格
The container
and item
props are two independent booleans; they can be combined to allow a Grid component to be both a flex container and child.
<Grid container spacing={1}>
<Grid container item spacing={3}>
<FormRow />
</Grid>
<Grid container item spacing={3}>
<FormRow />
</Grid>
<Grid container item spacing={3}>
<FormRow />
</Grid>
</Grid>
⚠️ Defining an explicit width to a Grid element that is flex container, flex item, and has spacing at the same time lead to unexpected behavior, avoid doing it:
<Grid spacing={1} container item xs={12}>
If you need to do such, remove one of the props.
列
You can change the default number of columns (12) with the columns
prop.
<Grid container spacing={2} columns={16}>
<Grid item xs={8}>
<Item>xs=8</Item>
</Grid>
<Grid item xs={8}>
<Item>xs=8</Item>
</Grid>
</Grid>
设计局限
负边距
The spacing between items is implemented with a negative margin. This might lead to unexpected behaviors. For instance, to apply a background color, you need to apply display: flex;
to the parent.
white-space: nowrap;
The initial setting on flex items is min-width: auto
. It's causing a positioning conflict when the children is using white-space: nowrap;
. You can experience the issue with:
<Grid item xs>
<Typography noWrap>
In order for the item to stay within the container you need to set min-width: 0
. In practice, you can set the zeroMinWidth
prop:
<Grid item xs zeroMinWidth>
<Typography noWrap>
Truncation should be conditionally applicable on this long line of text as this is a much longer line than what the container can support.
Truncation should be conditionally applicable on this long line of text as this is a much longer line than what the container can support.
Truncation should be conditionally applicable on this long line of text as this is a much longer line than what the container can support.
direction: column | column-reverse
The xs
, sm
, md
, lg
, and xl
props are not supported within direction="column"
and direction="column-reverse"
containers.
They define the number of grids the component will use for a given breakpoint. They are intended to control width using flex-basis
in row
containers but they will impact height in column
containers. If used, these props may have undesirable effects on the height of the Grid
item elements.
CSS 栅格布局
The Grid
component is using CSS flexbox internally. But as seen below, you can easily use the system and CSS Grid to layout your pages.
<Box display="grid" gridTemplateColumns="repeat(12, 1fr)" gap={2}>
<Box gridColumn="span 8">
<Item>xs=8</Item>
</Box>
<Box gridColumn="span 4">
<Item>xs=4</Item>
</Box>
<Box gridColumn="span 4">
<Item>xs=4</Item>
</Box>
<Box gridColumn="span 8">
<Item>xs=8</Item>
</Box>
</Box>
系统属性
As a CSS utility component, the Grid
supports all system
properties. You can use them as props directly on the component. For instance, a padding:
<Grid item p={2}>