input button的行高在FF下的显示问题

本文详细介绍了Firefox和Opera浏览器在处理按钮行高时出现的bug,即无法正确应用设置的行高样式。通过Firebug工具的辅助,开发人员发现了Firefox将按钮文本的行高强行设置为默认值normal的问题。文章最后提供了替代解决方案,以确保按钮的外观符合预期。

最近国外有开发者发现Firefox和Opera的一个BUG:无法改变按钮里文字的行高(包括input type=”sumbit”和HTML的button标签)。举个例子,对一个按钮使用以下样式:

input#button {
border: 2px solid #06f;
color: #06f;
background-color: #6cf;
font: bold 12px Arial, Helvetica, sans-serif;
line-height: 50px;
}

按照常理,这个样式将为按钮设置50px的行高,并使文本垂直居中。因为这个样式在Chrome、Safari和IE8里的表现都一致,唯独在Firefox和Opera里并没有表现出50px的行高,具体效果如下图。

button-sizes-by-browser

是什么原因引起了这个问题?

在Firebug的帮助下,该开发人员发现虽然自己已经把行高设为50px,但Firefox依然使用15px的行高。

firebug-firefox-line-height

该开发人员认为是Firefox强行限制了按钮文本的行高,于是编写了以下的CSS用于验证。

button, input[type="reset"], input[type="button"], input[type="submit"] {
line-height:normal !important;
}

该开发人员发现,Firefox会私自把按钮内文本的行高设置为normal,这个值在Firefox里默认为15px,但各个浏览器对normal的值并没有一个统一的标准。最后这个开发人员放弃了通过行高把按钮撑开并使文本居中的方法,而采用了以下的样式,解决了Firefox的按钮行高问题。

input#button {
border: 2px solid #06f;
color: #06f;
background-color: #6cf;
font: bold 12px Arial, Helvetica, sans-serif;
padding: 18px 6px;
}


这并不是最好的办法,但也算是达到了所需的效果。

本文案例出自The Firefox Input Button Line-Height Bug


<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" /> ``` 使用虚拟表格组件处理大数据量
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值