Space
Apply consistent spacing to a group of elements, horizontally or vertically, with optional separators between adjacent elements.
Import
import { HuiSpace } from '@nscode/handy-ui'
Basic Usage
Place elements directly inside HuiSpace. Each child occupies one cell, arranged horizontally with a default 16px gap.
<script setup lang="ts">
import { HuiSpace, HuiButton } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace>
<HuiButton>Create</HuiButton>
<HuiButton>Edit</HuiButton>
<HuiButton>Delete</HuiButton>
</HuiSpace>
</template>
Direction
direction accepts horizontal (the default) or vertical. In a vertical layout, the container follows its content width, and align positions children within the width of the widest child.
<template>
<HuiSpace size="large" align="start">
<div>
<p class="label">horizontal (default)</p>
<HuiSpace>
<HuiButton>First</HuiButton>
<HuiButton>Second button</HuiButton>
<HuiButton>Third</HuiButton>
</HuiSpace>
</div>
<div>
<p class="label">vertical</p>
<HuiSpace direction="vertical" align="start">
<HuiButton>First</HuiButton>
<HuiButton>Second button</HuiButton>
<HuiButton>Third</HuiButton>
</HuiSpace>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin: 0 0 8px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Gap Size
size accepts the presets small (8px), default (16px), and large (24px), or a number in pixels.
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace>
<span class="row-label">small</span>
<HuiSpace size="small">
<HuiButton>8px</HuiButton>
<HuiButton>8px</HuiButton>
<HuiButton>8px</HuiButton>
</HuiSpace>
</HuiSpace>
<HuiSpace>
<span class="row-label">default</span>
<HuiSpace>
<HuiButton>16px</HuiButton>
<HuiButton>16px</HuiButton>
<HuiButton>16px</HuiButton>
</HuiSpace>
</HuiSpace>
<HuiSpace>
<span class="row-label">large</span>
<HuiSpace size="large">
<HuiButton>24px</HuiButton>
<HuiButton>24px</HuiButton>
<HuiButton>24px</HuiButton>
</HuiSpace>
</HuiSpace>
<HuiSpace>
<span class="row-label">40</span>
<HuiSpace :size="40">
<HuiButton>40px</HuiButton>
<HuiButton>40px</HuiButton>
<HuiButton>40px</HuiButton>
</HuiSpace>
</HuiSpace>
<HuiSpace>
<span class="row-label">0</span>
<HuiSpace :size="0">
<HuiButton>0px</HuiButton>
<HuiButton>0px</HuiButton>
<HuiButton>0px</HuiButton>
</HuiSpace>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Drag the slider to adjust spacing:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSlider, HuiSpace } from '@nscode/handy-ui'
const gap = ref(16)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSlider v-model="gap" :min="0" :max="48" style="max-width: 320px" />
<div>
<HuiSpace :size="gap">
<HuiButton>Button</HuiButton>
<HuiButton>Button</HuiButton>
<HuiButton>Button</HuiButton>
</HuiSpace>
</div>
<span>Gap {{ gap }}px</span>
</HuiSpace>
</template>
Separate Horizontal and Vertical Gaps
An array size uses [horizontal gap, vertical gap], in pixels. The horizontal gap separates adjacent elements in a row; the vertical gap separates rows and is visible when wrapping or arranging vertically.
<template>
<HuiSpace wrap :size="[8, 24]" style="width: 320px">
<HuiButton v-for="i in 10" :key="i">Button {{ i }}</HuiButton>
</HuiSpace>
</template>
Wrapping
With wrap=true, elements that do not fit move to the next line. Without wrap, they remain on one line even when they overflow. Both containers below are 300px wide:
<template>
<HuiSpace direction="vertical" align="start">
<p class="label">wrap: overflow moves to the next line</p>
<HuiSpace wrap class="box">
<HuiButton v-for="i in 8" :key="i">Tag {{ i }}</HuiButton>
</HuiSpace>
<p class="label">
Without wrap: one row overflows the container (horizontal scrolling is enabled here)
</p>
<div class="box box--scroll">
<HuiSpace>
<HuiButton v-for="i in 8" :key="i">Tag {{ i }}</HuiButton>
</HuiSpace>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin: 0;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.box {
width: 300px;
padding: var(--hui-spacing-2);
border: var(--hui-border-width) dashed var(--hui-border-color);
}
.box--scroll {
overflow-x: auto;
}
</style>
Alignment
align controls cross-axis alignment: vertical alignment in a horizontal layout, or horizontal alignment in a vertical layout. It accepts start, center (the default), end, or baseline.
Each horizontal row below uses a different value. The gray block is taller than the button:
<script setup lang="ts">
import { HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { SpaceAlign } from '@nscode/handy-ui'
const aligns: SpaceAlign[] = ['start', 'center', 'end', 'baseline']
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace v-for="item in aligns" :key="item" :align="item" class="row">
<span class="row-label">{{ item }}</span>
<HuiButton>Button</HuiButton>
<div class="block">Taller block</div>
<span style="font-size: 24px">Large text</span>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row {
padding: var(--hui-spacing-2);
background: var(--hui-fill-color-light);
}
.row-label {
display: inline-block;
width: 64px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.block {
padding: 24px 12px;
background: var(--hui-fill-color-dark);
}
</style>
In a vertical layout, align places children at the left, center, or right within the widest child's width:
<template>
<HuiSpace size="large" align="start">
<div>
<p class="label">start</p>
<HuiSpace direction="vertical" align="start" class="column">
<HuiButton>Short</HuiButton>
<HuiButton>A longer button</HuiButton>
<HuiButton>Medium button</HuiButton>
</HuiSpace>
</div>
<div>
<p class="label">center (default)</p>
<HuiSpace direction="vertical" class="column">
<HuiButton>Short</HuiButton>
<HuiButton>A longer button</HuiButton>
<HuiButton>Medium button</HuiButton>
</HuiSpace>
</div>
<div>
<p class="label">end</p>
<HuiSpace direction="vertical" align="end" class="column">
<HuiButton>Short</HuiButton>
<HuiButton>A longer button</HuiButton>
<HuiButton>Medium button</HuiButton>
</HuiSpace>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin: 0 0 8px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.column {
padding: var(--hui-spacing-2);
background: var(--hui-fill-color-light);
}
</style>
Main-axis distribution
justify supports the same six values as Row. When provided, the container uses display: flex; width: 100%, while children keep their content width. Omit it to retain inline-flex. Vertical distribution follows the height axis and requires a defined container height.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { SpaceJustify } from '@nscode/handy-ui'
const values: SpaceJustify[] = [
'start',
'center',
'end',
'space-between',
'space-around',
'space-evenly'
]
const current = ref<SpaceJustify>('end')
</script>
<template>
<HuiSpace class="space-justify-demo" direction="vertical" fill>
<div v-for="value in values" :key="value" style="width: 100%">
<span>{{ value }}</span>
<HuiSpace
:justify="value"
:size="8"
style="border: 1px dashed var(--hui-border-color); padding: 8px; box-sizing: border-box"
>
<HuiButton>A</HuiButton><HuiButton>B</HuiButton><HuiButton>C</HuiButton>
</HuiSpace>
</div>
<HuiButton class="justify-switch" @click="current = current === 'end' ? 'start' : 'end'"
>Toggle left and right alignment</HuiButton
>
<span class="justify-status">Current:{{ current }}</span>
<HuiSpace class="justify-interactive" :justify="current" :size="8">
<HuiButton>Cancel</HuiButton><HuiButton type="primary">Confirm</HuiButton>
</HuiSpace>
<span>Separators are separate items and spread out too:</span>
<HuiSpace justify="space-between" separator="|" :size="8">
<span>View</span><span>Edit</span><span>Export</span>
</HuiSpace>
<span>Wrapped lines are distributed independently, including the last line:</span>
<div style="width: 240px; max-width: 100%">
<HuiSpace justify="space-between" wrap :size="[8, 12]">
<HuiButton v-for="n in 5" :key="n">Item {{ n }}</HuiButton>
</HuiSpace>
</div>
<span>Vertical layout uses the height axis; set height to see vertical distribution:</span>
<HuiSpace
direction="vertical"
justify="space-between"
align="start"
style="height: 160px; border: 1px dashed var(--hui-border-color)"
>
<HuiButton>Top</HuiButton><HuiButton>Middle</HuiButton><HuiButton>Bottom</HuiButton>
</HuiSpace>
</HuiSpace>
</template>
Separators are independent flex items and participate in distribution. With wrap, each line is distributed separately, including the last line. fill makes children occupy the remaining space; combining it with justify produces a development warning while preserving the existing fill behavior. Use fill for fixed gaps with stretched children, and justify to distribute remaining space.
Separators
Pass a string to separator to display it between adjacent elements, with none after the last element. The separator has spacing on each side and also works vertically.
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace separator="|">
<HuiLink>View</HuiLink>
<HuiLink>Edit</HuiLink>
<HuiLink>Delete</HuiLink>
</HuiSpace>
<HuiSpace separator="/" size="small">
<span>Home</span>
<span>System management</span>
<span>User management</span>
</HuiSpace>
<HuiSpace separator="·" size="small">
<span>3 minutes ago</span>
<span>1024 views</span>
<span>12 comments</span>
</HuiSpace>
<HuiSpace direction="vertical" separator="—" size="small">
<span>First item</span>
<span>Second item</span>
<span>Third item</span>
</HuiSpace>
</HuiSpace>
</template>
Filling the Container
With fill=true, the container occupies the parent's full width. Cells share the width equally, and their children stretch to fill them.
<template>
<HuiSpace fill>
<HuiButton>Previous</HuiButton>
<HuiButton type="primary">Next</HuiButton>
</HuiSpace>
</template>
In a vertical layout, fill makes each cell occupy the full row width, suitable for stacking block elements such as alerts and cards with consistent spacing.
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert type="success" title="Saved successfully" />
<HuiAlert type="warning" title="3 fields remain incomplete" />
</HuiSpace>
</template>
Children Generated with v-for
Each element generated by v-for or <template> occupies its own cell, with normal spacing and separators.
<script setup lang="ts">
import { HuiSpace, HuiTag } from '@nscode/handy-ui'
const tags = ['前端', '后端', '测试', '运维']
</script>
<template>
<HuiSpace separator="/">
<HuiTag v-for="tag in tags" :key="tag">{{
{ 前端: 'Frontend', 后端: 'Backend', 测试: 'Testing', 运维: 'Operations' }[tag]
}}</HuiTag>
</HuiSpace>
</template>
When a <template v-if> condition is false, it occupies no cell and leaves no extra spacing or separator:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiLink, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
const canEdit = ref(true)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSwitch v-model="canEdit" active-text="Editing permitted" />
<HuiSpace separator="|">
<HuiLink>View</HuiLink>
<template v-if="canEdit">
<HuiLink>Edit</HuiLink>
<HuiLink type="danger">Delete</HuiLink>
</template>
<HuiLink>Export</HuiLink>
</HuiSpace>
</HuiSpace>
</template>
Following the Global Size
When size is omitted, spacing follows the surrounding HuiConfigProvider.size: small 8px, default 16px, or large 24px. An explicit size takes precedence. Buttons also read ConfigProvider's size, so the buttons below change size with it.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiConfigProvider, HuiSegmented, HuiSpace } from '@nscode/handy-ui'
const size = ref<'large' | 'default' | 'small'>('default')
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSegmented v-model="size" :options="['large', 'default', 'small']" />
<HuiConfigProvider :size="size">
<HuiSpace direction="vertical" align="start">
<HuiSpace>
<HuiButton>Inherited</HuiButton>
<HuiButton>Inherited</HuiButton>
<HuiButton>Inherited</HuiButton>
</HuiSpace>
<HuiSpace size="small">
<HuiButton>Fixed small</HuiButton>
<HuiButton>Fixed small</HuiButton>
</HuiSpace>
</HuiSpace>
</HuiConfigProvider>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| direction | 'horizontal' | 'vertical' | 'horizontal' | Layout direction |
| size | 'small' | 'default' | 'large' | number | [number, number] | 'default' | Gap. Presets are 8, 16, and 24px; numbers use pixels; arrays use [horizontal, vertical]. Follows ConfigProvider.size when omitted, otherwise falls back to 'default' |
| wrap | boolean | false | Whether overflowing elements wrap |
| align | 'start' | 'center' | 'end' | 'baseline' | 'center' | Cross-axis alignment of children |
| justify | SpaceJustify | — | Main-axis distribution: start/end/center/space-between/space-around/space-evenly. When provided, the container fills its parent width as a block. |
| fill | boolean | false | Whether to fill the parent's width. Cells share the width equally (each fills the row vertically), and children stretch to fill their cells |
| separator | string | - | String separator between adjacent elements; omitted when not provided |
Slots
The default slot is read once in each render, so updated scoped-slot values and replaced slot branches appear immediately. Fragments, separators, keyed child instances, and user input retain their existing behavior.
| Slot | Scope arguments | Description |
|---|---|---|
| default | - | Elements to arrange; each child occupies a cell |
Types
import type {
SpaceProps,
SpaceDirection,
SpaceAlign,
SpaceSize,
SpaceJustify
} from '@nscode/handy-ui'
import type { RowJustify } from '@nscode/handy-ui'
type SpaceDirection = 'horizontal' | 'vertical'
type SpaceAlign = 'start' | 'center' | 'end' | 'baseline'
type SpaceSize = 'small' | 'default' | 'large' | number | [number, number]
type SpaceJustify = RowJustify
interface SpaceProps {
direction?: SpaceDirection
size?: SpaceSize
wrap?: boolean
align?: SpaceAlign
justify?: SpaceJustify
fill?: boolean
separator?: string
}