element-ui 修改时间选择器的样式

如上篇文章Vue中scoped属性相关中提到的,一般项目中如果设置了scoped属性,可以通过>>>或者/deep/来修改其他第三方组件的样式。

 <el-date-picker
      v-model="valueTime"
       type="datetime"
       placeholder="选择日期时间">
  </el-date-picker>

但是elementUI的时间选择器el-date-picker是将元素直接挂载到页面的<body>中,而非自身元素下,所以使用/deep/穿透也是无法定位到元素的。

这个时候有两种思路,一是查询elementUI的配置项文档,看看能不能将时间选择器挂载到其元素自身下面,而非<body>中,这样就能通过>>>或者/deep/来修改其样式;二也同样是查询elementUI的配置项文档【官方文档很重要!】,看看是否能够其设置单独的样式,且不会影响项目中其他的时间选择器样式。

这里采用方法二,
step1 利用时间选择器的popper-class属性,给其设置样式。

 <el-date-picker
      v-model="valueTime"
       type="datetime"
       placeholder="选择日期时间"
       popper-class="date-style">
  </el-date-picker>

setp2 然后可以在项目的/assets/下写一个.less文件,在其中编写date-style对应的样式。【注意css层级关系】

.date-style.el-date-picker {
  width: 190px !important;
  height: 235px !important;

  // .el-picker-panel {
      line-height: 0px;
  // }
  .el-picker-panel__content {
      width: 155px !important;
      height: 180px !important;
  }
  table {
      font-size: 11px;
  }
  .el-date-picker__header {
      margin: 5px;
  }
  .el-date-picker__header-label {
      font-size: 11px;
  }
  .el-date-picker__header el-date-picker__header-label {
      font-size: 11px;
  }
  .el-date-table td, .el-date-table td div {
      height: 22px;
  }
  .el-month-table td .cell,
  .el-year-table td .cell,
  .el-date-table td .cell {
      width: 100%;
  }
}

step3 在项目中(比如说APP.vue)引入该样式文件

@import "./assets/style/common.less";

这样,样式就能顺利生效啦。同时该样式只跟类名date-style绑定,不会影响其他的时间选择器。

Tips:
利用多个<style>也能够达到效果,但是不推荐。

<style>
  .el-picker-panel  .el-picker-panel__footer  .el-button--text{
    display:none!important;
  }
</style>
//一定要把这个style样式写在scss样式前面
<style lang="scss" src="./style.scss" scoped></style>
GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:1 个月前 )
c345bb45 5 个月前
a07f3a59 * Update transition.md * Update table.md * Update transition.md * Update table.md * Update transition.md * Update table.md * Update table.md * Update transition.md * Update popover.md 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐