第三方UI组件库的样式修改

一、场景:

一般来说,我们在使用第三方UI组件库(如:vant,element-plus等)时,UI组件库自带的样式不能满足用户的个性化需求时,就需要我们开发人员自己动手对组件库的局部样式进行修改。

二、修改UI组件库的顺序和方法

1、修改主题

1、修改主题:每个ui组件库都有专门的修改主题的解释,如vant。Vant 3 - Lightweight Mobile UI Components built on Vue

2、使用props

如:element-plus的el-buton组件,可以通过 修改type属性的取值,来改变外观样式

html 复制代码
<el-button type="primary">按钮</el-button>
3、添加 class/style
复制代码
html 复制代码
<el-button type="success" class="mybutton" style="height: 250px;">按钮</el-button>
​
​
<style lang="css" scoped>
​
.mybutton{
    border-radius: 20px;
}
    
</style>

如果某个属性覆盖不了,就加属性的权重

.mybutton{ border-radius: 20px !important; }

4、查看元素,查询相关样式名,修改编译后的样式
html 复制代码
<el-button type="success" class="mybutton" style="height: 250px;">按钮</el-button>
​
<style lang="css" scoped>
​
.mybutton{
    border-radius: 20px;
}
​
.el-button{
    width: 600px;
}
    
</style>
5、样式穿透(覆盖ui组件库的样式名)
1)、 .a >>> .b { /* ... */ } 深度选择器;

如果实在不行的话,可以考虑给外面加一个容器

即: 自定义的样式名 >>> ui组件库的样式名

注意:这个写法sass和less不支持。

html 复制代码
<template>
    <el-table class="mytable" :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="Date" width="180" />
      <el-table-column prop="name" label="Name" width="180" />
      <el-table-column prop="address" label="Address" />
    </el-table>
  </template>
  
  <script  setup>
  const tableData = [
    {
      date: '2016-05-03',
      name: 'Tom',
      address: 'No. 189, Grove St, Los Angeles',
    },
    {
      date: '2016-05-02',
      name: 'Tom',
      address: 'No. 189, Grove St, Los Angeles',
    }
  ]
  </script>
​
<style scoped>
​
.mytable >>> .el-table_1_column_1  .cell{
    background-color: red;
}
​
</style>
2)、 /deep/ ui组件选择器 { }
html 复制代码
/deep/ .a{
​
    ***
​
}

sass和less的写法:

<style lang="scss" scoped>
.a{
  /deep/ .b { 
  /* ... */
 }
}
</style>

scoped 影响 (不加scoped,deep不生效)

3)、::v-deep ui组件选择器 { }
html 复制代码
::v-deep .a{
​
    ***
​
}

示例代码:

html 复制代码
<el-button type="success" class="mybutton" style="height: 250px;">按钮</el-button>
​
<style lang="css" scoped>
​
.mybutton{
    border-radius: 20px;
}
​
::v-deep .el-button span{
    width: 100px;
    height: 30px;
    background-color: aqua;
}
​
.el-button{
    width: 600px;
}
​
</style>


sass和less的写法:

<style lang="scss" scoped>
.a{
 ::v-deep .b { 
  /* ... */
 }
} 
</style>

/deep/在某些时候会报错,::v-deep更保险并且编译速度更快.

三、最后

由于UI组件库内部的样式我们不是完全清楚,所以,以上方法,都可以尝试,那种方式可以,就用那种。

其实,你可以打开chrome浏览器的元素(elements),查看不同组件的内部标签结构和样式,先做做尝试,再在代码中写,同时,也能够了解UI组件库内部的样式情况。

相关推荐
ZC跨境爬虫9 小时前
跟着 MDN 学CSS day_37:(从文档流到粘性定位的底层原理)
前端·javascript·css·ui·html
G_dou_10 小时前
Flutter三方库适配OpenHarmony【compass】罗盘 UI 项目完整实战
flutter·ui
ZC跨境爬虫11 小时前
跟着 MDN 学CSS day_40:(Flexbox实战技能测试)
前端·css·ui·html·tensorflow
ZC跨境爬虫11 小时前
跟着 MDN 学CSS day_36:(float、clear与BFC深度解析)
前端·javascript·css·ui·交互
豆豆13 小时前
2026实测:AI生成UI设计稿后,如何优雅集成到PageAdmin CMS?(附标签替换代码)
人工智能·ui·cms·建站系统·ai工具·ai建站
爱吃大芒果14 小时前
鸿蒙 ArkUI 架构蓝图:MoodLite 的 UI 渲染与数据逻辑解耦实践
ui·架构·harmonyos
Ulyanov15 小时前
深入QML滑块与进度控制:构建动态数据可视化界面:QML+PySide6现代开发入门(六)
开发语言·python·算法·ui·信息可视化·雷达电子对抗仿真
爱吃大芒果15 小时前
声明式 UI 进阶剖析:复杂长列表懒加载与视图模型 (ViewModel) 的内存优化策略
ui·华为·harmonyos
ZC跨境爬虫15 小时前
跟着 MDN 学CSS day_42:等分轨道、层叠放置与混合布局
前端·javascript·css·ui·html