bootstrap表格内容居中技巧:使用text-center类实现水平居中(table-centered类名应用于th和td元素)。使用d-flex和align-items-center类垂直对齐内容,并设置div高度匹配行高(适用于固定行高的表格)。

Bootstrap 表格内容居中:提升表格可读性
在 Bootstrap 表格中,内容居中是改善表格可读性和美观性的有效方法。通过 CSS 样式,可以轻松地将表格内容水平居中或垂直居中。
水平居中
要水平居中表格内容,请使用 text-center 类:
<code class="css">.table-centered {
text-align: center;
}</code>然后将其应用于 <th> 和 <code><td> 元素:<pre class="brush:php;toolbar:false;"><code class="html"><table class="table-centered">
<tr>
<th>头 1</th>
<th>头 2</th>
...
</tr>
<tr>
<td>数据 1</td>
<td>数据 2</td>
...
</tr>
</table></code></pre><p><strong>垂直居中</strong></p><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/ai/766" title="Dora"><img
src="https://img.php.cn/upload/ai_manual/000/000/000/175679962280150.png" alt="Dora" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/ai/766" title="Dora">Dora</a>
<p>创建令人惊叹的3D动画网站,无需编写一行代码。</p>
</div>
<a href="/ai/766" title="Dora" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</div>
<p>垂直居中表格内容需要更多步骤:</p>
<ul><li>创建一个额外的 <code><div> 元素并将其包裹在 <code><th> 或 <code><td> 中。<li>为这个 <code><div> 元素应用 <code>d-flex 和 align-items-center 类,以垂直对其内容。
<div> 的高度以匹配行高。<p>例如:</p><pre class="brush:php;toolbar:false;"><code class="html"><table>
<tr>
<th>
<div class="d-flex align-items-center" style="height: 50px;">
头 1
</div>
</th>
...
</tr>
...
</table></code></pre><p>要自定义高度,只需调整 <code>style="height: 50px;" 中的值即可。
注意:
- 垂直居中仅适用于固定行高的表格。
- 如果表格内容包含元素(例如按钮或图像),请使用适当的 CSS 规则来使其水平或垂直居中。








