Bootstrap 表格内容居中:提升表格可读性
在 Bootstrap 表格中,内容居中是改善表格可读性和美观性的有效方法。通过 CSS 样式,可以轻松地将表格内容水平居中或垂直居中。
水平居中
要水平居中表格内容,请使用 text-center 类:
1
2
3
.table-centered {
text-align: center;
}
然后将其应用于
和 元素:1
2
3
4
5
6
7
8
9
10
11
12
<table class="table-centered">
<tr>
<th>头 1</th>
<th>头 2</th>
...
</tr>
<tr>
<td>数据 1</td>
<td>数据 2</td>
...
</tr>
</table>
垂直居中
垂直居中表格内容需要更多步骤:
创建一个额外的例如:
1
2
3
4
5
6
7
8
9
10
11
<table>
<tr>
<th>
<div class="d-flex align-items-center" style="height: 50px;">
头 1
</div>
</th>
...
</tr>
...
</table>
要自定义高度,只需调整 style="height: 50px;" 中的值即可。
注意:
垂直居中仅适用于固定行高的表格。 如果表格内容包含元素(例如按钮或图像),请使用适当的 CSS 规则来使其水平或垂直居中。