在 HTML 中,可以使用三种方法使表格居中:使用 margin: auto; 属性使表格水平居中。使用 text-align: center; 属性在父元素内垂直居中表格。使用 align 属性指定表格相对于父元素的位置(弃用)。

如何让 HTML 表格居中
在 HTML 中,有几种方法可以使表格居中:
1. 使用 margin: auto;
margin: auto; 属性,可以让表格在水平方向上居中。示例:
立即学习“前端免费学习笔记(深入)”;
<code class="html"><table style="margin: auto;">
<tr>
<th>列 1</th>
<th>列 2</th>
</tr>
<tr>
<td>数据 1</td>
<td>数据 2</td>
</tr>
</table></code>2. 使用 text-align: center;
text-align: center; 属性,可以让表格在父元素内垂直居中。示例:
立即学习“前端免费学习笔记(深入)”;
<code class="html"><div style="text-align: center;">
<table border="1">
<tr>
<th>列 1</th>
<th>列 2</th>
</tr>
<tr>
<td>数据 1</td>
<td>数据 2</td>
</tr>
</table>
</div></code>3. 使用 align 属性
align 属性,可以指定表格相对于其父元素的位置。示例:
立即学习“前端免费学习笔记(深入)”;
<code class="html"><table align="center">
<tr>
<th>列 1</th>
<th>列 2</th>
</tr>
<tr>
<td>数据 1</td>
<td>数据 2</td>
</tr>
</table></code>注意:
align 属性在 HTML5 中已弃用,应优先使用 margin 或 text-align 属性。以上就是html中表格怎么居中的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号