Grid
Divide each row into 24 columns. Wrap a row with HuiRow and allocate its columns with HuiCol to create proportional layouts that respond to screen width.
Import
import { HuiRow, HuiCol } from '@nscode/handy-ui'
Basic Usage
HuiCol.span specifies how many of the row's 24 columns it occupies. When the combined spans exceed 24, the excess columns wrap to the next line.
<script setup lang="ts">
import { HuiRow, HuiCol, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiRow>
<HuiCol :span="24"><div class="cell">24</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="12"><div class="cell">12</div></HuiCol>
<HuiCol :span="12"><div class="cell cell--light">12</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="8"><div class="cell">8</div></HuiCol>
<HuiCol :span="8"><div class="cell cell--light">8</div></HuiCol>
<HuiCol :span="8"><div class="cell">8</div></HuiCol>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
The .cell style only adds a background to make each column visible; it is not part of the grid. The following examples include the same styling.
span accepts an integer from 0 to 24. Columns within a row can have different widths:
<template>
<HuiSpace direction="vertical" fill>
<HuiRow>
<HuiCol v-for="i in 4" :key="i" :span="6">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">6</div>
</HuiCol>
</HuiRow>
<HuiRow>
<HuiCol v-for="i in 6" :key="i" :span="4">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">4</div>
</HuiCol>
</HuiRow>
<HuiRow>
<HuiCol v-for="i in 8" :key="i" :span="3">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">3</div>
</HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="16"><div class="cell">16</div></HuiCol>
<HuiCol :span="8"><div class="cell cell--light">8</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="4"><div class="cell">4</div></HuiCol>
<HuiCol :span="16"><div class="cell cell--light">16</div></HuiCol>
<HuiCol :span="4"><div class="cell">4</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="16"><div class="cell">16</div></HuiCol>
<HuiCol :span="16"
><div class="cell cell--light">16 (total exceeds 24, wraps to the next line)</div></HuiCol
>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
Gutters
HuiRow.gutter sets column spacing in pixels. An array [horizontal, vertical] also adds spacing between rows, useful when columns wrap.
<template>
<HuiSpace direction="vertical" fill>
<HuiRow :gutter="16">
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
</HuiRow>
<HuiRow :gutter="[16, 16]">
<HuiCol v-for="i in 8" :key="i" :span="6"
><div class="cell">{{ i }}</div></HuiCol
>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
Each HuiCol receives gutter / 2 padding on both sides, offset by -gutter / 2 margins on HuiRow. The first and last columns therefore remain aligned with the row edges. Apply backgrounds to elements inside HuiCol; a background on HuiCol itself also colors the gutter.
Compare different gutter sizes:
<template>
<HuiSpace direction="vertical" fill>
<div v-for="gutter in [0, 8, 16, 32]" :key="gutter">
<div class="label">gutter {{ gutter }}px</div>
<HuiRow :gutter="gutter">
<HuiCol v-for="i in 4" :key="i" :span="6"><div class="cell">6</div></HuiCol>
</HuiRow>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
Changing gutter updates column spacing reactively. Use the sliders below to adjust horizontal and vertical spacing separately:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCol, HuiRow, HuiSlider, HuiSpace } from '@nscode/handy-ui'
const horizontal = ref(16)
const vertical = ref(16)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap size="large">
<span>Horizontal {{ horizontal }}px</span>
<HuiSlider
v-model="horizontal"
:min="0"
:max="48"
:step="8"
show-stops
style="width: 200px"
/>
<span>Vertical {{ vertical }}px</span>
<HuiSlider v-model="vertical" :min="0" :max="48" :step="8" show-stops style="width: 200px" />
</HuiSpace>
<HuiRow :gutter="[horizontal, vertical]">
<HuiCol v-for="i in 8" :key="i" :span="6">
<div class="cell">{{ i }}</div>
</HuiCol>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
Column Offsets
offset leaves the specified number of columns empty to the left. Like span, it accepts 0 through 24.
<template>
<HuiSpace direction="vertical" fill>
<HuiRow>
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
<HuiCol :span="6" :offset="6"><div class="cell">6, offset 6</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="12" :offset="6"><div class="cell">12, offset 6 (centered)</div></HuiCol>
</HuiRow>
<HuiRow v-for="offset in [0, 4, 8, 12, 16, 20]" :key="offset">
<HuiCol :span="4" :offset="offset">
<div class="cell cell--light">Offset {{ offset }}</div>
</HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="6" :offset="2"><div class="cell">6, offset 2</div></HuiCol>
<HuiCol :span="6" :offset="2"><div class="cell">6, offset 2</div></HuiCol>
<HuiCol :span="6" :offset="2"><div class="cell">6, offset 2</div></HuiCol>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
Visual Reordering
push moves a column right and pull moves it left, in column units. They change only its visual position, leaving other columns unaffected. Use them to swap columns without changing template order.
<template>
<HuiRow>
<HuiCol :span="18" :push="6"
><div class="cell">First in the template, displayed on the right</div></HuiCol
>
<HuiCol :span="6" :pull="18"
><div class="cell cell--light">Second in the template, displayed on the left</div></HuiCol
>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
Horizontal Distribution
HuiRow.justify controls horizontal distribution: start (the default), end, center, space-around, space-between, or space-evenly. The difference is visible only when the combined spans leave unused space within the 24-column row.
<script setup lang="ts">
import { HuiCol, HuiRow, HuiSpace } from '@nscode/handy-ui'
import type { RowJustify } from '@nscode/handy-ui'
const justifies: RowJustify[] = [
'start',
'end',
'center',
'space-around',
'space-between',
'space-evenly'
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<div v-for="item in justifies" :key="item">
<div class="label">{{ item }}</div>
<HuiRow :justify="item" class="row">
<HuiCol :span="4"><div class="cell">4</div></HuiCol>
<HuiCol :span="4"><div class="cell cell--light">4</div></HuiCol>
<HuiCol :span="4"><div class="cell">4</div></HuiCol>
</HuiRow>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.row {
outline: 1px dashed var(--hui-border-color);
}
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-dark);
}
</style>
Vertical Alignment
HuiRow.align controls vertical alignment: top (the default), middle, bottom, or stretch. stretch makes every column as tall as the tallest column in the row.
<template>
<HuiRow align="middle" :gutter="16">
<HuiCol :span="8"><div class="cell" style="height: 80px">80px high</div></HuiCol>
<HuiCol :span="8"><div class="cell">Vertically centered</div></HuiCol>
<HuiCol :span="8"><div class="cell" style="height: 50px">50px high</div></HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
Compare all four values below. In each row, the left column is 80px high, the middle has no fixed height and follows stretching, and the right is 50px high:
<script setup lang="ts">
import { HuiCol, HuiRow, HuiSpace } from '@nscode/handy-ui'
import type { RowAlign } from '@nscode/handy-ui'
const aligns: RowAlign[] = ['top', 'middle', 'bottom', 'stretch']
</script>
<template>
<HuiSpace direction="vertical" fill>
<div v-for="item in aligns" :key="item">
<div class="label">{{ item }}</div>
<HuiRow :align="item" :gutter="16">
<HuiCol :span="8"><div class="cell" style="height: 80px">80px high</div></HuiCol>
<HuiCol :span="8"><div class="cell cell--fit">No fixed height</div></HuiCol>
<HuiCol :span="8"><div class="cell" style="height: 50px">50px high</div></HuiCol>
</HuiRow>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.cell {
display: flex;
align-items: center;
justify-content: center;
min-height: 36px;
box-sizing: border-box;
background: var(--hui-fill-color);
}
/* 列被拉伸时,高度 100% 才能跟着列变高 */
.cell--fit {
height: 100%;
background: var(--hui-fill-color-dark);
}
</style>
Disabling Wrapping
HuiRow allows wrapping by default. With wrap=false, columns remain on one line without shrinking. Combined spans greater than 24 overflow the row.
<template>
<HuiRow :wrap="false">
<HuiCol :span="12"><div class="cell">12</div></HuiCol>
<HuiCol :span="12"><div class="cell cell--light">12</div></HuiCol>
<HuiCol :span="12"><div class="cell">12</div></HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
Compare three columns of span 12 with wrapping enabled and disabled. The second example has a horizontally scrollable container so the overflow remains accessible:
<template>
<HuiSpace direction="vertical" fill>
<div>
<div class="label">wrap (default): the third column wraps</div>
<HuiRow>
<HuiCol v-for="i in 3" :key="i" :span="12">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">12</div>
</HuiCol>
</HuiRow>
</div>
<div>
<div class="label">:wrap="false": the third column overflows</div>
<div style="overflow-x: auto">
<HuiRow :wrap="false">
<HuiCol v-for="i in 3" :key="i" :span="12">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">12</div>
</HuiCol>
</HuiRow>
</div>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
Content Width and Hidden Columns
When span is omitted, the column follows its content width. span=0 hides it.
<template>
<HuiRow :gutter="8">
<HuiCol><div class="cell" style="padding: 0 12px">Content width</div></HuiCol>
<HuiCol :span="0"><div class="cell">Hidden</div></HuiCol>
<HuiCol :span="8"><div class="cell cell--light">8</div></HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
Responsive Layouts
xs, sm, md, lg, and xl configure columns by screen width. Each accepts a number, equivalent to span, or an object { span, offset, push, pull }. The breakpoint ranges are:
| Breakpoint | Effective range |
|---|---|
| xs | <640px only |
| sm | From ≥640px, inherited at larger widths |
| md | From ≥768px, inherited at larger widths |
| lg | From ≥1024px, inherited at larger widths |
| xl | From ≥1280px, inherited at larger widths |
Inheritance at larger widths means a breakpoint remains effective on wider screens until a larger breakpoint overrides it. Non-breakpoint props such as span and offset apply at all widths, with breakpoint values taking precedence.
In the following example, each column fills a row below 768px; from 768px there are two columns per row; from 1280px there are four.
<template>
<HuiRow :gutter="[16, 16]">
<HuiCol v-for="i in 4" :key="i" :span="24" :md="12" :xl="6">
<div class="cell">{{ i }}</div>
</HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
With all five breakpoints set, each width range has its own column span. The example below displays the current window width and active breakpoint. Resize the browser window to compare:
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import { HuiCol, HuiRow, HuiSpace } from '@nscode/handy-ui'
const width = ref(0)
const updateWidth = () => {
width.value = window.innerWidth
}
onMounted(() => {
updateWidth()
window.addEventListener('resize', updateWidth)
})
onBeforeUnmount(() => window.removeEventListener('resize', updateWidth))
const current = computed(() => {
if (width.value < 640) return 'xs (24 per column, 1 column per row)'
if (width.value < 768) return 'sm (12 per column, 2 columns per row)'
if (width.value < 1024) return 'md (8 per column, 3 columns per row)'
if (width.value < 1280) return 'lg (6 per column, 4 columns per row)'
return 'xl (4 per column, 6 columns per row)'
})
</script>
<template>
<HuiSpace direction="vertical" fill>
<span>Window width {{ width }}px; active breakpoint: {{ current }}</span>
<HuiRow :gutter="[16, 16]">
<HuiCol v-for="i in 6" :key="i" :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">{{ i }}</div>
</HuiCol>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
An object can change the offset at a breakpoint as well. This column fills the row below 1024px, then occupies 12 centered columns from 1024px:
<template>
<HuiRow>
<HuiCol :span="24" :lg="{ span: 12, offset: 6 }">
<div class="cell">Responsive offset</div>
</HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
Use push and pull within a breakpoint object to reorder columns only on wider screens. Below 1024px, the main content is above the sidebar; from 1024px, the sidebar moves to the left:
<template>
<HuiRow :gutter="[16, 16]">
<HuiCol :span="24" :lg="{ span: 16, push: 8 }">
<div class="cell">Main content (first in the template)</div>
</HuiCol>
<HuiCol :span="24" :lg="{ span: 8, pull: 16 }">
<div class="cell cell--light">Sidebar (second in the template)</div>
</HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
Set a breakpoint to 0 to hide a column from that width upward. A nonzero value at a larger breakpoint makes it visible again:
<template>
<HuiRow :gutter="16">
<HuiCol :span="6" :md="0" :xl="6"><div class="cell">Hidden at 768–1279px</div></HuiCol>
<HuiCol :span="18" :md="24" :xl="18"><div class="cell cell--light">Main content</div></HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
Custom Tags
HuiRow and HuiCol render as div by default. Use tag for other elements, such as ul and li for a list.
<template>
<HuiRow tag="ul" :gutter="16" style="padding: 0; list-style: none">
<HuiCol v-for="i in 3" :key="i" tag="li" :span="8"
><div class="cell">{{ i }}</div></HuiCol
>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
HuiRow Props
| Prop | Type | Default | Description |
|---|---|---|---|
| gutter | number | [number, number] | 0 | Gutter in pixels. An array is [horizontal, vertical], with vertical spacing between rows |
| justify | 'start' | 'end' | 'center' | 'space-around' | 'space-between' | 'space-evenly' | 'start' | Horizontal distribution of columns |
| align | 'top' | 'middle' | 'bottom' | 'stretch' | 'top' | Vertical alignment of columns |
| wrap | boolean | true | Whether overflowing columns wrap |
| tag | string | 'div' | Rendered element |
HuiCol Props
| Prop | Type | Default | Description |
|---|---|---|---|
| span | number | - | Column span from 0 to 24. Omit for content width; 0 hides the column |
| offset | number | - | Empty column units to the left |
| push | number | - | Column units to move right, without affecting other columns |
| pull | number | - | Column units to move left, without affecting other columns |
| xs | number | ColResponsiveProps | - | Settings only below 640px. A number is equivalent to span |
| sm | number | ColResponsiveProps | - | Effective from ≥640px and inherited upward. A number is equivalent to span |
| md | number | ColResponsiveProps | - | Effective from ≥768px and inherited upward. A number is equivalent to span |
| lg | number | ColResponsiveProps | - | Effective from ≥1024px and inherited upward. A number is equivalent to span |
| xl | number | ColResponsiveProps | - | Effective from ≥1280px and inherited upward. A number is equivalent to span |
| tag | string | 'div' | Rendered element |
HuiRow Slots
| Slot | Scope arguments | Description |
|---|---|---|
| default | - | Place HuiCol components here |
HuiCol Slots
| Slot | Scope arguments | Description |
|---|---|---|
| default | - | Column content |
Types
import type { RowProps, ColProps, ColResponsiveProps, RowJustify, RowAlign } from '@nscode/handy-ui'
type RowJustify = 'start' | 'end' | 'center' | 'space-around' | 'space-between' | 'space-evenly'
type RowAlign = 'top' | 'middle' | 'bottom' | 'stretch'
interface RowProps {
gutter?: number | [number, number]
justify?: RowJustify
align?: RowAlign
wrap?: boolean
tag?: string
}
/** Object format for a breakpoint prop */
interface ColResponsiveProps {
span?: number
offset?: number
push?: number
pull?: number
}
interface ColProps extends ColResponsiveProps {
xs?: number | ColResponsiveProps
sm?: number | ColResponsiveProps
md?: number | ColResponsiveProps
lg?: number | ColResponsiveProps
xl?: number | ColResponsiveProps
tag?: string
}
RowContext and ROW_INJECTION_KEY are also exported. HuiCol uses them to read its row's gutter; application code normally does not need them.