解决Antd Table组件表头不对齐的问题
#代码知识 发布时间: 2026-01-12
背景:

在使用Antd的table组件时,由于表头参数过多,于是设置了scroll属性,在其超出一定宽度后进行滚动
但是在添加了该属性之后,经常会出现表头不对齐的问题:
针对该问题Google 了一下解决方案,但大多不是很完善,为解决问题。现整理下完整的解决方案:
1、对表格的每一行 【columns】设置width属性(留出一行进行宽度自适应);
2、scroll属性中的x选择一个合适的值(或者直接设为 max-content);
如果以上两步仍解决不了对齐问题的话,请继续第三步操作
3、对table的每一个td 添加 className=“word-wrap”,并设置对应样式
(因为td内部的内容在出现连续字母或数字的时候不会主动换行),导致td内部宽度撑开,与th宽度不一致
.word-wrap {
word-break: break-all;
}
以上操作完成之后可能还是有问题(请检查下是不是表头中内容的宽度默认被撑开了),然后重新调整下column中的width即可
近期在开发的过程中,另发现了一种非常有效得解决方案,特与大家分享:
在对columns的每一项设置了宽度后,如果还是有错位问题的话,可以尝试在columns的末位push一个空的column进行占位,这个空的column不用设置宽度,任其自适应。
注意:该column的title需要设置为空字符串,避免在界面上将其渲染出来
补充知识:vue实现超过两行显示展开收起
基于vue-cli2,sass,vant(ui组件):https://youzan.github.io/vant/#/zh-CN/home
具体代码如下:
<template>
<div>
<div class="group">
<div class="text more" ref="more">
占位
</div>
<div class="list" v-for="(item, index) in historyList" :key="index">
<van-row>
<van-col span="12">{{ item.version }}</van-col>
<van-col class="t_right l_text" span="12">{{ item.time }}</van-col>
</van-row>
<div class="l_title">{{ item.title }}</div>
<div
class="text"
ref="textContainer"
:class="{ retract: item.status }"
:style="{ 'max-height': item.status ? textHeight : '' }"
>
{{ item.content }}
</div>
<span
v-if="item.status !== null"
class="link"
@click="more(index)"
>{{ item.status ? "展开" : "收起" }}</span
>
</div>
</div>
</div>
</template>
<script>
export default {
data () {
return {
textHeight: '',
historyList: [
{
version: '7.1.4',
title: '本次更新',
content:
'-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐;-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐',
time: '2周前'
},
{
version: '7.1.4',
title: '本次更新',
content:
'-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐',
time: '5周前'
},
{
version: '7.1.4',
title: '本次更新',
content:
'-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐;-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐',
time: '6周前'
},
{
version: '7.1.4',
title: '本次更新',
content:
'-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐',
time: '9周前'
}
]
}
},
mounted () {
this.historyList.forEach((ele, index) => {
this.$set(
this.historyList,
index,
Object.assign({}, ele, { status: null })
)
})
// DOM 加载完执行
this.$nextTick(() => {
this.calculateText()
//console.log(this.historyList)
})
window.onresize = () => {
this.historyList.forEach((ele, index) => {
this.$set(
this.historyList,
index,
Object.assign({}, ele, { status: null })
)
})
setTimeout(() => {
this.calculateText()
}, 0)
}
},
methods: {
// 计算文字 显示展开 收起
calculateText () {
// 获取一行文字的height 计算当前文字比较列表文字
let oneHeight = this.$refs.more.scrollHeight
let twoHeight = oneHeight * 2 || 40
this.textHeight = `${twoHeight}px`
let txtDom = this.$refs.textContainer
for (let i = 0; i < txtDom.length; i++) {
let curHeight = txtDom[i].offsetHeight
if (curHeight > twoHeight) {
this.$set(
this.historyList,
i,
Object.assign({}, this.historyList[i], { status: true })
)
} else {
this.$set(
this.historyList,
i,
Object.assign({}, this.historyList[i], { status: null })
)
}
}
},
more (index) {
this.historyList[index].status = !this.historyList[index].status
}
}
}
</script>
<style lang="scss" scoped>
.group {
.list {
padding: 5px 0;
border-bottom: 1px solid #eaeaea;
}
.text {
position: relative;
color: #000;
font-size: 14px;
}
.more {
visibility: hidden;
}
.link {
font-size: 12px;
color: #2d95fe;
}
.retract {
position: relative;
overflow: hidden;
}
.retract:after {
content: "...";
position: absolute;
bottom: 0;
right: 2px;
width: 25px;
padding-left: 25px;
background: linear-gradient(to right, transparent, #fff 45%);
}
}
</style>
以上这篇解决Antd Table组件表头不对齐的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
代码知识SEO上一篇 : Python通过yagmail实现发送邮件代码解析
下一篇 : 关于SpringBoot 打包成的可执行jar不能被其他项目依赖的问题
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!