Layout 布局
通过基础的 24 分栏,迅速简便地创建布局。
基础布局
通过 row 和 col 组件,并通过 col 组件的 span 属性我们就可以自由地组合布局。
分栏间隔
行提供 gutter 属性来指定列之间的间距,其默认值为0。
<template>
<el-row :gutter="20">
<el-col :span="6"><div class="grid-content ep-bg-purple" /></el-col>
<el-col :span="6"><div class="grid-content ep-bg-purple" /></el-col>
<el-col :span="6"><div class="grid-content ep-bg-purple" /></el-col>
<el-col :span="6"><div class="grid-content ep-bg-purple" /></el-col>
</el-row>
</template>
<style>
.el-row {
margin-bottom: 20px;
}
.el-row:last-child {
margin-bottom: 0;
}
.el-col {
border-radius: 4px;
}
.grid-content {
border-radius: 4px;
min-height: 36px;
}
</style>
列偏移
通过制定 col 组件的 offset 属性可以指定分栏偏移的栏数。
<template>
<el-row :gutter="20">
<el-col :span="6"><div class="grid-content ep-bg-purple" /></el-col>
<el-col :span="6" :offset="6">
<div class="grid-content ep-bg-purple" />
</el-col>
</el-row>
<el-row :gutter="20">
<el-col :span="6" :offset="6">
<div class="grid-content ep-bg-purple" />
</el-col>
<el-col :span="6" :offset="6">
<div class="grid-content ep-bg-purple" />
</el-col>
</el-row>
<el-row :gutter="20">
<el-col :span="12" :offset="6">
<div class="grid-content ep-bg-purple" />
</el-col>
</el-row>
</template>
<style>
.el-row {
margin-bottom: 20px;
}
.el-row:last-child {
margin-bottom: 0;
}
.el-col {
border-radius: 4px;
}
.grid-content {
border-radius: 4px;
min-height: 36px;
}
</style>
对齐方式
默认使用 flex 布局来对分栏进行灵活的对齐。
您可以通过justify 属性来定义子元素的排版方式,其取值为start、center、end、space-between、space-around或space-evenly。
<template>
<el-row class="row-bg" justify="space-between">
<el-col :span="6"><div class="grid-content ep-bg-purple" /></el-col>
<el-col :span="6"><div class="grid-content ep-bg-purple-light" /></el-col>
<el-col :span="6"><div class="grid-content ep-bg-purple" /></el-col>
</el-row>
</template>
响应式布局
参照了 Bootstrap 的 响应式设计,预设了五个响应尺寸:xs、sm、md、lg 和 xl。
<template>
<el-row :gutter="10">
<el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1">
<div class="grid-content ep-bg-purple" />
</el-col>
<el-col :xs="4" :sm="6" :md="8" :lg="9" :xl="11">
<div class="grid-content ep-bg-purple-light" />
</el-col>
<el-col :xs="4" :sm="6" :md="8" :lg="9" :xl="11">
<div class="grid-content ep-bg-purple" />
</el-col>
<el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1">
<div class="grid-content ep-bg-purple-light" />
</el-col>
</el-row>
</template>
<style>
.el-col {
border-radius: 4px;
}
.grid-content {
border-radius: 4px;
min-height: 36px;
}
</style>
Button 按钮
基础用法
使用 type、plain、round 和 circle 来定义按钮的样式。
<template>
<div class="mb-4">
<el-button>Default</el-button>
<el-button type="primary">Primary</el-button>
<el-button type="success">Success</el-button>
<el-button type="info">Info</el-button>
<el-button type="warning">Warning</el-button>
<el-button type="danger">Danger</el-button>
</div>
<div class="mb-4">
<el-button plain>Plain</el-button>
<el-button type="primary" plain>Primary</el-button>
<el-button type="success" plain>Success</el-button>
<el-button type="info" plain>Info</el-button>
<el-button type="warning" plain>Warning</el-button>
<el-button type="danger" plain>Danger</el-button>
</div>
<div class="mb-4">
<el-button round>Round</el-button>
<el-button type="primary" round>Primary</el-button>
<el-button type="success" round>Success</el-button>
<el-button type="info" round>Info</el-button>
<el-button type="warning" round>Warning</el-button>
<el-button type="danger" round>Danger</el-button>
</div>
<div>
<el-button :icon="Search" circle />
<el-button type="primary" :icon="Edit" circle />
<el-button type="success" :icon="Check" circle />
<el-button type="info" :icon="Message" circle />
<el-button type="warning" :icon="Star" circle />
<el-button type="danger" :icon="Delete" circle />
</div>
</template>
<script lang="ts" setup>
import {
Check,
Delete,
Edit,
Message,
Search,
Star,
} from '@element-plus/icons-vue'
</script>
禁用状态
使用 disabled 属性来控制按钮是否为禁用状态。 该属性接受一个 Boolean 类型的值。
<template>
<div class="mb-4">
<el-button disabled>Default</el-button>
<el-button type="primary" disabled>Primary</el-button>
<el-button type="success" disabled>Success</el-button>
<el-button type="info" disabled>Info</el-button>
<el-button type="warning" disabled>Warning</el-button>
<el-button type="danger" disabled>Danger</el-button>
</div>
<div>
<el-button plain disabled>Plain</el-button>
<el-button type="primary" plain disabled>Primary</el-button>
<el-button type="success" plain disabled>Success</el-button>
<el-button type="info" plain disabled>Info</el-button>
<el-button type="warning" plain disabled>Warning</el-button>
<el-button type="danger" plain disabled>Danger</el-button>
</div>
</template>
链接按钮
使用 link 属性表示链接。
<template>
<p>Basic link button</p>
<div class="mb-4">
<el-button
v-for="button in buttons"
:key="button.text"
:type="button.type"
link
>
{{ button.text }}
</el-button>
</div>
<p>Disabled link button</p>
<div>
<el-button
v-for="button in buttons"
:key="button.text"
:type="button.type"
link
disabled
>
{{ button.text }}
</el-button>
</div>
</template>
<script setup lang="ts">
const buttons = [
{ type: '', text: 'plain' },
{ type: 'primary', text: 'primary' },
{ type: 'success', text: 'success' },
{ type: 'info', text: 'info' },
{ type: 'warning', text: 'warning' },
{ type: 'danger', text: 'danger' },
] as const
</script>
文字按钮
没有边框和背景色的按钮。使用 text 属性表示链接。
<template>
<p>Basic text button</p>
<div class="mb-4">
<el-button
v-for="button in buttons"
:key="button.text"
:type="button.type"
text
>
{{ button.text }}
</el-button>
</div>
<p>Background color always on</p>
<div class="mb-4">
<el-button
v-for="button in buttons"
:key="button.text"
:type="button.type"
text
bg
>
{{ button.text }}
</el-button>
</div>
<p>Disabled text button</p>
<div>
<el-button
v-for="button in buttons"
:key="button.text"
:type="button.type"
text
disabled
>
{{ button.text }}
</el-button>
</div>
</template>
<script setup lang="ts">
const buttons = [
{ type: '', text: 'plain' },
{ type: 'primary', text: 'primary' },
{ type: 'success', text: 'success' },
{ type: 'info', text: 'info' },
{ type: 'warning', text: 'warning' },
{ type: 'danger', text: 'danger' },
] as const
</script>
图标按钮
使用 icon 属性来为按钮添加图标。
<template>
<div>
<el-button type="primary" :icon="Edit" />
<el-button type="primary" :icon="Share" />
<el-button type="primary" :icon="Delete" />
<el-button type="primary" :icon="Search">Search</el-button>
<el-button type="primary">
Upload<el-icon class="el-icon--right"><Upload /></el-icon>
</el-button>
</div>
</template>
<script setup lang="ts">
import { Delete, Edit, Search, Share, Upload } from '@element-plus/icons-vue'
</script>
按钮组
使用 <el-button-group> 对多个按钮分组。
<template>
<el-button-group>
<el-button type="primary" :icon="ArrowLeft">Previous Page</el-button>
<el-button type="primary">
Next Page<el-icon class="el-icon--right"><ArrowRight /></el-icon>
</el-button>
</el-button-group>
<el-button-group class="ml-4">
<el-button type="primary" :icon="Edit" />
<el-button type="primary" :icon="Share" />
<el-button type="primary" :icon="Delete" />
</el-button-group>
</template>
<script setup lang="ts">
import {
ArrowLeft,
ArrowRight,
Delete,
Edit,
Share,
} from '@element-plus/icons-vue'
</script>
加载状态按钮
通过设置 loading 属性为 true 来显示加载中状态。
<template>
<el-button type="primary" loading>Loading</el-button>
<el-button type="primary" :loading-icon="Eleme" loading>Loading</el-button>
<el-button type="primary" loading>
<template #loading>
<div class="custom-loading">
<svg class="circular" viewBox="-10, -10, 50, 50">
<path
class="path"
d="
M 30 15
L 28 17
M 25.61 25.61
A 15 15, 0, 0, 1, 15 30
A 15 15, 0, 1, 1, 27.99 7.5
L 15 15
"
style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"
/>
</svg>
</div>
</template>
Loading
</el-button>
</template>
<script lang="ts" setup>
import { Eleme } from '@element-plus/icons-vue'
</script>
<style scoped>
.el-button .custom-loading .circular {
margin-right: 6px;
width: 18px;
height: 18px;
animation: loading-rotate 2s linear infinite;
}
.el-button .custom-loading .circular .path {
animation: loading-dash 1.5s ease-in-out infinite;
stroke-dasharray: 90, 150;
stroke-dashoffset: 0;
stroke-width: 2;
stroke: var(--el-button-text-color);
stroke-linecap: round;
}
</style>
调整尺寸
使用 size 属性额外配置尺寸,可使用 large和small两种值。
<template>
<div class="flex flex-wrap items-center mb-4">
<el-button size="large">Large</el-button>
<el-button>Default</el-button>
<el-button size="small">Small</el-button>
<el-button size="large" :icon="Search">Search</el-button>
<el-button :icon="Search">Search</el-button>
<el-button size="small" :icon="Search">Search</el-button>
</div>
<div class="flex flex-wrap items-center mb-4">
<el-button size="large" round>Large</el-button>
<el-button round>Default</el-button>
<el-button size="small" round>Small</el-button>
<el-button size="large" :icon="Search" round>Search</el-button>
<el-button :icon="Search" round>Search</el-button>
<el-button size="small" :icon="Search" round>Search</el-button>
</div>
<div class="flex flex-wrap items-center">
<el-button :icon="Search" size="large" circle />
<el-button :icon="Search" circle />
<el-button :icon="Search" size="small" circle />
</div>
</template>
<script setup lang="ts">
import { Search } from '@element-plus/icons-vue'
</script>
自定义颜色
<script lang="ts" setup>
import { isDark } from '~/composables/dark'
</script>
<template>
<div>
<el-button color="#626aef" :dark="isDark">Default</el-button>
<el-button color="#626aef" :dark="isDark" plain>Plain</el-button>
<el-button color="#626aef" :dark="isDark" disabled>Disabled</el-button>
<el-button color="#626aef" :dark="isDark" disabled plain>
Disabled Plain
</el-button>
</div>
</template>
Container 布局容器
- <el-container>:外层容器。 当子元素中包含 <el-header> 或 <el-footer> 时,全部子元素会垂直上下排列, 否则会水平左右排列。
- <el-header>:顶栏容器。
- <el-aside>:侧边栏容器。
- <el-main>:主要区域容器。
- <el-footer>:底栏容器。
<template>
<div class="common-layout">
<el-container>
<el-aside width="200px">Aside</el-aside>
<el-container>
<el-header>Header</el-header>
<el-main>Main</el-main>
<el-footer>Footer</el-footer>
</el-container>
</el-container>
</div>
</template>
Border 边框
边框样式
<template>
<table class="demo-border">
<tbody>
<tr>
<td class="text">Name</td>
<td class="text">Thickness</td>
<td class="line">Demo</td>
</tr>
<tr>
<td class="text">Solid</td>
<td class="text">1px</td>
<td class="line">
<div />
</td>
</tr>
<tr>
<td class="text">Dashed</td>
<td class="text">2px</td>
<td class="line">
<div class="dashed" />
</td>
</tr>
</tbody>
</table>
</template>
<style scoped>
.demo-border .text {
width: 15%;
}
.demo-border .line {
width: 70%;
}
.demo-border .line div {
width: 100%;
height: 0;
border-top: 1px solid var(--el-border-color);
}
.demo-border .line .dashed {
border-top: 2px dashed var(--el-border-color);
}
</style>
圆角
<template>
<el-row :gutter="12" class="demo-radius">
<el-col
v-for="(radius, i) in radiusGroup"
:key="i"
:span="6"
:xs="{ span: 12 }"
>
<div class="title">{{ radius.name }}</div>
<div class="value">
<code>
border-radius:
{{
radius.type
? useCssVar(`--el-border-radius-${radius.type}`)
: '"0px"'
}}
</code>
</div>
<div
class="radius"
:style="{
borderRadius: radius.type
? `var(--el-border-radius-${radius.type})`
: '',
}"
/>
</el-col>
</el-row>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { useCssVar } from '@vueuse/core'
const radiusGroup = ref([
{
name: 'No Radius',
type: '',
},
{
name: 'Small Radius',
type: 'small',
},
{
name: 'Large Radius',
type: 'base',
},
{
name: 'Round Radius',
type: 'round',
},
])
</script>
<style scoped>
.demo-radius .title {
color: var(--el-text-color-regular);
font-size: 18px;
margin: 10px 0;
}
.demo-radius .value {
color: var(--el-text-color-primary);
font-size: 16px;
margin: 10px 0;
}
.demo-radius .radius {
height: 40px;
width: 70%;
border: 1px solid var(--el-border-color);
border-radius: 0;
margin-top: 20px;
}
</style>
阴影
<template>
<div class="flex justify-between items-center flex-wrap">
<div
v-for="(shadow, i) in shadowGroup"
:key="i"
class="flex flex-col justify-center items-center"
m="auto"
w="46"
>
<div
class="inline-flex"
h="30"
w="30"
m="2"
:style="{
boxShadow: `var(${getCssVarName(shadow.type)})`,
}"
/>
<span p="y-4" class="demo-shadow-text" text="sm">
{{ shadow.name }}
</span>
<code text="xs">
{{ getCssVarName(shadow.type) }}
</code>
</div>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const shadowGroup = ref([
{
name: 'Basic Shadow',
type: '',
},
{
name: 'Light Shadow',
type: 'light',
},
{
name: 'Lighter Shadow',
type: 'lighter',
},
{
name: 'Dark Shadow',
type: 'dark',
},
])
const getCssVarName = (type: string) => {
return `--el-box-shadow${type ? '-' : ''}${type}`
}
</script>
Icon 图标
安装
npm install @element-plus/icons-vue
注册所有图标
// main.ts
// 如果您正在使用CDN引入,请删除下面一行。
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component)
}
结合 el-icon 使用
<template>
<p>
with extra class <b>is-loading</b>, your icon is able to rotate 360 deg in 2
seconds, you can also override this
</p>
<el-icon :size="20">
<Edit />
</el-icon>
<el-icon color="#409efc" class="no-inherit">
<Share />
</el-icon>
<el-icon>
<Delete />
</el-icon>
<el-icon class="is-loading">
<Loading />
</el-icon>
<el-button type="primary">
<el-icon style="vertical-align: middle">
<Search />
</el-icon>
<span style="vertical-align: middle"> Search </span>
</el-button>
</template>
直接使用 SVG 图标
<template>
<div style="font-size: 20px">
<!-- 由于SVG图标默认不携带任何属性 -->
<!-- 你需要直接提供它们 -->
<Edit style="width: 1em; height: 1em; margin-right: 8px" />
<Share style="width: 1em; height: 1em; margin-right: 8px" />
<Delete style="width: 1em; height: 1em; margin-right: 8px" />
<Search style="width: 1em; height: 1em; margin-right: 8px" />
</div>
</template>
Link 链接
基础用法
<template>
<div>
<el-link href="https://element-plus.org" target="_blank">default</el-link>
<el-link type="primary">primary</el-link>
<el-link type="success">success</el-link>
<el-link type="warning">warning</el-link>
<el-link type="danger">danger</el-link>
<el-link type="info">info</el-link>
</div>
</template>
<style scoped>
.el-link {
margin-right: 8px;
}
</style>
禁用状态
<template>
<div>
<el-link disabled>default</el-link>
<el-link type="primary" disabled>primary</el-link>
<el-link type="success" disabled>success</el-link>
<el-link type="warning" disabled>warning</el-link>
<el-link type="danger" disabled>danger</el-link>
<el-link type="info" disabled>info</el-link>
</div>
</template>
<style scoped>
.el-link {
margin-right: 8px;
}
</style>
下划线
<template>
<div>
<el-link>default</el-link>
<el-link underline="always">always</el-link>
<el-link underline="hover">hover</el-link>
<el-link underline="never">never</el-link>
</div>
</template>
<style scoped>
.el-link {
margin-right: 8px;
}
</style>
带图标的链接
<template>
<div>
<el-link :icon="Edit">Edit</el-link>
<el-link>
Check<el-icon class="el-icon--right"><icon-view /></el-icon>
</el-link>
</div>
</template>
<script setup lang="ts">
import { Edit, View as IconView } from '@element-plus/icons-vue'
</script>
<style scoped>
.el-link {
margin-right: 8px;
}
</style>
Text
基础用法
<template>
<el-text class="mx-1">Default</el-text>
<el-text class="mx-1" type="primary">Primary</el-text>
<el-text class="mx-1" type="success">Success</el-text>
<el-text class="mx-1" type="info">Info</el-text>
<el-text class="mx-1" type="warning">Warning</el-text>
<el-text class="mx-1" type="danger">Danger</el-text>
</template>
尺寸
使用 size 属性配置尺寸,可选的尺寸大小有: large, default 或 small
<template>
<el-text class="mx-1" size="large">Large</el-text>
<el-text class="mx-1">Default</el-text>
<el-text class="mx-1" size="small">Small</el-text>
</template>
省略
通过 truncated 属性,在文本超过视图或最大宽度设置时展示省略符。 通过 line-clamp 属性控制多行的样式
<template>
<el-text class="w-150px mb-2" truncated>
Self element set width 100px
</el-text>
<el-row class="w-150px mb-2">
<el-text truncated>Squeezed by parent element</el-text>
</el-row>
<el-text line-clamp="2">
The -webkit-line-clamp CSS property<br />
allows limiting of the contents of<br />
a block to the specified number of lines.
</el-text>
</template>
覆盖
使用属性 tag 覆盖元素
<template>
<el-space direction="vertical">
<el-text>span</el-text>
<el-text tag="p">This is a paragraph.</el-text>
<el-text tag="b">Bold</el-text>
<el-text tag="i">Italic</el-text>
<el-text>
This is
<el-text tag="sub" size="small">subscript</el-text>
</el-text>
<el-text>
This is
<el-text tag="sup" size="small">superscript</el-text>
</el-text>
<el-text tag="ins">Inserted</el-text>
<el-text tag="del">Deleted</el-text>
<el-text tag="mark">Marked</el-text>
</el-space>
</template>
混合使用
<template>
<el-space direction="vertical">
<el-text>
<el-icon>
<ElementPlus />
</el-icon>
Element-Plus
</el-text>
<el-row>
<el-text>Rate</el-text>
<el-rate class="ml-1" />
</el-row>
<el-text>
This is text mixed icon
<el-icon>
<Bell />
</el-icon>
and component
<el-button>Button</el-button>
</el-text>
</el-space>
</template>
<script lang="ts" setup>
import { Bell, ElementPlus } from '@element-plus/icons-vue'
</script>
Scrollbar 滚动条
基础用法
通过 height 属性设置滚动条高度,若不设置则根据父容器高度自适应。
<template>
<el-scrollbar height="400px">
<p v-for="item in 20" :key="item" class="scrollbar-demo-item">{{ item }}</p>
</el-scrollbar>
</template>
<style scoped>
.scrollbar-demo-item {
display: flex;
align-items: center;
justify-content: center;
height: 50px;
margin: 10px;
text-align: center;
border-radius: 4px;
background: var(--el-color-primary-light-9);
color: var(--el-color-primary);
}
</style>
横向滚动
当元素宽度大于滚动条宽度时,会显示横向滚动条。(不需要额外的属性)
<template>
<el-scrollbar>
<div class="scrollbar-flex-content">
<p v-for="item in 50" :key="item" class="scrollbar-demo-item">
{{ item }}
</p>
</div>
</el-scrollbar>
</template>
<style scoped>
.scrollbar-flex-content {
display: flex;
width: fit-content;
}
.scrollbar-demo-item {
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
width: 100px;
height: 50px;
margin: 10px;
text-align: center;
border-radius: 4px;
background: var(--el-color-danger-light-9);
color: var(--el-color-danger);
}
</style>
最大高度
当元素高度超过最大高度,才会显示滚动条。
<template>
<el-button @click="add">Add Item</el-button>
<el-button @click="onDelete">Delete Item</el-button>
<el-scrollbar max-height="400px">
<p v-for="item in count" :key="item" class="scrollbar-demo-item">
{{ item }}
</p>
</el-scrollbar>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const count = ref(3)
const add = () => {
count.value++
}
const onDelete = () => {
if (count.value > 0) {
count.value--
}
}
</script>
<style scoped>
.scrollbar-demo-item {
display: flex;
align-items: center;
justify-content: center;
height: 50px;
margin: 10px;
text-align: center;
border-radius: 4px;
background: var(--el-color-primary-light-9);
color: var(--el-color-primary);
}
</style>
手动滚动
通过使用 setScrollTop 与 setScrollLeft 方法,可以手动控制滚动条滚动。
<template>
<el-scrollbar ref="scrollbarRef" height="400px" always @scroll="scroll">
<div ref="innerRef">
<p v-for="item in 20" :key="item" class="scrollbar-demo-item">
{{ item }}
</p>
</div>
</el-scrollbar>
<el-slider
v-model="value"
:max="max"
:format-tooltip="formatTooltip"
@input="inputSlider"
/>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import type { ScrollbarInstance } from 'element-plus'
type Arrayable<T> = T | T[]
const max = ref(0)
const value = ref(0)
const innerRef = ref<HTMLDivElement>()
const scrollbarRef = ref<ScrollbarInstance>()
onMounted(() => {
max.value = innerRef.value!.clientHeight - 380
})
const inputSlider = (value: Arrayable<number>) => {
scrollbarRef.value!.setScrollTop(value as number)
}
const scroll = ({ scrollTop }) => {
value.value = scrollTop
}
const formatTooltip = (value: number) => `${value} px`
</script>
<style scoped>
.scrollbar-demo-item {
display: flex;
align-items: center;
justify-content: center;
height: 50px;
margin: 10px;
text-align: center;
border-radius: 4px;
background: var(--el-color-primary-light-9);
color: var(--el-color-primary);
}
.el-slider {
margin-top: 20px;
}
</style>
Space 间距
基础用法
最基础的用法,通过这个组件来给组件之间提供统一的间距。
<template>
<el-space wrap>
<el-card v-for="i in 3" :key="i" class="box-card" style="width: 250px">
<template #header>
<div class="card-header">
<span>Card name</span>
<el-button class="button" text>Operation button</el-button>
</div>
</template>
<div v-for="o in 4" :key="o" class="text item">
{{ 'List item ' + o }}
</div>
</el-card>
</el-space>
</template>
垂直布局
使用 direction 来控制布局的方式, 背后实际上是利用了 flex-direction 来控制.
<template>
<el-space direction="vertical">
<el-card v-for="i in 2" :key="i" class="box-card" style="width: 250px">
<template #header>
<div class="card-header">
<span>Card name</span>
<el-button class="button" text>Operation button</el-button>
</div>
</template>
<div v-for="o in 4" :key="o" class="text item">
{{ 'List item ' + o }}
</div>
</el-card>
</el-space>
</template>
控制间距的大小
- 通过调整 size 的值来控制间距的大小
- 使用内置的 small、default、large 来设置间距大小,分别对应 8px、12px 和 16px 的间距。 默认的间距大小为 small,也就是 8px。
- 也可以通过自定义的 size 来控制大小.
<template>
<el-space direction="vertical" alignment="start" :size="30">
<el-radio-group v-model="size">
<el-radio value="large">Large</el-radio>
<el-radio value="default">Default</el-radio>
<el-radio value="small">Small</el-radio>
</el-radio-group>
<el-space wrap :size="size">
<el-card v-for="i in 3" :key="i" class="box-card" style="width: 250px">
<template #header>
<div class="card-header">
<span>Card name</span>
<el-button class="button" text>Operation button</el-button>
</div>
</template>
<div v-for="o in 4" :key="o" class="text item">
{{ 'List item ' + o }}
</div>
</el-card>
</el-space>
</el-space>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import type { ComponentSize } from 'element-plus'
const size = ref<ComponentSize>('default')
</script>
自定义 Size
<template>
<el-slider v-model="size" />
<el-space wrap :size="size">
<el-card v-for="i in 2" :key="i" class="box-card" style="width: 250px">
<template #header>
<div class="card-header">
<span>Card name</span>
<el-button class="button" text>Operation button</el-button>
</div>
</template>
<div v-for="o in 4" :key="o" class="text item">
{{ 'List item ' + o }}
</div>
</el-card>
</el-space>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const size = ref(20)
</script>
自动换行
利用 wrap 属性控制换行
<template>
<el-space wrap>
<div v-for="i in 20" :key="i">
<el-button text> Text button </el-button>
</div>
</el-space>
</template>
行间分隔符
- 字母数字类型分隔符
<template>
<el-space :size="size" spacer="|">
<div v-for="i in 2" :key="i">
<el-button> button {{ i }} </el-button>
</div>
</el-space>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const size = ref(10)
</script>
- 分隔符还可以是 VNode 类型
<template>
<el-space :size="size" :spacer="spacer">
<div v-for="i in 2" :key="i">
<el-button> button {{ i }} </el-button>
</div>
</el-space>
</template>
<script lang="ts" setup>
import { h, ref } from 'vue'
import { ElDivider } from 'element-plus'
const size = ref(10)
const spacer = h(ElDivider, { direction: 'vertical' })
</script>
填充容器
- 用 fill 属性让子节点自动填充容器
<template>
<div>
<div style="margin-bottom: 15px">fill: <el-switch v-model="fill" /></div>
<el-space :fill="fill" wrap>
<el-card v-for="i in 3" :key="i" class="box-card">
<template #header>
<div class="card-header">
<span>Card name</span>
<el-button class="button" text>Operation button</el-button>
</div>
</template>
<div v-for="o in 4" :key="o" class="text item">
{{ 'List item ' + o }}
</div>
</el-card>
</el-space>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const fill = ref(true)
</script>
- 用 fillRatio 自定义填充比例
<template>
<div>
<div style="margin-bottom: 15px">
direction:
<el-radio v-model="direction" value="horizontal">horizontal</el-radio>
<el-radio v-model="direction" value="vertical">vertical</el-radio>
</div>
<div style="margin-bottom: 15px">
fillRatio:<el-slider v-model="fillRatio" />
</div>
<el-space
fill
wrap
:fill-ratio="fillRatio"
:direction="direction"
style="width: 100%"
>
<el-card v-for="i in 5" :key="i" class="box-card">
<template #header>
<div class="card-header">
<span>Card name</span>
<el-button class="button" text>Operation button</el-button>
</div>
</template>
<div v-for="o in 4" :key="o" class="text item">
{{ 'List item ' + o }}
</div>
</el-card>
</el-space>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import type { SpaceInstance } from 'element-plus'
const direction = ref<SpaceInstance['direction']>('horizontal')
const fillRatio = ref(30)
</script>