第三方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组件库内部的样式情况。

相关推荐
寒雪谷1 天前
用户登陆UI
开发语言·javascript·ui·harmonyos·鸿蒙
尤老师FPGA2 天前
使用ZYNQ芯片和LVGL框架实现用户高刷新UI设计系列教程(第八讲)
ui
架构文摘JGWZ2 天前
Apache Kafka UI :一款功能丰富且美观的 Kafka 开源管理平台!!
ui·kafka·开源·apache·工具
qq_205279053 天前
UNITY 屏幕UI自适应
ui
好名字08213 天前
vue2改变el-message字体、图标尺寸样式(vue2,element-ui)
开发语言·javascript·ui
UI设计兰亭妙微3 天前
探索 UI 设计服务:提升用户体验的关键力量
ui·ux
GDAL4 天前
掌握 Photoshop 脚本 Folder 对象:批量处理文件的利器
ui·photoshop
ZoeLandia4 天前
Element UI 设置 el-table-column 宽度 width 为百分比无效
前端·ui·element-ui
sunly_5 天前
Flutter:图片在弹窗外部的UI布局
flutter·ui
cjxIt6 天前
element-ui 中的 select 组件如何 remote-method 函数中传参
javascript·vue.js·ui