<think>我们继续使用Element Plus表格,目标是将行高设置为35px,同时保持原有样式(如字体大小、内边距、按钮样式等)不变。关键点在于只调整行高而不影响其他视觉样式。
实现思路:
1. 设置表格行(包括表头行)的高度为35px
2. 调整单元格内边距,确保内容在35px高度内垂直居中
3. 特别处理内部元素(如按钮、标签、头像等)的样式,使其在缩小行高后仍然保持原有外观
4. 保留表格的原有视觉特性(如斑马纹、悬停效果、表头样式等)
具体步骤:
在全局样式文件或组件内的样式中添加以下CSS规则,并确保使用!important覆盖Element Plus的默认样式。
注意:为了不影响其他表格,我们给目标表格添加一个自定义类名(例如:compact-table-35),然后基于这个类名编写样式。</think># Element Plus 表格行高设为35px且保持样式不变
要实现行高设置为35px但保持原有样式不变,需要精细调整表格的CSS样式,确保内容在更大的行高中仍然保持原有比例和视觉效果。以下是完整的解决方案:
## 核心实现代码
```css
/* 全局样式文件(如 src/assets/styles/global.css) */
.el-table .el-table__row {
height: 35px !important;
}
.el-table .el-table__header-row {
height: 35px !important;
}
.el-table .el-table__cell {
padding: 0 !important;
}
.el-table .el-table__cell .cell {
height: 35px;
line-height: 35px;
display: flex;
align-items: center;
padding: 0 10px;
font-size: 14px; /* 保持原有字体大小 */
}
/* 确保按钮样式不变 */
.el-table .el-button {
padding: 6px 12px;
height: auto;
line-height: normal;
}
/* 确保标签样式不变 */
.el-table .el-tag {
height: auto;
line-height: normal;
padding: 3px 8px;
}
/* 确保头像样式不变 */
.el-table .el-avatar {
width: 28px !important;
height: 28px !important;
line-height: 28px !important;
}
```
## 完整组件实现
```vue
<template>
<div class="table-container">
<h2>行高35px的表格(保持原有样式)</h2>
<div class="controls">
<el-button @click="addRow">添加新行</el-button>
<el-button @click="toggleStripe">{{ striped ? '取消斑马纹' : '启用斑马纹' }}</el-button>
<el-button @click="toggleHover">{{ hoverEffect ? '取消悬停效果' : '启用悬停效果' }}</el-button>
</div>
<el-table
:data="tableData"
:stripe="striped"
:highlight-current-row="hoverEffect"
border
style="width: 100%"
class="custom-height-table"
row-key="id"
>
<el-table-column type="index" label="序号" width="70" align="center" />
<el-table-column label="用户" width="200">
<template #default="scope">
<div class="user-info">
<el-avatar :size="28" :src="scope.row.avatar" />
<div class="user-details">
<div class="name">{{ scope.row.name }}</div>
<div class="email">{{ scope.row.email }}</div>
</div>
</div>
</template>
</el-table-column>
<el-table-column prop="department" label="部门" width="150" />
<el-table-column label="状态" width="120" align="center">
<template #default="scope">
<el-tag
:type="scope.row.status === 'active' ? 'success' :
scope.row.status === 'vacation' ? 'warning' : 'danger'"
>
{{ statusText[scope.row.status] }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="项目进度" width="200">
<template #default="scope">
<el-progress
:percentage="scope.row.progress"
:stroke-width="12"
:color="progressColor(scope.row.progress)"
/>
<div class="progress-text">{{ scope.row.progress }}%</div>
</template>
</el-table-column>
<el-table-column prop="lastLogin" label="最后登录" width="180" />
<el-table-column label="操作" width="180" fixed="right" align="center">
<template #default="scope">
<el-button size="small" @click="editItem(scope.row)">编辑</el-button>
<el-button
size="small"
type="danger"
@click="deleteItem(scope.row.id)"
>删除</el-button>
</template>
</el-table-column>
</el-table>
<div class="pagination">
<el-pagination
v-model:current-page="currentPage"
v-model:page-size="pageSize"
:page-sizes="[5, 10, 20]"
layout="total, sizes, prev, pager, next, jumper"
:total="tableData.length"
/>
</div>
</div>
</template>
<script setup>
import { ref, computed } from 'vue';
// 状态文本映射
const statusText = {
active: '在职',
vacation: '休假',
leave: '离职'
};
// 表格数据
const tableData = ref([
{
id: 1,
name: '张三',
email: 'zhangsan@company.com',
department: '技术部',
status: 'active',
progress: 85,
lastLogin: '2023-06-15 09:30',
avatar: 'https://avatars.dicebear.com/api/human/zhangsan.svg'
},
{
id: 2,
name: '李四',
email: 'lisi@company.com',
department: '设计部',
status: 'vacation',
progress: 45,
lastLogin: '2023-06-14 14:20',
avatar: 'https://avatars.dicebear.com/api/human/lisi.svg'
},
{
id: 3,
name: '王五',
email: 'wangwu@company.com',
department: '市场部',
status: 'active',
progress: 70,
lastLogin: '2023-06-15 11:15',
avatar: 'https://avatars.dicebear.com/api/human/wangwu.svg'
},
{
id: 4,
name: '赵六',
email: 'zhaoliu@company.com',
department: '财务部',
status: 'leave',
progress: 0,
lastLogin: '2023-05-20 16:45',
avatar: 'https://avatars.dicebear.com/api/human/zhaoliu.svg'
},
{
id: 5,
name: '钱七',
email: 'qianqi@company.com',
department: '人力资源部',
status: 'active',
progress: 95,
lastLogin: '2023-06-15 08:45',
avatar: 'https://avatars.dicebear.com/api/human/qianqi.svg'
}
]);
// 分页相关
const currentPage = ref(1);
const pageSize = ref(5);
// 表格显示控制
const striped = ref(true);
const hoverEffect = ref(true);
// 进度条颜色
const progressColor = (percentage) => {
if (percentage >= 90) return '#67C23A';
if (percentage >= 70) return '#409EFF';
if (percentage >= 50) return '#E6A23C';
return '#F56C6C';
};
// 添加新行
const addRow = () => {
const newId = tableData.value.length + 1;
tableData.value.push({
id: newId,
name: `新员工${newId}`,
email: `new${newId}@company.com`,
department: '待分配',
status: 'active',
progress: 0,
lastLogin: '从未登录',
avatar: 'https://avatars.dicebear.com/api/human/neutral.svg'
});
};
// 编辑项目
const editItem = (item) => {
console.log('编辑项目:', item);
};
// 删除项目
const deleteItem = (id) => {
const index = tableData.value.findIndex(item => item.id === id);
if (index !== -1) {
tableData.value.splice(index, 1);
}
};
// 切换斑马纹
const toggleStripe = () => {
striped.value = !striped.value;
};
// 切换悬停效果
const toggleHover = () => {
hoverEffect.value = !hoverEffect.value;
};
</script>
<style scoped>
.table-container {
max-width: 1200px;
margin: 20px auto;
padding: 25px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}
h2 {
color: #303133;
font-size: 22px;
margin-bottom: 25px;
padding-bottom: 15px;
border-bottom: 1px solid #ebeef5;
}
.controls {
margin-bottom: 20px;
display: flex;
gap: 15px;
}
.user-info {
display: flex;
align-items: center;
gap: 12px;
}
.user-details {
display: flex;
flex-direction: column;
}
.name {
font-weight: 500;
}
.email {
font-size: 12px;
color: #909399;
}
.progress-text {
margin-top: 4px;
font-size: 12px;
color: #606266;
text-align: center;
}
.pagination {
display: flex;
justify-content: flex-end;
margin-top: 25px;
padding-top: 20px;
border-top: 1px solid #ebeef5;
}
</style>
<style>
/* 行高35px的关键样式 */
.custom-height-table .el-table__row {
height: 35px !important;
}
.custom-height-table .el-table__header-row {
height: 35px !important;
}
.custom-height-table .el-table__cell {
padding: 0 !important;
}
.custom-height-table .el-table__cell .cell {
height: 35px;
line-height: 35px;
display: flex;
align-items: center;
padding: 0 10px;
font-size: 14px;
}
/* 按钮样式调整 */
.custom-height-table .el-button {
padding: 6px 12px;
height: auto;
line-height: normal;
font-size: 13px;
}
/* 标签样式调整 */
.custom-height-table .el-tag {
height: auto;
line-height: normal;
padding: 3px 8px;
font-size: 13px;
}
/* 头像样式调整 */
.custom-height-table .el-avatar {
width: 28px !important;
height: 28px !important;
line-height: 28px !important;
}
/* 进度条样式调整 */
.custom-height-table .el-progress {
margin-top: 4px;
}
.custom-height-table .el-progress-bar__outer {
height: 12px !important;
}
/* 表头样式保持不变 */
.custom-height-table .el-table__header .el-table__cell {
background-color: #f5f7fa;
font-weight: bold;
}
/* 斑马纹样式保持不变 */
.custom-height-table .el-table--striped .el-table__body tr.el-table__row--striped td {
background-color: #fafafa;
}
/* 悬停效果保持不变 */
.custom-height-table .el-table__body tr:hover > td {
background-color: #f5f7ff !important;
}
/* 当前行高亮效果 */
.custom-height-table .el-table__body tr.current-row > td {
background-color: #ecf5ff !important;
}
</style>
```
## 关键实现说明
### 1. 行高设置核心代码
```css
.custom-height-table .el-table__row {
height: 35px !important;
}
.custom-height-table .el-table__header-row {
height: 35px !important;
}
```
这两行代码将表格行和表头行的高度都设置为35px
### 2. 单元格内容垂直居中
```css
.custom-height-table .el-table__cell .cell {
height: 35px;
line-height: 35px;
display: flex;
align-items: center;
padding: 0 10px;
}
```
使用Flex布局确保单元格内容在35px高度内完美垂直居中
### 3. 组件尺寸比例调整
```css
/* 按钮 */
.custom-height-table .el-button {
padding: 6px 12px;
font-size: 13px;
}
/* 标签 */
.custom-height-table .el-tag {
padding: 3px 8px;
font-size: 13px;
}
/* 头像 */
.custom-height-table .el-avatar {
width: 28px !important;
height: 28px !important;
}
```
按比例调整内部组件尺寸,使其在35px行高中保持协调
### 4. 保留原有视觉特性
```css
/* 表头样式 */
.custom-height-table .el-table__header .el-table__cell {
background-color: #f5f7fa;
}
/* 斑马纹样式 */
.custom-height-table .el-table--striped .el-table__body tr.el-table__row--striped td {
background-color: #fafafa;
}
/* 悬停效果 */
.custom-height-table .el-table__body tr:hover > td {
background-color: #f5f7ff !important;
}
```
这些样式确保表格的视觉特性保持不变
## 高级技巧:动态行高调整
```vue
<template>
<div class="controls">
<el-slider
v-model="rowHeight"
:min="30"
:max="60"
:step="5"
show-input
input-size="small"
/>
<span class="slider-label">当前行高: {{ rowHeight }}px</span>
</div>
<el-table :data="tableData" :style="tableStyle">
<!-- 表格列定义 -->
</el-table>
</template>
<script setup>
import { ref, computed } from 'vue';
const rowHeight = ref(35);
const tableStyle = computed(() => {
return {
'--row-height': `${rowHeight.value}px`,
'--cell-padding': `${Math.max(8, rowHeight.value / 5)}px`
};
});
</script>
<style>
.dynamic-table .el-table__row {
height: var(--row-height) !important;
}
.dynamic-table .el-table__header-row {
height: var(--row-height) !important;
}
.dynamic-table .el-table__cell .cell {
height: var(--row-height);
line-height: var(--row-height);
padding: 0 var(--cell-padding) !important;
}
</style>
```
## 响应式设计考虑
```css
/* 不同屏幕尺寸下的行高优化 */
@media (max-width: 992px) {
.custom-height-table .el-table__row,
.custom-height-table .el-table__header-row {
height: 40px !important;
}
.custom-height-table .el-table__cell .cell {
height: 40px;
line-height: 40px;
padding: 0 12px;
font-size: 13px;
}
}
@media (max-width: 768px) {
.custom-height-table .el-table__row,
.custom-height-table .el-table__header-row {
height: 45px !important;
}
.custom-height-table .el-table__cell .cell {
height: 45px;
line-height: 45px;
padding: 0 15px;
font-size: 12px;
}
.custom-height-table .el-button {
padding: 4px 8px;
font-size: 12px;
}
}
```
## 最佳实践建议
1. **字体大小比例**:
- 35px行高适合14px字体大小
- 调整比例:行高 = 字体大小 × 2.5
2. **内边距优化**:
```css
.custom-height-table .el-table__cell .cell {
padding: 0 calc(35px / 3.5); /* 约10px */
}
```
3. **复杂内容处理**:
- 多行文本使用弹性高度
```css
.custom-height-table .multi-line .cell {
height: auto;
line-height: 1.5;
padding: 5px 10px;
align-items: flex-start;
}
```
4. **性能优化**:
```vue
<el-table-v2
:row-height="35"
:estimated-row-height="35"
:data="largeData"
/>
```
使用虚拟表格组件处理大数据量