网格布局

对齐目标:@astryxdesign/core v0.7.1 · Grid

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10
Item 11
Item 12

使用方法 #

Grid 用 CSS 网格把子元素排进行和列,解决的是二维布局:卡片墙、仪表盘面板,以及任何需要多列对齐的区域。一维排布交给 Stack 系列,只有当行和列都要对齐时才换成 Grid。

给出列配置就能开始使用:

Vue
<script setup lang="ts">
import { Grid } from '@astryx-vue/core/Grid'
</script>

<template>
  <Grid :columns="3" :gap="4">
    <div>第一项</div>
    <div>第二项</div>
    <div>第三项</div>
  </Grid>
</template>

columns 传数字表示固定列数,每一列等宽。传对象则表示响应式列:minWidth 是每列的最小宽度(像素),网格按可用宽度自己决定能放几列,窄屏时自动减少列数。

Vue
<template>
  <Grid :columns="{ minWidth: 280 }" :gap="4">
    <div>第一项</div>
    <div>第二项</div>
  </Grid>
</template>

响应式配置里还有两个可选项。repeat 决定最后一行没排满时留下的空隙如何处理:'fill'(默认)保留空的轨道,所以每列宽度始终一致;'fit' 收起空轨道,让实际存在的列把整行撑满。max 限制最多几列,避免大屏上每一列被拉得过宽。列数上限加在轨道的最小尺寸上而不是最大尺寸上,因此列数不会超过 max,而排不满时留下的列依然会撑满整行,右侧不会出现空白。

gap 控制行列间距,rowGap 与 columnGap 可以分别在行、列方向覆盖它,取值都来自 Astryx 的间距刻度。rowHeight 用像素固定隐式行高,配合 GridSpan 的 rows 可以做出跨若干行的布局。

需要某一项跨列或跨行时,用 GridSpan 包一层。它的 columns 给数字表示跨几列,给 'full' 表示跨越整行;rows 表示跨几行。

Vue
<template>
  <Grid :columns="4" :gap="3">
    <GridSpan :columns="2">
      跨两列的项目
    </GridSpan>
    <div>普通项目</div>
    <GridSpan columns="full">
      跨越整行
    </GridSpan>
  </Grid>
</template>

width、height、maxWidth、minHeight 控制容器自身的尺寸,数字按像素处理,字符串原样使用。align 控制项目在行内的垂直对齐(align-items),justify 控制水平对齐(justify-items),两者默认都是 stretch,也就是撑满所在的单元格。

列模板写在容器的 CSS 变量 --astryx-grid-template-columns 上,而不是内联的 grid-template-columns 声明。因此在自有样式或媒体查询里重写这个变量、或者直接重写 grid-template-columns,都能生效。

解剖

部分说明
网格容器按行和列排列使用方提供的项的二维布局容器。
跨列项可选的 GridSpan,改变某一项参与列或行的方式。

最佳实践 #

指引实践
推荐布局需要随屏幕宽度变化时用响应式列(:columns="{ minWidth: 280 }"),让网格自己决定列数,不必再写媒体查询。
推荐大屏上用 max 给列数封顶。没有上限时每一列会被拉得很宽,一行里的内容彼此离得太远。
推荐需要每列宽度一致时保留默认的 repeat: 'fill';希望不足一行的项目撑开剩余空间时改用 'fit'。
推荐间距交给 gap,容器自身的尺寸交给 width、maxWidth 等属性,不要在子元素上再加外边距。
避免手写 display: grid 和 grid-template-columns 并自己计算间距,Grid 已经把这些处理好了。
避免用允许换行的 HStack 冒充网格。一维排布用 Stack,行和列都要对齐时才用 Grid。
避免用 columns 的对象形式表达固定列数。固定列数直接传数字,语义更清楚。

示例 #

网格布局 — 仪表盘布局

Weekly Traffic Page views and unique visitors over the last 7 days
Revenue$48,290
Active Users12,841
Conversion3.2%
Avg Response245ms
Recent Activity Latest events across all projects

仪表盘把一张大的流量卡片、四项指标和一整行动态排在一起。

网格布局 — 卡片画廊

Getting StartedLearn the basics of the platform.
ComponentsBrowse the full component library.
Design TokensColors, spacing, and typography.
ThemingCustomize the look and feel.
AccessibilityBuild inclusive experiences.
PatternsCommon UI composition patterns.

卡片保持一致的最小宽度,画廊变窄时自动换行。

网格布局 — 自适应列宽

Design Systems8 members
Frontend Platform12 members
Developer Experience6 members
Accessibility4 members
Performance7 members
Mobile Infrastructure9 members

拖动分隔条。一行没排满时,卡片会把剩下的宽度撑满。

网格布局 — 跨列

Featured Release Astryx 4.0 is now available with new layout primitives, refreshed tokens, and improved theming support across the system.
Components54 available
Templates28 available
Tokens120 defined
Themes6 published
Icons312 available
Patterns18 documented
Contributors42 active
Community Showcase See how teams are building with Astryx across the organization

重点卡片跨过若干列和行,旁边是较小的统计项。