Grid栅格

24 栅格布局。Row 排列其中的 Col;一列占 24 格中的 span 格,放不下时整列换到下一行。行可以对齐和分布各列,列可以偏移、重新排序、隐藏,并按屏幕宽度调整。响应式取值由 CSS 完成,因此服务器和浏览器渲染出同一个页面。Breakpoints 让你的代码知道屏幕匹配哪些断点。

引入
import { Row, Col, Breakpoints } from 'svelte-ant-design';
链接
src/lib/Grid 问题反馈 组件 README

何时使用

  • 需要把页面或区块排布在等宽、逐行对齐的列上时,使用栅格。
  • 当布局需要随屏幕宽度变化时,使用响应式属性(xs 到 xxxl)。
  • 只需要一维间距、不需要列数时,直接在自己的样式中使用 flex 或 CSS grid 更简单。

代码演示

col-24
col-12
col-12
col-8
col-8
col-8
col-6
col-6
col-6
col-6

基本用法

把 Col 放进 Row。一列占 24 格中的 span 格;Row 与父元素等宽。

gutter={16}
col-6
col-6
col-6
col-6
gutter={{ xs: 8, sm: 16, md: 24, lg: 32 }} (resize the window)
col-6
col-6
col-6
col-6
gutter={[16, 24]}: horizontal and vertical
col-6
col-6
col-6
col-6
col-6
col-6
col-6
col-6
gutter="2rem": a CSS length
col-6
col-6
col-6
col-6

栅格间隔

gutter 是列之间的间隔:像素值、CSS 长度、按断点给出的值,或 [水平, 垂直]。它基于 flex 的 gap,因此 Row 不会超出父元素,页面也不会出现横向滚动条。

col-8
col-8 offset-8
col-6 offset-6
col-6 offset-6
col-12 offset-6

左右偏移

offset 会在该列前空出相应的格数,例如 offset={4} 会让它向行尾方向偏移四格。

push={6} and pull={18}: the first column in the DOM is drawn second
col-18 push-6
col-6 pull-18

栅格排序

push 与 pull 只在视觉上移动列。DOM 顺序、Tab 顺序和读屏顺序保持不变;两者同时设置时,按差值移动。

justify="start"
col-4
col-4
col-4
col-4
justify="center"
col-4
col-4
col-4
col-4
justify="end"
col-4
col-4
col-4
col-4
justify="space-between"
col-4
col-4
col-4
col-4
justify="space-around"
col-4
col-4
col-4
col-4
justify="space-evenly"
col-4
col-4
col-4
col-4

排版

justify 决定一行中各列的排列方式:start、center、end、space-between、space-around 与 space-evenly。

align="top"
col-4
col-4
col-4
col-4
align="middle"
col-4
col-4
col-4
col-4
align="bottom"
col-4
col-4
col-4
col-4
align="stretch"
col-4
col-4
col-4
col-4

对齐

align 决定一行中各列的垂直对齐方式:top(默认)、middle、bottom 与 stretch。

Written 1, 2, 3, 4; drawn 1, 2, 3, 4
1 order-1
2 order-2
3 order-3
4 order-4
Written 1, 2, 3, 4; drawn 4, 3, 2, 1
1 order-4
2 order-3
3 order-2
4 order-1

排序

order 改变列的绘制顺序,但不改变它们在 DOM 中的位置:Tab 与读屏仍按书写顺序,因此不要用它来调整阅读顺序。

flex={2} and flex={3}: a number n is "n n auto"
2 / 5
3 / 5
flex="100px" and flex="auto": a fixed width and the rest
100px
Fill rest
flex="1 1 200px" and flex="0 1 300px": a raw flex value
1 1 200px
0 1 300px
flex="none" and flex="auto"
none
auto with no-wrap

Flex 填充

flex 可以是数字(n n auto)、单个长度如 100px(固定宽度,即 0 0 100px),或字符串形式的任意 CSS flex 值。

wrap
wrap={true}
Three columns of 12: the third does not fit in a line
col-12
col-12
col-12

换行

默认情况下,放不下的列会整列换到下一行。设置 wrap={false} 后,列保持在同一行并保留各自宽度,超出 Row 的末端,可由父元素滚动。

Outer gutter 16; the inner Row on the left has gutter 4, the one on the right none
12 / 24 of 12
12 / 24 of 12
8
16

嵌套

Col 中的 Row 会填满该列的内容区域,并拥有自己的 gutter。没有设置 gutter 的内层 Row 不会继承外层的 gutter。

xs={0} md={8}: the first column shows from 768 px (resize the window)
Only from md
Always
span={0}: always hidden
col-12
Never seen
col-12

隐藏列

span 为 0 等同于 display: none:该列会从布局、Tab 顺序和读屏中消失。配合断点,xs={0} md={8} 可在小屏幕上隐藏该列。

xs 2/20/2, sm 4/16/4, md 6/12/6, lg 8/8/8, xl 10/4/10, xxl 11/2/11, xxxl 12/0/12
Col
Col
Col

响应式布局

xs、sm、md、lg、xl、xxl 与 xxxl 从相应的屏幕宽度起生效(576、768、992、1200、1600、1920 px)。xs 是基础值;未设置的断点沿用更小断点的值。请调整窗口宽度查看。

flex 100% / 50% / 40% / 20% / 10% from xs / sm / md / lg / xl
Col
Col
Col
Col
Col

响应式 Flex

断点对象也可以包含 flex,让列占据一行的比例随屏幕变化。请调整窗口宽度查看。

xs span 5 offset 1; lg span 6 offset 2 (resize the window)
Col
Col
Col
xs={12}, md order 2 / 1: drawn in the other order from md
First in the DOM
Second in the DOM
md push 6 and pull 18: swapped visually from md, the DOM order stays
First in the DOM
Second in the DOM

更多响应式属性

span、offset、push、pull、order 与 flex 可写在断点对象中(xs={6} 等同于 xs={{ span: 6 }})。每个键各自继承:lg 只给出 span 时,xs 的 offset 仍然生效。

Horizontal gutter (px)
Vertical gutter (px)
Columns in a row
<Row gutter={[16, 16]}> with 4 columns of span 6, twice
Column
Column
Column
Column
Column
Column
Column
Column

演示

选择间隔和每行的列数。每个控件都有各自独立的状态。

screens.current xs
screens.xs ... screens.xxxl (xs is true below 576 px, the others from their width up)
xs: true
sm: false
md: false
lg: false
xl: false
xxl: false
xxxl: false
{#if screens.md}: markup that depends on the screen
The screen is narrower than 768 px

在代码中读取断点

new Breakpoints() 让脚本知道当前屏幕匹配哪些断点。在浏览器中,首次渲染的值就是正确的;在服务器上则取 options.fallback。请调整窗口宽度查看。

Row API

属性说明类型默认值
align一行中各列的垂直对齐方式:top、middle、bottom 或 stretch。对象形式可按断点分别设置。RowAlign | Partial<Record<Breakpoint, RowAlign>>'top'
justify一行中各列的水平排列方式:start、end、center、space-around、space-between 或 space-evenly。对象形式可按断点分别设置。RowJustify | Partial<Record<Breakpoint, RowJustify>>'start'
gutter列之间的间隔。数字为像素,字符串为 CSS 长度(2rem、5vw、calc()),对象可按断点分别设置,数组为 [水平, 垂直](每一项可以是前三种形式之一)。不是长度的字符串会被忽略;负数或非有限数在开发环境中会抛出错误。Gutter | [Gutter, Gutter]0
wrap放不下的列是否换到下一行。为 false 时,列保持在同一行并保留各自宽度,超出 Row 的末端。booleantrue
children各列(Col)。其他子元素按普通 flex 子项排列。Snippet—
classRow 根元素的额外类名。string—
style根元素的内联样式。它写在组件自身的值之后,因此在同一属性上优先生效。string—
native attributes其余属性都会传给根元素 <div>:id、role、aria-*、data-* 与事件处理函数。HTMLAttributes<HTMLDivElement>—
与 Ant Design 的差异:gutter 是 flex 的 gap,而不是负外边距加列上的内边距,因此 Row 不会超出父元素(Ant Design 的 Row 会,导致页面出现横向滚动条),列自身的背景也不会铺满间隔;响应式取值仅由 CSS 完成,断点固定(576、768、992、1200、1600、1920 px),无法通过主题修改;Breakpoints 取代 Grid.useBreakpoint 钩子,首次渲染即正确;没有 classNames/styles,因为 Ant Design 的三个版本都没有为栅格提供它们。

Col 的 API

属性说明类型默认值
span占 24 格中的多少格。0 等同于 display: none。不设置时,列保持内容宽度。负数或非有限数在开发环境中会抛出错误。number—
offset该列左侧空出的格数。number0
push让该列向行尾方向移动相应格数,仅影响视觉效果,不改变 DOM 顺序。number0
pull让该列向行首方向移动相应格数,仅影响视觉效果。同时设置 push 与 pull 时,按差值移动。number0
orderflex 的 order:决定列的绘制顺序,而不是它在 DOM 中的位置。整数,可为负。number0
flex数字 n 即 flex: n n auto(0 也是有效值);纯数字字符串为 n 1 0px;单个长度或百分比(100px、30%)为固定宽度,即 0 0 <长度>;其他字符串按原样使用。它优先于 span 的宽度。string | number—
xs从 0 px 起:所有宽度的基础值。数字表示 span;对象可使用 ColSize 的属性。number | ColSize—
sm从 576 px 起。数字表示 span;对象可使用 ColSize 的属性。number | ColSize—
md从 768 px 起。数字表示 span;对象可使用 ColSize 的属性。number | ColSize—
lg从 992 px 起。数字表示 span;对象可使用 ColSize 的属性。number | ColSize—
xl从 1200 px 起。数字表示 span;对象可使用 ColSize 的属性。number | ColSize—
xxl从 1600 px 起。数字表示 span;对象可使用 ColSize 的属性。number | ColSize—
xxxl从 1920 px 起。数字表示 span;对象可使用 ColSize 的属性。number | ColSize—
children列的内容。Snippet—
classCol 根元素的额外类名。string—
style根元素的内联样式。它写在组件自身的值之后,因此在同一属性上优先生效。string—
native attributes其余属性都会传给根元素 <div>:id、role、aria-*、data-* 与事件处理函数。HTMLAttributes<HTMLDivElement>—

ColSize:断点对象的键

属性说明类型默认值
span占 24 格中的多少格;0 会在该断点隐藏此列。number—
offset该列左侧空出的格数。number—
push向行尾方向的视觉偏移,单位为格。number—
pull向行首方向的视觉偏移,单位为格。number—
orderflex 的 order。number—
flex原始 flex 值,规则与 flex 属性相同。string | number—

Breakpoints:在代码中读取断点

属性说明类型默认值
new Breakpoints(options?)创建读取器。数值立即从屏幕读取,因此在浏览器中首次渲染就是正确的。只有在响应式代码读取时才会存在监听器。(options?: BreakpointsOptions) => Breakpoints—
options.fallback在没有屏幕的环境(服务器)中所报告的断点:各标志与恰好等于该断点起始宽度的屏幕一致。未知值在开发环境中会抛出错误。Breakpoint'xs'
xs宽度小于 576 px 时为 true。boolean—
sm宽度达到 576 px 及以上时为 true。boolean—
md宽度达到 768 px 及以上时为 true。boolean—
lg宽度达到 992 px 及以上时为 true。boolean—
xl宽度达到 1200 px 及以上时为 true。boolean—
xxl宽度达到 1600 px 及以上时为 true。boolean—
xxxl宽度达到 1920 px 及以上时为 true。boolean—
current匹配的最大断点(宽度小于 576 px 时为 xs)。Breakpoint—

BREAKPOINTS:各断点的最小宽度(像素)

属性默认值
xs0
sm576
md768
lg992
xl1200
xxl1600
xxxl1920

无障碍

  • Row 和 Col 是普通的 <div>,自身没有角色、名称或状态。它们是布局而不是数据表格,因此不带 row、cell、grid 角色。如果你的标记需要角色(列表、区域),请通过原生属性传入 role 或 aria-*。
  • 阅读顺序就是 DOM 顺序。order、push 与 pull 只改变列的绘制方式,因此 Tab 顺序和读屏顺序可能与眼睛看到的不一致(WCAG 2.4.3 与 1.3.2)。请按应有的阅读顺序书写各列,并仅把这些属性用于视觉调整。
  • span 为 0 等同于 display: none:内容不仅不可见,对辅助技术也隐藏,其中的控件也会从 Tab 顺序中移除。
  • 栅格不绘制颜色、边框或动画,其中也没有交互元素,因此对比度、forced-colors、reduced-motion、输入法、触摸与读屏检查均不适用。阅读顺序与无角色这两点由测试覆盖。