Html 如何在中心对齐 td 元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10675427/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:33:09  来源:igfitidea点击:

How to align td elements in center

htmlcss

提问by user1184100

I have created a simple table and want to align the td elements in center but align:center in css doesn't seem to work

我创建了一个简单的表格,并希望将 td 元素居中对齐,但 align:center 在 css 中似乎不起作用

.cTable td{
align:center;
}

<table border='1' id='mytable' class="cTable">
<tbody>
<tr><th>Claim ID</th><th>Status</th></tr> 
<tr><td align="center">22</td><td>333</td></tr>    
<tr><td>22</td><td>333</td></tr>
<tr><td>22</td><td>333</td></tr>
</tbody>
</table> 

回答by Ja?ck

It should be text-align, not align

应该是text-align,不是align

https://developer.mozilla.org/en/CSS/text-align

https://developer.mozilla.org/en/CSS/text-align

回答by GuruMatics

What worked for me is the following (in view of the confusion in other answers):

对我有用的是以下内容(鉴于其他答案中的混淆):

<td style="text-align:center;">
    <input type="radio" name="ageneral" value="male">
</td>

The proposed solution (text-align) works but must be used in a style attribute.

建议的解决方案 ( text-align) 有效,但必须在样式属性中使用。

回答by Seb

margin:auto;text-align, if this won't work - try adding display:block;and set there width:200px;(in case your TD is too small).

margin:auto;文本对齐,如果这不起作用 - 尝试添加display:block;并设置width:200px;(以防您的 TD 太小)。

回答by Y2H

I personally didn't find any of these answers helpful. What worked in my case was giving the element float:noneand position:relative. After that the element centered itself in the <td>.

我个人认为这些答案中的任何一个都没有帮助。在我的情况下有效的是给元素float:noneposition:relative. 之后元素将自己居中于<td>.

回答by Ashwini Bj

Give a style inside the tdelement or in your scssfile, like this:

td元素或scss文件中指定样式,如下所示:

vertical-align: 
    middle;