Grid

A 24-column layout. A Row lays out its Col children; a column takes span of the 24 and moves to the next line as a whole when it does not fit. Rows align and distribute their columns, and columns can be shifted, reordered, hidden and resized per screen width. The responsive values are resolved in CSS, so the server and the browser render the same page. Breakpoints tells your code which breakpoints the screen matches.

Import
import { Row, Col, Breakpoints } from 'svelte-ant-design';
Links
src/lib/Grid Report an issue Component README

When to use

  • Use the grid to lay a page or a section out on equal columns that line up from row to row.
  • Use the responsive props (xs to xxxl) when the layout has to change with the screen width.
  • For one-dimensional spacing without a column count, plain flex or CSS grid in your own styles is simpler.

Examples

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

Basic

Put Col elements in a Row. A column takes span of the 24 columns; the Row is as wide as its parent.

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

gutter is the space between columns: pixels, a CSS length, a value per breakpoint, or [horizontal, vertical]. It is a flex gap, so the Row never grows past its parent and the page does not scroll sideways.

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

Offset

offset skips that many columns before the column, so offset={4} moves it four columns to the inline end.

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

Push and pull

push and pull move a column visually. The DOM order, the Tab order and the reading order of a screen reader stay as written; with both set, the column moves by the difference.

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

justify arranges the columns of a line: start, center, end, space-between, space-around and 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

align aligns the columns of a line vertically: top (the default), middle, bottom and 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

order changes how the columns are drawn. It does not change their place in the DOM: Tab and screen readers keep the written order, so do not use it to fix a reading order.

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 takes a number (n n auto), a single length such as 100px (a fixed width, 0 0 100px), or any CSS flex value as a string.

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

Wrap

By default a column that does not fit moves to the next line as a whole. With wrap={false} the columns stay on one line, keep their widths and run past the end of the Row, so the parent can scroll.

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

Nesting

A Row in a Col fills the content box of that column and has a gutter of its own. An inner Row without a gutter does not take the gutter of the outer one.

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

Hide a column

A span of 0 is display: none: the column leaves the layout, the Tab order and the screen reader. With a breakpoint, xs={0} md={8} hides a column on small screens.

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

Responsive

xs, sm, md, lg, xl, xxl and xxxl take a span from that screen width up (576, 768, 992, 1200, 1600 and 1920 px). xs is the base; a breakpoint that is not given keeps the value of the next smaller one. Resize the window.

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

Responsive flex

A breakpoint object can carry flex too, for a share of the line that changes with the screen. Resize the window.

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

More responsive

span, offset, push, pull, order and flex go inside a breakpoint object (xs={6} is xs={{ span: 6 }}). Each key cascades on its own: the offset of xs still applies where lg gives only a span.

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

Playground

Pick the gutters and the number of columns. Every control has its own state.

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

Breakpoints in code

new Breakpoints() tells the script which breakpoints the screen matches. The values are right on the first render in a browser; on a server they follow options.fallback. Resize the window.

Row API

PropertyDescriptionTypeDefault
alignVertical alignment of the columns of a line: top, middle, bottom or stretch. An object gives one value per breakpoint.RowAlign | Partial<Record<Breakpoint, RowAlign>>'top'
justifyHorizontal arrangement of the columns of a line: start, end, center, space-around, space-between or space-evenly. An object gives one value per breakpoint.RowJustify | Partial<Record<Breakpoint, RowJustify>>'start'
gutterSpace between the columns. A number is pixels, a string is a CSS length (2rem, 5vw, calc()), an object gives one value per breakpoint, and a pair is [horizontal, vertical] (each can be any of the first three). A string that is not a length is ignored. A negative or non-finite number throws in development.Gutter | [Gutter, Gutter]0
wrapWhether columns that do not fit move to the next line. With false they stay on one line, keep their widths and run past the end of the Row.booleantrue
childrenThe columns (Col). Other children are laid out as plain flex items.Snippet—
classExtra class of the Row.string—
styleInline style of the root. It is written after the values of the component, so it wins on the same property.string—
native attributesEverything else goes to the root <div>: id, role, aria-*, data-*, event handlers.HTMLAttributes<HTMLDivElement>—
Differences from Ant Design: gutter is a flex gap, not a negative margin with padding on the columns, so a Row never grows past its parent (the Ant Design Row does, which makes a page scroll sideways) and a column's own background does not paint the gutter; the responsive values are CSS only and the breakpoints are fixed (576, 768, 992, 1200, 1600 and 1920 px), so a theme cannot change them; Breakpoints replaces the Grid.useBreakpoint hook and is right on the first render; there is no classNames/styles, because none of the three Ant Design libraries has them for the grid.

Col API

PropertyDescriptionTypeDefault
spanHow many of the 24 columns to take. 0 is display: none. Without it the column keeps its content width. A negative or non-finite number throws in development.number—
offsetEmpty columns before this one.number0
pushMoves the column that many columns towards the inline end, visually only. The DOM order does not change.number0
pullMoves the column that many columns towards the inline start, visually only. With both push and pull the column moves by the difference.number0
orderThe flex order: how the column is drawn, not where it is in the DOM. An integer, negative allowed.number0
flexA number n is flex: n n auto (0 counts); a string of digits is n 1 0px; a single length or percentage (100px, 30%) is a fixed width, 0 0 <length>; any other string is used as written. It wins over the width of span.string | number—
xsFrom 0 px up: the base of every width. A number is a span; an object takes the props of ColSize.number | ColSize—
smFrom 576 px up. A number is a span; an object takes the props of ColSize.number | ColSize—
mdFrom 768 px up. A number is a span; an object takes the props of ColSize.number | ColSize—
lgFrom 992 px up. A number is a span; an object takes the props of ColSize.number | ColSize—
xlFrom 1200 px up. A number is a span; an object takes the props of ColSize.number | ColSize—
xxlFrom 1600 px up. A number is a span; an object takes the props of ColSize.number | ColSize—
xxxlFrom 1920 px up. A number is a span; an object takes the props of ColSize.number | ColSize—
childrenThe content of the column.Snippet—
classExtra class of the Col.string—
styleInline style of the root. It is written after the values of the component, so it wins on the same property.string—
native attributesEverything else goes to the root <div>: id, role, aria-*, data-*, event handlers.HTMLAttributes<HTMLDivElement>—

ColSize: the keys of a breakpoint object

PropertyDescriptionTypeDefault
spanHow many of the 24 columns to take; 0 hides the column at this breakpoint.number—
offsetEmpty columns before this one.number—
pushVisual shift towards the inline end, in columns.number—
pullVisual shift towards the inline start, in columns.number—
orderThe flex order.number—
flexA raw flex value, as for the flex prop.string | number—

Breakpoints

PropertyDescriptionTypeDefault
new Breakpoints(options?)Creates the reader. The values are taken from the screen at once, so they are right on the first render in a browser. The listeners exist only while something reactive reads them.(options?: BreakpointsOptions) => Breakpoints—
options.fallbackThe breakpoint to report where there is no screen (on a server): the flags are those of a screen exactly as wide as this breakpoint starts. An unknown value throws in development.Breakpoint'xs'
xsTrue below 576 px.boolean—
smTrue from 576 px up.boolean—
mdTrue from 768 px up.boolean—
lgTrue from 992 px up.boolean—
xlTrue from 1200 px up.boolean—
xxlTrue from 1600 px up.boolean—
xxxlTrue from 1920 px up.boolean—
currentThe largest breakpoint that matches (xs below 576 px).Breakpoint—

BREAKPOINTS: the minimum widths, in pixels

PropertyDefault
xs0
sm576
md768
lg992
xl1200
xxl1600
xxxl1920

Accessibility

  • Row and Col are plain <div> elements with no role, name or state of their own. They are layout, not a data grid, so they do not get row, cell or grid roles. If your markup needs a role (a list, a region), pass role or aria-* as native attributes.
  • The reading order is the DOM order. order, push and pull change only how the columns are drawn, so the Tab order and a screen reader can differ from what the eye sees (WCAG 2.4.3 and 1.3.2). Write the columns in the order they should be read and use these props for decoration.
  • A span of 0 is display: none, so the content is hidden from assistive technology as well as from view, and its controls leave the Tab order.
  • The grid draws no colour, border or motion and nothing in it is interactive, so contrast, forced-colors, reduced-motion, IME, touch and screen-reader checks do not apply to it. The reading order and the missing roles are covered by tests.