Element UI布局组件:容器组件的高效应用

在本篇文章中,我们将深入探讨如何使用 ElementUI 提供的布局容器组件来构建一个结构化的网页界面。
从基础的容器创建到高级的嵌套布局,本文将一步步演示如何利用 Element UI 的布局容器组件来设计出既美观又实用的网页布局。我们将学习如何通过简单的标签和属性来控制页面的顶部、侧边、主要内容和底部区域,以及如何通过调整属性来满足不同的设计需求。

在这里插入图片描述

ElementUI 专栏 : https://blog.csdn.net/m0_53117341/category_12780595.html

一 . 创建布局容器

创建一个布局容器非常简单 , 我们只需要使用 标签即可创建出一个最基本的外层容器

那在这个外层容器中 , 我们又可以添加许多子元素

在 ElementUI 中 , 将子元素分成了以下四种 :

  • : 顶栏容器
  • : 侧边栏容器
  • : 主要区域容器
  • : 底栏容器

2.1 注册路由

然后我们就可以将该组件注册到路由中

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Button from '@/components/Button'
import ButtonDetail from '@/components/ButtonDetail'
import Link from '@/components/Link'
import Layout from '@/components/Layout'
import Container from '@/components/Container'

Vue.use(Router)

export default new Router({
  routes: [
    { path: '/', name: 'HelloWorld', component: HelloWorld },
    { path: '/button', name: 'Button', component: Button },
    { path: '/buttondetail', name: 'ButtonDetail', component: ButtonDetail },
    { path: '/link', name: 'Link', component: Link },
    { path: '/layout', name: 'Layout', component: Layout },
    { path: '/container', name: 'Container', component: Container },
  ]
})

然后在 App.vue 中添加对应的链接

<template>
  <div id="app">
    <!-- 我们自己的标签页 -->
    <a href="#/button">点我显示 Button</a> <br>
    <!-- URL 必须是 #/ 开头 -->
    <a href="#/buttondetail">点我学习按钮的具体用法</a> <br>
    <a href="#/link">文字链接组件</a> <br>
    <a href="#/layout">布局组件</a> <br>
    <a href="#/container">容器布局组件</a> <br>

    <!-- Vue 的路由 -->
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

2.2 创建容器子元素

我们首先需要创建一个外层容器 , 使用 el-container 标签

在外层容器中 , 我们可以创建各种子元素 , 比如 : header、aside 等等

那我们可以看一下效果

我们可以使用官网提供的 CSS 样式来美化布局 : https://element.eleme.cn/#/zh-CN/component/container

将这段代码粘贴到我们下方的 style 中 , 然后删除我们自己写的样式

<template>
  <div>
    <h1>容器组件的使用</h1>
    <!-- 使用 el-container 标签创建外层容器 -->
    <el-container>
      <!-- 顶栏容器 -->
      <el-header><div>顶栏容器</div></el-header>

      <!-- 侧边栏容器 -->
      <el-aside><div>侧边栏容器</div></el-aside>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  }
}
</script>

<style>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>

2.3 容器的嵌套使用

我们可以继续添加其他子元素 , 比如 : el-main

来看一下效果

那这就需要我们将侧边栏容器和主要区域容器放到一个容器中 , 实现嵌套的效果

<template>
  <div>
    <h1>容器组件的使用</h1>
    <!-- 使用 el-container 标签创建外层容器 -->
    <el-container>
      <!-- 顶栏容器 -->
      <el-header><div>顶栏容器</div></el-header>

      <!-- 使用 el-container 将侧边栏容器和主要区域容器嵌套起来 -->
      <el-container>
        <!-- 侧边栏容器 -->
        <el-aside><div>侧边栏容器</div></el-aside>

        <!-- 主要区域容器 -->
        <el-main><div>主要区域容器</div></el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  }
}
</script>

<style>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>

此时 , 中心容器就来到了正确的位置

三 . 布局容器的属性

3.1 外层容器的属性

direction 属性指的是子元素水平排列还是垂直排列

水平排列类似于这样

垂直排列类似于这样

那需要注意的是 , 子元素中只要有 el-header 或 el-footer 的时候 , 就为垂直容器,否则为水平容器

那我们目前的代码就是垂直排列的效果

我们也可以设置成水平排列看一下效果 , 通过 direction 属性设置成 horizontal

<template>
  <div>
    <h1>容器组件的使用</h1>
    <!-- 使用 el-container 标签创建外层容器 -->
    <!-- direction="horizontal" 表示水平排列 -->
    <el-container direction="horizontal">
      <!-- 顶栏容器 -->
      <el-header><div>顶栏容器</div></el-header>

      <!-- 使用 el-container 将侧边栏容器和主要区域容器嵌套起来 -->
      <el-container>
        <!-- 侧边栏容器 -->
        <el-aside><div>侧边栏容器</div></el-aside>

        <!-- 主要区域容器 -->
        <el-main><div>主要区域容器</div></el-main>
      </el-container>

      <!-- 底栏容器 -->
      <el-footer><div>底栏容器</div></el-footer>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  }
}
</script>

<style>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>

3.2 顶栏容器的属性

我们可以设置 height 属性来设置顶栏容器的高度

<template>
  <div>
    <h1>容器组件的使用</h1>
    <!-- 使用 el-container 标签创建外层容器 -->
    <el-container>
      <!-- 顶栏容器 -->
      <!-- 通过 height 属性设置顶栏容器的高度 -->
      <el-header height="300px"><div>顶栏容器</div></el-header>

      <!-- 使用 el-container 将侧边栏容器和主要区域容器嵌套起来 -->
      <el-container>
        <!-- 侧边栏容器 -->
        <el-aside><div>侧边栏容器</div></el-aside>

        <!-- 主要区域容器 -->
        <el-main><div>主要区域容器</div></el-main>
      </el-container>

      <!-- 底栏容器 -->
      <el-footer><div>底栏容器</div></el-footer>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  }
}
</script>

<style>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>

3.3 侧边栏容器的属性

我们可以通过 width 属性设置侧边栏的宽度

<template>
  <div>
    <h1>容器组件的使用</h1>
    <!-- 使用 el-container 标签创建外层容器 -->
    <el-container>
      <!-- 顶栏容器 -->
      <el-header><div>顶栏容器</div></el-header>

      <!-- 使用 el-container 将侧边栏容器和主要区域容器嵌套起来 -->
      <el-container>
        <!-- 侧边栏容器 -->
        <!-- 通过 width 属性设置侧边栏容器的宽度 -->
        <el-aside width="600px"><div>侧边栏容器</div></el-aside>

        <!-- 主要区域容器 -->
        <el-main><div>主要区域容器</div></el-main>
      </el-container>

      <!-- 底栏容器 -->
      <el-footer><div>底栏容器</div></el-footer>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  }
}
</script>

<style>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>

3.4 底栏容器属性

我们可以通过 height 属性设置底栏高度

<template>
  <div>
    <h1>容器组件的使用</h1>
    <!-- 使用 el-container 标签创建外层容器 -->
    <el-container>
      <!-- 顶栏容器 -->
      <el-header><div>顶栏容器</div></el-header>

      <!-- 使用 el-container 将侧边栏容器和主要区域容器嵌套起来 -->
      <el-container>
        <!-- 侧边栏容器 -->
        <el-aside><div>侧边栏容器</div></el-aside>

        <!-- 主要区域容器 -->
        <el-main><div>主要区域容器</div></el-main>
      </el-container>

      <!-- 底栏容器 -->
      <!-- 通过 height 属性设置底栏容器的高 -->
      <el-footer height="300px"><div>底栏容器</div></el-footer>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  }
}
</script>

<style>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>


小结 :

在本文中,我们全面探索了 Element UI 框架中的布局容器组件,这些组件是构建现代网页布局的基础。通过使用 <el-container> , <el-header> , <el-aside> , <el-main> , 和 <el-footer> ,我们能够创建出结构清晰、响应灵敏的网页界面。我们学习了如何通过简单的标签和属性设置来控制页面的不同部分,包括顶栏、侧边栏、主要内容和底栏区域。

我们不仅展示了如何单独使用这些组件,还深入讨论了它们的嵌套使用和如何通过调整 direction, height, 和 width 等属性来满足多样化的设计需求。

如果对你有帮助的话 , 还请一键三连支持~
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加勒比海涛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值