使表头文字居中:
: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:改变某行的背景色
希望以上内容帮助到你!