HTML 中的中心表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13469383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
center table in HTML
提问by R.S
How can I center the tablewithin a divusing html?
如何table在div使用 html的范围内居中?
I have placed my content within a divtag and set the text-alignattribute to centerlike the following.
我已将我的内容放在一个div标签中,并将该text-align属性设置center为如下所示。
text-align: center;
This has not worked.
这没有奏效。
Below is my html.
下面是我的html。
<html>
<body>
<div style="text-align:center">
<p>
text test
</p>
<table border="1">
<tr>
<td>100</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>400</td>
<td>500</td>
<td>600</td>
</tr>
</table>
</div>
</body>
</html>
回答by Kalpesh Patel
Give width to table, and set margin auto horizontally.
为表格提供宽度,并水平设置边距自动。
table {
width:500px;
margin: 10px auto;
}
回答by Murali Murugesan
Try the below
试试下面的
<table style="margin:0px auto; width:500px">
回答by Sergio Ramirez
<div style="text-align:center">
<table style="display: inline-table;">
</table>
</div>
回答by svk
Edit the table tag like below
编辑表格标签,如下所示
<table border="1" align="center">
and then check.
然后检查。
回答by Maxim Pechenin
<html>
<body>
<div style="text-align:center">
<p>
text test
</p>
<table border="1" style="margin: 0 auto;">
<tr>
<td>100</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>400</td>
<td>500</td>
<td>600</td>
</tr>
</table>
</div>
</body>
</html>
回答by hellozimi
Instead of <div style="text-align:center">you could write <div style="width:600px;margin:0 auto">
Which gives you a div with the width of 600 pixels and centers the div in the parent element.
而不是<div style="text-align:center">您可以编写<div style="width:600px;margin:0 auto">
which 为您提供一个宽度为 600 像素的 div 并将 div 居中在父元素中。
回答by Cdeez
Add margin: 0px autoto your tabletag
添加margin: 0px auto到您的table标签
回答by Claudio Carta
you can put the style in the table
你可以把样式放在表格中
<table border="1" style="margin:0 auto;">
However I do suggest that you use a style sheet and not inline styles
但是我建议您使用样式表而不是内联样式

