方法一:全局修改(推荐)
/* 全局透明表格样式 */
.el-table,
.el-table__header-wrapper,
.el-table__body-wrapper,
.el-table__row {background-color: transparent !important;
}/* 可选:自定义表头和斑马纹行的透明度 */
.el-table__header th {background-color: rgba(245, 247, 250, 0.5) !important;
}.el-table__row--striped td {background-color: rgba(249, 250, 251, 0.3) !important;
}.el-table__row:hover td {background-color: rgba(230, 247, 255, 0.5) !important;
}
方法二:局部修改(按需使用)
/* 局部透明表格样式 */
.transparent-table .el-table,
.transparent-table .el-table__header-wrapper,
.transparent-table .el-table__body-wrapper,
.transparent-table .el-table__row {background-color: transparent !important;
}/* 可选样式... */
模板中使用
<div class="transparent-table"><el-table :data="tableData"><!-- 表格内容 --></el-table>
</div>
方法三:使用 CSS 变量(更灵活)
/* 自定义表格透明度 */
:root {--el-table-bg-color: transparent;--el-table-header-bg-color: rgba(245, 247, 250, 0.5);--el-table-row-hover-bg-color: rgba(230, 247, 255, 0.5);
}
注意事项
-
层级问题:如果表格在卡片或其他容器内,可能需要同时设置容器背景为透明:
.el-card {background-color: transparent !important; }
-
优先级:确保你的样式文件在 ElementPlus 样式之后加载,或使用
!important
提升优先级。 -
响应式设计:透明表格在深色或图案背景上效果更佳,需确保文字与背景有足够对比度。
完整示例
<template><div class="container"><el-card class="transparent-card"><el-table :data="tableData" stripe border><el-table-column prop="name" label="姓名"></el-table-column><el-table-column prop="age" label="年龄"></el-table-column><el-table-column prop="address" label="地址"></el-table-column></el-table></el-card></div>
</template><style scoped>
/* 透明表格样式 */
:deep(.el-table),
:deep(.el-table__header-wrapper),
:deep(.el-table__body-wrapper),
:deep(.el-table__row) {background-color: transparent !important;
}/* 可选:透明卡片 */
:deep(.el-card) {background-color: transparent !important;border: none !important;box-shadow: none !important;
}
</style>