Grid栅格
24 栅格布局。Row 排列其中的 Col;一列占 24 格中的 span 格,放不下时整列换到下一行。行可以对齐和分布各列,列可以偏移、重新排序、隐藏,并按屏幕宽度调整。响应式取值由 CSS 完成,因此服务器和浏览器渲染出同一个页面。Breakpoints 让你的代码知道屏幕匹配哪些断点。
何时使用
- 需要把页面或区块排布在等宽、逐行对齐的列上时,使用栅格。
- 当布局需要随屏幕宽度变化时,使用响应式属性(
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
<script lang="ts">
import { Col, Row } from 'svelte-ant-design';
</script>
<!-- Columns go into a Row. A column takes span of the 24; the Row is as wide as its parent. -->
<div class="stack">
<Row>
<Col span={24}><div class="box">col-24</div></Col>
</Row>
<Row>
<Col span={12}><div class="box">col-12</div></Col>
<Col span={12}><div class="box alt">col-12</div></Col>
</Row>
<Row>
<Col span={8}><div class="box">col-8</div></Col>
<Col span={8}><div class="box alt">col-8</div></Col>
<Col span={8}><div class="box">col-8</div></Col>
</Row>
<Row>
<Col span={6}><div class="box">col-6</div></Col>
<Col span={6}><div class="box alt">col-6</div></Col>
<Col span={6}><div class="box">col-6</div></Col>
<Col span={6}><div class="box alt">col-6</div></Col>
</Row>
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
</style>
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
<script lang="ts">
import { Col, Row } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">{'gutter={16}'}</span>
<!-- A number is pixels. The Row does not grow past its parent: there is no negative margin. -->
<Row gutter={16}>
<Col span={6}><div class="box">col-6</div></Col>
<Col span={6}><div class="box">col-6</div></Col>
<Col span={6}><div class="box">col-6</div></Col>
<Col span={6}><div class="box">col-6</div></Col>
</Row>
</div>
<div class="field">
<span class="caption">{'gutter={{ xs: 8, sm: 16, md: 24, lg: 32 }}'} (resize the window)</span>
<!-- One value per breakpoint; a breakpoint that is not given keeps the value of the next smaller one. -->
<Row gutter={{ xs: 8, sm: 16, md: 24, lg: 32 }}>
<Col span={6}><div class="box">col-6</div></Col>
<Col span={6}><div class="box">col-6</div></Col>
<Col span={6}><div class="box">col-6</div></Col>
<Col span={6}><div class="box">col-6</div></Col>
</Row>
</div>
<div class="field">
<span class="caption">{'gutter={[16, 24]}'}: horizontal and vertical</span>
<Row gutter={[16, 24]}>
{#each [0, 1, 2, 3, 4, 5, 6, 7] as index (index)}
<Col span={6}><div class="box">col-6</div></Col>
{/each}
</Row>
</div>
<div class="field">
<span class="caption">gutter="2rem": a CSS length</span>
<Row gutter="2rem">
<Col span={6}><div class="box">col-6</div></Col>
<Col span={6}><div class="box">col-6</div></Col>
<Col span={6}><div class="box">col-6</div></Col>
<Col span={6}><div class="box">col-6</div></Col>
</Row>
</div>
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.field {
display: grid;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
</style>
col-8
col-8 offset-8
col-6 offset-6
col-6 offset-6
col-12 offset-6
<script lang="ts">
import { Col, Row } from 'svelte-ant-design';
</script>
<!-- offset skips that many columns before the column. -->
<div class="stack">
<Row>
<Col span={8}><div class="box">col-8</div></Col>
<Col span={8} offset={8}><div class="box alt">col-8 offset-8</div></Col>
</Row>
<Row>
<Col span={6} offset={6}><div class="box">col-6 offset-6</div></Col>
<Col span={6} offset={6}><div class="box alt">col-6 offset-6</div></Col>
</Row>
<Row>
<Col span={12} offset={6}><div class="box">col-12 offset-6</div></Col>
</Row>
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
</style>
push={6} and pull={18}: the first column in the DOM is drawn second
col-18 push-6
col-6 pull-18
<script lang="ts">
import { Col, Row } from 'svelte-ant-design';
</script>
<!-- push and pull move a column visually. The DOM order, the Tab order and the reading order do not change. -->
<div class="stack">
<div class="field">
<span class="caption">{'push={6} and pull={18}'}: the first column in the DOM is drawn second</span>
<Row>
<Col span={18} push={6}><div class="box">col-18 push-6</div></Col>
<Col span={6} pull={18}><div class="box alt">col-6 pull-18</div></Col>
</Row>
</div>
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.field {
display: grid;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
</style>
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
<script lang="ts">
import { Col, Row } from 'svelte-ant-design';
const values = ['start', 'center', 'end', 'space-between', 'space-around', 'space-evenly'] as const;
</script>
<div class="stack">
{#each values as value (value)}
<div class="field">
<span class="caption">justify="{value}"</span>
<div class="area">
<Row justify={value}>
{#each [0, 1, 2, 3] as index (index)}
<Col span={4}><div class="box" class:alt={index % 2 === 1}>col-4</div></Col>
{/each}
</Row>
</div>
</div>
{/each}
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.field {
display: grid;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
.area {
background: var(--ads-color-bg-container-disabled);
border-radius: 4px;
}
</style>
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
<script lang="ts">
import { Col, Row } from 'svelte-ant-design';
const values = ['top', 'middle', 'bottom', 'stretch'] as const;
const heights = [100, 50, 120, 80];
</script>
<div class="stack">
{#each values as value (value)}
<div class="field">
<span class="caption">align="{value}"</span>
<div class="area">
<Row justify="space-around" align={value}>
{#each heights as height, index (index)}
<Col span={4}>
<!-- stretch makes every column as tall as the line. The box of a column fills it when it is as tall as its parent. -->
<div
class="box"
class:alt={index % 2 === 1}
class:fill={value === 'stretch' && index !== 0}
style:height={value !== 'stretch' ? `${height}px` : index === 0 ? `${height}px` : undefined}
>
col-4
</div>
</Col>
{/each}
</Row>
</div>
</div>
{/each}
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.field {
display: grid;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
.area {
background: var(--ads-color-bg-container-disabled);
border-radius: 4px;
}
.fill {
height: 100%;
box-sizing: border-box;
}
</style>
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
<script lang="ts">
import { Col, Row } from 'svelte-ant-design';
</script>
<!-- order changes how the columns are drawn, not their place in the DOM (a screen reader and Tab keep the written order). -->
<div class="stack">
<div class="field">
<span class="caption">Written 1, 2, 3, 4; drawn 1, 2, 3, 4</span>
<Row>
<Col span={6} order={1}><div class="box">1 order-1</div></Col>
<Col span={6} order={2}><div class="box alt">2 order-2</div></Col>
<Col span={6} order={3}><div class="box">3 order-3</div></Col>
<Col span={6} order={4}><div class="box alt">4 order-4</div></Col>
</Row>
</div>
<div class="field">
<span class="caption">Written 1, 2, 3, 4; drawn 4, 3, 2, 1</span>
<Row>
<Col span={6} order={4}><div class="box">1 order-4</div></Col>
<Col span={6} order={3}><div class="box alt">2 order-3</div></Col>
<Col span={6} order={2}><div class="box">3 order-2</div></Col>
<Col span={6} order={1}><div class="box alt">4 order-1</div></Col>
</Row>
</div>
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.field {
display: grid;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
</style>
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
<script lang="ts">
import { Col, Row } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">{'flex={2} and flex={3}'}: a number n is "n n auto"</span>
<Row>
<Col flex={2}><div class="box">2 / 5</div></Col>
<Col flex={3}><div class="box alt">3 / 5</div></Col>
</Row>
</div>
<div class="field">
<span class="caption">flex="100px" and flex="auto": a fixed width and the rest</span>
<Row>
<Col flex="100px"><div class="box">100px</div></Col>
<Col flex="auto"><div class="box alt">Fill rest</div></Col>
</Row>
</div>
<div class="field">
<span class="caption">flex="1 1 200px" and flex="0 1 300px": a raw flex value</span>
<Row>
<Col flex="1 1 200px"><div class="box">1 1 200px</div></Col>
<Col flex="0 1 300px"><div class="box alt">0 1 300px</div></Col>
</Row>
</div>
<div class="field">
<span class="caption">flex="none" and flex="auto"</span>
<Row wrap={false}>
<Col flex="none"><div class="box">none</div></Col>
<Col flex="auto"><div class="box alt">auto with no-wrap</div></Col>
</Row>
</div>
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.field {
display: grid;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
</style>
wrap
wrap={true}
Three columns of 12: the third does not fit in a line
col-12
col-12
col-12
<script lang="ts">
import { Col, Row, Switch } from 'svelte-ant-design';
let wrap = $state(true);
</script>
<div class="stack">
<div class="field">
<span class="caption">wrap</span>
<div class="row">
<Switch bind:checked={wrap} aria-label="wrap" />
<span role="status">{`wrap={${wrap}}`}</span>
</div>
</div>
<div class="field">
<span class="caption">Three columns of 12: the third does not fit in a line</span>
<!-- With wrap, a column that does not fit moves to the next line as a whole. Without it the columns keep their width and run past the end of the Row. -->
<div class="strip">
<Row {wrap}>
<Col span={12}><div class="box">col-12</div></Col>
<Col span={12}><div class="box alt">col-12</div></Col>
<Col span={12}><div class="box">col-12</div></Col>
</Row>
</div>
</div>
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.field {
display: grid;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.strip {
overflow-x: auto;
}
</style>
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
<script lang="ts">
import { Col, Row } from 'svelte-ant-design';
</script>
<!-- A Row inside a Col fills the content box of the column and has a gutter of its own. -->
<div class="stack">
<div class="field">
<span class="caption">Outer gutter 16; the inner Row on the left has gutter 4, the one on the right none</span>
<Row gutter={16}>
<Col span={12}>
<Row gutter={4}>
<Col span={12}><div class="box">12 / 24 of 12</div></Col>
<Col span={12}><div class="box alt">12 / 24 of 12</div></Col>
</Row>
</Col>
<Col span={12}>
<Row>
<Col span={8}><div class="box">8</div></Col>
<Col span={16}><div class="box alt">16</div></Col>
</Row>
</Col>
</Row>
</div>
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.field {
display: grid;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
</style>
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
<script lang="ts">
import { Col, Row } from 'svelte-ant-design';
</script>
<!-- xs is the base for every width; each larger breakpoint applies from its minimum width up. Resize the window. -->
<div class="stack">
<div class="field">
<span class="caption">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</span>
<Row>
<Col xs={2} sm={4} md={6} lg={8} xl={10} xxl={11} xxxl={12}><div class="box">Col</div></Col>
<Col xs={20} sm={16} md={12} lg={8} xl={4} xxl={2} xxxl={0}><div class="box alt">Col</div></Col>
<Col xs={2} sm={4} md={6} lg={8} xl={10} xxl={11} xxxl={12}><div class="box">Col</div></Col>
</Row>
</div>
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.field {
display: grid;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
</style>
flex 100% / 50% / 40% / 20% / 10% from xs / sm / md / lg / xl
Col
Col
Col
Col
Col
<script lang="ts">
import { Col, Row } from 'svelte-ant-design';
</script>
<!-- A breakpoint object can carry flex: a share of the line that changes with the screen. Resize the window. -->
<div class="stack">
<div class="field">
<span class="caption">flex 100% / 50% / 40% / 20% / 10% from xs / sm / md / lg / xl</span>
<Row>
{#each [0, 1, 2, 3, 4] as index (index)}
<Col xs={{ flex: '100%' }} sm={{ flex: '50%' }} md={{ flex: '40%' }} lg={{ flex: '20%' }} xl={{ flex: '10%' }}>
<div class="box" class:alt={index % 2 === 1}>Col</div>
</Col>
{/each}
</Row>
</div>
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.field {
display: grid;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
</style>
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
<script lang="ts">
import { Col, Row } from 'svelte-ant-design';
</script>
<!-- span, offset, push, pull, order and flex go inside a breakpoint object; xs={6} is the same as xs={{ span: 6 }}.
Each key cascades on its own: the offset of xs still applies where lg gives only a span. -->
<div class="stack">
<div class="field">
<span class="caption">xs span 5 offset 1; lg span 6 offset 2 (resize the window)</span>
<Row>
<Col xs={{ span: 5, offset: 1 }} lg={{ span: 6, offset: 2 }}><div class="box">Col</div></Col>
<Col xs={{ span: 11, offset: 1 }} lg={{ span: 6, offset: 2 }}><div class="box alt">Col</div></Col>
<Col xs={{ span: 5, offset: 1 }} lg={{ span: 6, offset: 2 }}><div class="box">Col</div></Col>
</Row>
</div>
<div class="field">
<span class="caption">{'xs={12}'}, md order 2 / 1: drawn in the other order from md</span>
<Row>
<Col xs={12} md={{ span: 12, order: 2 }}><div class="box">First in the DOM</div></Col>
<Col xs={12} md={{ span: 12, order: 1 }}><div class="box alt">Second in the DOM</div></Col>
</Row>
</div>
<div class="field">
<span class="caption">md push 6 and pull 18: swapped visually from md, the DOM order stays</span>
<Row>
<Col xs={12} md={{ span: 18, push: 6 }}><div class="box">First in the DOM</div></Col>
<Col xs={12} md={{ span: 6, pull: 18 }}><div class="box alt">Second in the DOM</div></Col>
</Row>
</div>
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.field {
display: grid;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
</style>
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
<script lang="ts">
import { Col, RadioGroup, Row } from 'svelte-ant-design';
const gutters = [8, 16, 24, 32, 40, 48];
let horizontal = $state(16);
let vertical = $state(16);
let count = $state(4);
const counts = [2, 3, 4, 6, 8, 12];
</script>
<div class="stack">
<div class="field">
<span class="caption">Horizontal gutter (px)</span>
<RadioGroup bind:value={horizontal} optionType="button" options={gutters} aria-label="Horizontal gutter" />
</div>
<div class="field">
<span class="caption">Vertical gutter (px)</span>
<RadioGroup bind:value={vertical} optionType="button" options={gutters} aria-label="Vertical gutter" />
</div>
<div class="field">
<span class="caption">Columns in a row</span>
<RadioGroup bind:value={count} optionType="button" options={counts} aria-label="Columns in a row" />
</div>
<div class="field">
<span class="caption">{`<Row gutter={[${horizontal}, ${vertical}]}> with ${count} columns of span ${24 / count}, twice`}</span>
<Row gutter={[horizontal, vertical]}>
{#each Array.from({ length: count * 2 }, (_, index) => index) as index (index)}
<Col span={24 / count}><div class="box" class:alt={index >= count}>Column</div></Col>
{/each}
</Row>
</div>
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.field {
display: grid;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
</style>
screens.current
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
<script lang="ts">
import { Breakpoints, Col, Row } from 'svelte-ant-design';
// The values are read from the screen when the object is created, so they are right on the first render.
// On a server, where there is no screen, they follow options.fallback (default 'xs').
const screens = new Breakpoints();
const names = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl', 'xxxl'] as const;
</script>
<div class="stack">
<div class="field">
<span class="caption">screens.current</span>
<output>{screens.current}</output>
</div>
<div class="field">
<span class="caption">screens.xs ... screens.xxxl (xs is true below 576 px, the others from their width up)</span>
<Row gutter={[8, 8]}>
{#each names as name (name)}
<Col xs={12} sm={8} md={6}>
<div class="box" class:alt={!screens[name]}>{name}: {screens[name]}</div>
</Col>
{/each}
</Row>
</div>
<div class="field">
<span class="caption">{'{#if screens.md}'}: markup that depends on the screen</span>
{#if screens.md}
<div class="box">The screen is at least 768 px wide</div>
{:else}
<div class="box alt">The screen is narrower than 768 px</div>
{/if}
</div>
</div>
<style>
.stack {
display: grid;
gap: 20px;
width: 100%;
}
.field {
display: grid;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
padding: 8px 4px;
text-align: center;
font-size: 13px;
color: var(--ads-color-text);
background: var(--ads-color-bg-text-hover);
border: 1px solid var(--ads-color-primary-solid);
border-radius: 4px;
}
.alt {
background: var(--ads-color-bg-container);
}
</style>
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 的末端。 | boolean | true |
| children | 各列(Col)。其他子元素按普通 flex 子项排列。 | Snippet | — |
| class | Row 根元素的额外类名。 | 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 | 该列左侧空出的格数。 | number | 0 |
| push | 让该列向行尾方向移动相应格数,仅影响视觉效果,不改变 DOM 顺序。 | number | 0 |
| pull | 让该列向行首方向移动相应格数,仅影响视觉效果。同时设置 push 与 pull 时,按差值移动。 | number | 0 |
| order | flex 的 order:决定列的绘制顺序,而不是它在 DOM 中的位置。整数,可为负。 | number | 0 |
| 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 | — |
| class | Col 根元素的额外类名。 | string | — |
| style | 根元素的内联样式。它写在组件自身的值之后,因此在同一属性上优先生效。 | string | — |
| native attributes | 其余属性都会传给根元素 <div>:id、role、aria-*、data-* 与事件处理函数。 | HTMLAttributes<HTMLDivElement> | — |
ColSize:断点对象的键
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| span | 占 24 格中的多少格;0 会在该断点隐藏此列。 | number | — |
| offset | 该列左侧空出的格数。 | number | — |
| push | 向行尾方向的视觉偏移,单位为格。 | number | — |
| pull | 向行首方向的视觉偏移,单位为格。 | number | — |
| order | flex 的 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:各断点的最小宽度(像素)
| 属性 | 默认值 |
|---|---|
| xs | 0 |
| sm | 576 |
| md | 768 |
| lg | 992 |
| xl | 1200 |
| xxl | 1600 |
| xxxl | 1920 |
无障碍
- 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、输入法、触摸与读屏检查均不适用。阅读顺序与无角色这两点由测试覆盖。