vue3 el-table 表头以及表格文字居中

本文介绍了如何在ElementUI的表格中使用`:header-cell-style`和`:cell-style`属性实现表头和内容的居中对齐,并展示了如何进一步自定义表头样式,如背景颜色和高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使表头文字居中:

:header-cell-style="{ textAlign: 'center' }"

表头嘛就多个header

使表格内容文字居中:

:cell-style="{ textAlign: 'center' }"

对比效果:

修改后代码:

<el-card class="table-all">
            <el-row>
                <el-table :data="userData" class="userList-table" :header-cell-style="{ textAlign: 'center' }"
                          :cell-style="{ textAlign: 'center' }">
                    <el-table-column prop="username" label="姓名"></el-table-column>
                    <el-table-column prop="password" label="密码"></el-table-column>
                    <el-table-column prop="phone" label="电话号码"></el-table-column>
                    <el-table-column prop="email" label="邮箱"></el-table-column>
                    <el-table-column prop="sex" label="性别"></el-table-column>
                    <el-table-column prop="age" label="年龄"></el-table-column>
                    <el-table-column label="头像">
                        <template #default="scope">
                            <el-image :src="require(`@/assets/img/${scope.row.avatar}`)" :alt="picture"
                                      style="width: 60px; height: 60px;"></el-image>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <el-button type="primary" class="detail-btn">编辑</el-button>
                        <el-button type="danger" class="bigDelete-btn">删除</el-button>
                    </el-table-column>
                </el-table>
            </el-row>
            <el-row style="margin-left: 10%">
                <el-pagination @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pagesize"
                               :total="userData.length">
                </el-pagination>
            </el-row>
        </el-card>

表头及表格内容未居中前:

表头及表格内容居中后:

是不是看起来更整齐了!

也可以通过:header-cell-style改其它格式,比如修改表头背景,表头高度:

:header-cell-style="{ textAlign: 'center' ,height: '70px', background: 'ghostwhite'} "


其它属性还有比如:

1、border:给表格加边框
2、height:可实现固定表头的表格,不需要额外的代码
3、fixed:接受 Boolean 值left或者 right,表示左边固定还是右边固定
4、show-overflow-tooltip:当内容过长被隐藏时显示 tooltip(文字提示)
5、row-class-name:改变某行的背景色


希望以上内容帮助到你!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值