Html 与表格单元格中的 align 属性等效的 HTML5 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16985799/
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
What is the HTML5 equivalent to the align attribute in table cells?
提问by Alan Piralla
I'm refactoring an old site, and that maze is full of tables. We're moving to HTML5 and I need to fix a table full of
我正在重构一个旧网站,那个迷宫里到处都是桌子。我们正在转向 HTML5,我需要修复一个充满
<td align="center">
code. I found a partial solution by creating a class
代码。我通过创建一个类找到了部分解决方案
.centered {
text-align: center;
}
and assigning it to every TD containing text.
But this is not working on images and some other elements.
并将其分配给每个包含文本的 TD。
但这不适用于图像和其他一些元素。
margin: auto;
won't work either.
What's the fastest way to center ALL content inside a TD?
也不会工作。
将所有内容集中在 TD 中的最快方法是什么?
回答by Bill Criswell
If they're block level elements they won't be affected by text-align: center;
. Someone may have set img { display: block; }
and that's throwing it out of whack. You can try:
如果它们是块级元素,它们将不受text-align: center;
. 有人可能已经设置了img { display: block; }
,这让它变得不正常。你可以试试:
td { text-align: center; }
td * { display: inline; }
and if it looks as desired you should definitelyreplace * with the desired elements like:
如果它看起来像你想要的,你绝对应该用所需的元素替换 * ,例如:
td img, td foo { display: inline; }
回答by user3596003
You can use inline css : <td style = "text-align: center;">
您可以使用内联 css : <td style = "text-align: center;">
回答by Jukka K. Korpela
According to the HTML5 CR, which requires continued support to “obsolete” features, too, the align=center
attribute is rather tricky. Rendering rules for tables say: td
elements with that attribute “are expected to center text within themselves, as if they had their 'text-align' property set to 'center' in a presentational hint, and to align descendants to the center.”
根据需要继续支持“过时”功能的 HTML5 CR,该align=center
属性也相当棘手。表格的渲染规则说:td
具有该属性的元素“应该将文本居中,就好像它们的 'text-align' 属性在显示提示中设置为 'center' 一样,并将后代对齐到中心。”
And aligning descendants is defined as so that a browser will “align only those descendants that have both their 'margin-left' and 'margin-right' properties computing to a value other than 'auto', that are over-constrained and that have one of those two margins with a used value forced to a greater value, and that do not themselves have an applicable align attribute. When multiple elements are to align a particular descendant, the most deeply nested such element is expected to override the others. Aligned elements are expected to be aligned by having the used values of their left and right margins be set accordingly.”
对齐后代被定义为这样浏览器将“仅对齐那些同时具有'左边距'和'右边距'属性计算为'自动'以外的值的后代,这些后代被过度约束并且具有使用值强制为更大值的两个边距之一,并且它们本身没有适用的 align 属性。当多个元素要对齐一个特定的后代时,嵌套最深的此类元素将覆盖其他元素。对齐的元素应该通过相应地设置其左右边距的使用值来对齐。”
So it really depends on the content.
所以这真的取决于内容。
回答by dafi hirdhananda
you can use this code as replacement for table align
您可以使用此代码代替表格对齐
table
{
margin:auto;
}
回答by Mario
Add this code into your StyleSheet:
将此代码添加到您的样式表中:
margin-top:80px;