CSS Grid
使用全套响应式栅格工具,快速管理栅格列、导航、组件等的布局、对齐和尺寸。
如果您是栅格布局的新手或不熟悉栅格布局,建议您阅读这篇 CSS-Tricks 栅格 指南。
父元素的属性
display
要定义一个 grid
容器,您必须将 display
CSS 属性指定为以下值之一:grid
或 inline-grid
。
我是一个栅格容器!
<Box sx={{ display: 'grid' }}>…</Box>
<Box sx={{ display: 'inline-grid' }}>…</Box>
grid-template-rows
grid-template-rows
属性定义了栅格行的线名称和轨道尺寸函数。
1
2
3
按 Enter 键开始编辑
grid-template-columns
grid-template-columns
属性定义了栅格列的线名称和轨道尺寸函数。
1
2
3
按 Enter 键开始编辑
gap
gap: size
属性指定 CSS 栅格内不同项目之间的间隙。
1
2
3
4
按 Enter 键开始编辑
row-gap & column-gap
row-gap
和 column-gap
CSS 属性允许独立指定行和列的间隙。
1
2
3
4
按 Enter 键开始编辑
grid-template-areas
grid-template-area
属性通过引用使用 grid-area
属性指定的栅格区域的名称来定义栅格模板。
头部
主要内容
侧边栏
底部
按 Enter 键开始编辑
grid-auto-columns
grid-auto-column
属性指定隐式创建的栅格列轨道或轨道模式的大小。
span 2
4 / 5
按 Enter 键开始编辑
span 2
4 / 5
按 Enter 键开始编辑
grid-auto-flow
grid-auto-flow
属性控制自动放置算法的工作方式,精确指定自动放置的项目如何流入栅格。
1
2
3
4
5
按 Enter 键开始编辑
子元素的属性
grid-column
grid-column
属性是 grid-column-start
+ grid-column-end
的简写。您可以在 grid-auto-columns 示例中看到它的用法。
您可以设置起始线和结束线
<Box sx={{ gridColumn: '1 / 3' }}>…
或者设置要跨越的列数
<Box sx={{ gridColumn: 'span 2' }}>…
grid-row
grid-row
属性是 grid-row-start
+ grid-row-end
的简写。您可以在 grid-auto-rows 示例中看到它的用法。
您可以设置起始线和结束线
<Box sx={{ gridRow: '1 / 3' }}>…
或者设置要跨越的行数
<Box sx={{ gridRow: 'span 2' }}>…
grid-area
grid-area
属性允许您为项目命名,以便可以通过使用 grid-template-areas
属性创建的模板引用它。您可以在 grid-template-area 示例中看到它的用法。
<Box sx={{ gridArea: 'header' }}>…
API
import { grid } from '@mui/system';
导入名称 | Prop | CSS 属性 | Theme key |
---|---|---|---|
gap |
gap |
gap |
none |
columnGap |
columnGap |
column-gap |
none |
rowGap |
rowGap |
row-gap |
none |
gridColumn |
gridColumn |
grid-column |
none |
gridRow |
gridRow |
grid-row |
none |
gridAutoFlow |
gridAutoFlow |
grid-auto-flow |
none |
gridAutoColumns |
gridAutoColumns |
grid-auto-columns |
none |
gridAutoRows |
gridAutoRows |
grid-auto-rows |
none |
gridTemplateColumns |
gridTemplateColumns |
grid-template-columns |
none |
gridTemplateRows |
gridTemplateRows |
grid-template-rows |
none |
gridTemplateAreas |
gridTemplateAreas |
grid-template-areas |
none |
gridArea |
gridArea |
grid-area |
none |