不冷博客

ElementUI边框样式自定义

今天需要实现自定义的表格边框样式,但是官方并没有例子说明,不过提供的有api去操作。在 Element Plus<el-table> 组件中,表头的边框样式默认是由 border 这个属性控制的,但是如果你需要自定义表头的边框样式,可以使用 header-row-style 或者 header-cell-style 结合 CSS 来实现。


方法 1:使用 header-cell-style

如果你只想修改 表头单元格 的边框,可以使用 header-cell-style

<el-table 
  :data="tableData"
  border
  :header-cell-style="{ border: '2px solid #ff0000' }"
  size="large">
</el-table>

说明


方法 2:使用 header-row-style

如果你想给 整行表头 统一设置边框:

<el-table 
  :data="tableData"
  border
  :header-row-style="{ borderBottom: '3px solid #75a2f3' }"
  size="large">
</el-table>

说明


方法 3:使用 deep 方式自定义表头样式

如果你想更精细地控制表头的样式(比如边框颜色、粗细),可以用 deep 选择器:

<template>
  <el-table :data="tableData" border size="large">
    <el-table-column prop="name" label="姓名"></el-table-column>
    <el-table-column prop="age" label="年龄"></el-table-column>
  </el-table>
</template>

<style scoped>
/* 自定义表头的边框 */
:deep(.el-table__header th) {
  border: 2px solid #ff0000 !important; /* 红色边框 */
}
</style>

说明


总结

方式适用范围适用情况
header-cell-style单个 th需要给每个表头单元格加边框
header-row-style整个表头 tr统一给整个表头加边框
:deep(.el-table__header th)深度选择 th更精准控制样式,适用于复杂自定义需求

如果你只是要简单加个边框,用 方法 1 最方便。如果需要复杂的样式控制,推荐 方法 3

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »