Space
Space puts an equal gap between elements. SpaceCompact joins buttons and fields into one block, and SpaceAddon adds a cell of text to it.
When to use
- Use a
Spaceto keep buttons, links or tags apart without margins of your own. For a page layout in columns useGrid. - Use a
SpaceCompactwhen several controls make one input: an address with its button, a unit next to a number. - Do not join controls that do unrelated things: a joined block reads as one.
Examples
<script lang="ts">
import { Button, Space } from 'svelte-ant-design';
</script>
<!-- The elements stand side by side with a gap of 8px. -->
<Space>
<Button type="primary">Save</Button>
<Button>Preview</Button>
<Button type="link">Cancel</Button>
</Space>
vertical
vertical with SpaceItem
<script lang="ts">
import { Button, Input, Space, SpaceItem } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">vertical</span>
<!-- An element keeps its own width. -->
<Space vertical>
<Button>First</Button>
<Button>The second one</Button>
</Space>
</div>
<div class="field">
<span class="caption">vertical with SpaceItem</span>
<!-- A SpaceItem fills the width of the column. -->
<Space vertical size="medium" style="width: 100%">
<SpaceItem><Input aria-label="Name" placeholder="Name" /></SpaceItem>
<SpaceItem><Button block type="primary">Send</Button></SpaceItem>
</Space>
</div>
</div>
<style>
.stack {
display: grid;
gap: 16px;
width: 100%;
max-width: 320px;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
size="small"
size="medium"
size="large"
size={40}
<script lang="ts">
import { Button, Space } from 'svelte-ant-design';
const sizes = ['small', 'medium', 'large'] as const;
</script>
<div class="stack">
{#each sizes as size (size)}
<div class="field">
<span class="caption">size="{size}"</span>
<Space {size}>
<Button>One</Button>
<Button>Two</Button>
<Button>Three</Button>
</Space>
</div>
{/each}
<div class="field">
<span class="caption">size={'{40}'}</span>
<Space size={40}>
<Button>One</Button>
<Button>Two</Button>
</Space>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
align="start"
Text Block
align="center"
Text Block
align="end"
Text Block
align="baseline"
Text Block
<script lang="ts">
import { Button, Space } from 'svelte-ant-design';
const aligns = ['start', 'center', 'end', 'baseline'] as const;
</script>
<div class="stack">
{#each aligns as align (align)}
<div class="field">
<span class="caption">align="{align}"</span>
<Space {align} class="sample">
<span>Text</span>
<Button type="primary">Button</Button>
<span class="block">Block</span>
</Space>
</div>
{/each}
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.block {
display: inline-block;
padding: 24px 8px;
background: rgba(150, 150, 150, 0.15);
}
</style>
<script lang="ts">
import { Checkbox, Space, SpaceItem } from 'svelte-ant-design';
let archive = $state(false);
</script>
{#snippet dot()}
<span aria-hidden="true">•</span>
{/snippet}
<div class="stack">
<div class="field">
<span class="caption">separator="|"</span>
<!-- Each SpaceItem draws the separator before itself; the first one shows none. -->
<Space separator="|">
<SpaceItem><a href="#api">Profile</a></SpaceItem>
<SpaceItem><a href="#api">Settings</a></SpaceItem>
{#if archive}<SpaceItem><a href="#api">Archive</a></SpaceItem>{/if}
<SpaceItem><a href="#api">Sign out</a></SpaceItem>
</Space>
<Checkbox bind:checked={archive}>Show the archive link</Checkbox>
</div>
<div class="field">
<span class="caption">separator as a snippet</span>
<Space separator={dot} size="medium">
<SpaceItem>Draft</SpaceItem>
<SpaceItem>3 pages</SpaceItem>
<SpaceItem>Updated today</SpaceItem>
</Space>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
wrap, size={[8, 16]}
<script lang="ts">
import { Button, Space } from 'svelte-ant-design';
const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
</script>
<div class="stack">
<div class="field">
<span class="caption">wrap, size={'{[8, 16]}'}</span>
<!-- The row goes on in the next line: 8px between columns, 16px between rows. -->
<Space wrap size={[8, 16]}>
{#each days as day (day)}<Button>{day}</Button>{/each}
</Space>
</div>
</div>
<style>
.stack {
display: grid;
gap: 16px;
width: 100%;
max-width: 360px;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
block: Input and Button
Select and InputSearch
Docs
InputPassword and two buttons
<script lang="ts">
import { Button, Input, InputPassword, InputSearch, Select, SpaceCompact } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">block: Input and Button</span>
<SpaceCompact block>
<Input aria-label="Link" placeholder="Paste a link" />
<Button type="primary">Shorten</Button>
</SpaceCompact>
</div>
<div class="field">
<span class="caption">Select and InputSearch</span>
<SpaceCompact block>
<Select aria-label="Section" value="Docs" options={['Docs', 'Blog']} style="width: 100px" />
<InputSearch aria-label="Search" placeholder="Search" enterButton />
</SpaceCompact>
</div>
<div class="field">
<span class="caption">InputPassword and two buttons</span>
<SpaceCompact block>
<InputPassword aria-label="Token" value="secret" />
<Button>Copy</Button>
<Button danger>Revoke</Button>
</SpaceCompact>
</div>
</div>
<style>
.stack {
display: grid;
gap: 16px;
width: 100%;
max-width: 420px;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 6px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
size="large"
All
size="medium"
All
size="small"
All
<script lang="ts">
import { Button, Input, Select, SpaceCompact } from 'svelte-ant-design';
const sizes = ['large', 'medium', 'small'] as const;
</script>
<div class="stack">
{#each sizes as size (size)}
<div class="field">
<span class="caption">size="{size}"</span>
<!-- The controls take the size of the block; one with a size of its own keeps it. -->
<SpaceCompact {size}>
<Select aria-label="Kind, {size}" value="All" options={['All', 'New']} style="width: 90px" />
<Input aria-label="Query, {size}" placeholder="Query" style="width: 150px" />
<Button>Go</Button>
</SpaceCompact>
</div>
{/each}
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { Button, SpaceCompact } from 'svelte-ant-design';
</script>
<div class="row">
<SpaceCompact vertical>
<Button>Top</Button>
<Button>Middle</Button>
<Button>Bottom</Button>
</SpaceCompact>
<!-- Solid buttons side by side keep a line between them. `orientation` is the long form of `vertical`. -->
<SpaceCompact orientation="vertical">
<Button type="primary">Accept</Button>
<Button type="primary">Accept all</Button>
</SpaceCompact>
</div>
<style>
.row {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
gap: 24px;
}
</style>
SpaceAddon before and after a field
https://
.example.com
with a button
$
<script lang="ts">
import { Button, Input, SpaceAddon, SpaceCompact } from 'svelte-ant-design';
let site = $state('');
</script>
<div class="stack">
<div class="field">
<span class="caption">SpaceAddon before and after a field</span>
<SpaceCompact block>
<SpaceAddon>https://</SpaceAddon>
<Input bind:value={site} aria-label="Site name" placeholder="my-site" />
<SpaceAddon>.example.com</SpaceAddon>
</SpaceCompact>
<span role="status">{site ? `https://${site}.example.com` : ''}</span>
</div>
<div class="field">
<span class="caption">with a button</span>
<SpaceCompact>
<SpaceAddon>$</SpaceAddon>
<Input aria-label="Amount" inputmode="decimal" placeholder="0.00" style="width: 140px" />
<Button type="primary">Pay</Button>
</SpaceCompact>
</div>
</div>
<style>
.stack {
display: grid;
gap: 16px;
width: 100%;
max-width: 420px;
}
.field {
display: grid;
grid-template-columns: minmax(0, 1fr);
justify-items: start;
gap: 6px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
a block inside a block
with Tooltip and Dropdown
<script lang="ts">
import { Button, Dropdown, SpaceCompact, Tooltip } from 'svelte-ant-design';
let chosen = $state('');
</script>
<div class="stack">
<div class="field">
<span class="caption">a block inside a block</span>
<SpaceCompact>
<SpaceCompact>
<Button>Cut</Button>
<Button>Copy</Button>
</SpaceCompact>
<SpaceCompact>
<Button>Undo</Button>
<Button>Redo</Button>
</SpaceCompact>
</SpaceCompact>
</div>
<div class="field">
<span class="caption">with Tooltip and Dropdown</span>
<!-- A control inside a Tooltip or a Dropdown stays joined; what it opens does not. -->
<SpaceCompact>
<Tooltip title="Save the draft"><Button>Save</Button></Tooltip>
<Dropdown trigger={['click']} menu={{ items: [{ key: 'copy', label: 'Save a copy' }, { key: 'template', label: 'Save as a template' }], onclick: (info) => (chosen = info.key) }}>
<Button>More</Button>
</Dropdown>
</SpaceCompact>
<span role="status">{chosen}</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
/
Home
/ Library
/ Space
<script lang="ts">
import { Space, SpaceItem } from 'svelte-ant-design';
</script>
<Space
separator="/"
classNames={{ root: 'trail', item: 'step' }}
styles={{ item: { padding: '4px 8px', 'border-radius': '6px' }, separator: { opacity: 0.5 } }}
>
<SpaceItem>Home</SpaceItem>
<SpaceItem>Library</SpaceItem>
<SpaceItem>Space</SpaceItem>
</Space>
<style>
:global(.trail .step) {
background: rgba(22, 119, 255, 0.1);
}
</style>
API
| Property | Description | Type | Default |
|---|---|---|---|
| orientation | The direction of the row; horizontal unless it or vertical says otherwise. It wins over vertical. | 'horizontal' | 'vertical' | — |
| vertical | A shorthand for orientation="vertical". | boolean | false |
| size | The gap: a name or a number of pixels; a pair is the gap between columns and between rows. | SpaceSize | [SpaceSize, SpaceSize] | 'small' |
| align | Alignment across the row. Without it a row is centered; in a column an element keeps its width and a SpaceItem fills it. | 'start' | 'end' | 'center' | 'baseline' | 'stretch' | — |
| wrap | A horizontal row goes on in the next line. | boolean | false |
| separator | Drawn between the items, by each SpaceItem before itself. | string | Snippet | — |
| classNames | Extra classes for the parts root, item and separator. | SpaceClassNames | — |
| styles | Inline styles for the same parts. | SpaceStyles | — |
| children | The elements. | Snippet | — |
| class / style | Class and style of the root. | string | — |
| native attributes | Other attributes of the root <div>. | HTMLAttributes<HTMLDivElement> | — |
Differences from Ant Design:
Space does not wrap its children, so an element goes into SpaceItem where it needs a separator or item styles, and without it a block element of a column is as wide as its content; Space.Compact and Space.Addon are SpaceCompact and SpaceAddon; direction and split are not taken (use orientation and separator); classNames and styles are objects, not functions.SpaceItem API
| Property | Description | Type | Default |
|---|---|---|---|
| children | One item. | Snippet | — |
| class / style | Class and style of the element of the item. | string | — |
| native attributes | Other attributes of the <div>. | HTMLAttributes<HTMLDivElement> | — |
SpaceCompact API
| Property | Description | Type | Default |
|---|---|---|---|
| block | As wide as the parent. | boolean | false |
| orientation | The direction of the block; horizontal unless it or vertical says otherwise. It wins over vertical. | 'horizontal' | 'vertical' | — |
| vertical | A shorthand for orientation="vertical". | boolean | false |
| size | The size of the controls that name none of their own. middle and default are aliases of medium. | 'large' | 'medium' | 'middle' | 'small' | 'default' | — |
| children | The controls: Button, Input, InputPassword, InputSearch, InputTextArea, Select, SpaceAddon and nested blocks. | Snippet | — |
| class / style | Class and style of the root. | string | — |
| native attributes | Other attributes of the root <div>. | HTMLAttributes<HTMLDivElement> | — |
SpaceAddon API
| Property | Description | Type | Default |
|---|---|---|---|
| children | The content of the cell. | Snippet | — |
| class / style | Class and style of the cell. | string | — |
| native attributes | Other attributes of the <div>. | HTMLAttributes<HTMLDivElement> | — |
Design tokens
| Token | Description | Default |
|---|---|---|
| --ads-space-gap-small | The gap of size="small". | 8px |
| --ads-space-gap-medium | The gap of size="medium". | 16px |
| --ads-space-gap-large | The gap of size="large". | 24px |
| --ads-space-compact-divider | The line between two solid buttons of a block. | rgba(255, 255, 255, 0.4) |
Accessibility
Space,SpaceCompactandSpaceAddonare layout: they add no roles and move nothing, so a screen reader and the keyboard meet the elements in the order of the page.- The text of a
SpaceAddonis not a label. Name the control beside it witharia-labelor a<label>. A separator is read like any text: give a decorative onearia-hidden. - Checked with a real screen reader (Orca with Firefox): it reads the controls of a compact block with their names in the order of the page, the text of an addon before its field and the items of a separated row. Not checked with NVDA, JAWS, VoiceOver or on a phone.