在Google Chrome中默认为text-align

时间:2015-02-03 19:13:42

标签: html css google-chrome

问题

为什么th元素在浏览器默认值未指定text-align: center;时会居中显示文字?

系统设置

  • MacBook Pro,2014年中期
  • OS X v 10.10.2
  • Google Chrome v 40.0.2214.94(64位)
  • 截至发布时所有软件的最新版本

示例代码

<!-- no external or internal CSS applied.
     browser default styles only. -->
<table width="500" border="1"> <!-- attributes for debugging only. never use these attributes in real code (use CSS instead). -->
    <tr><th> table heading </th><th> centered text     </th></tr>
    <tr><td> table data    </td><td> left-aligned text </td></tr>
</table>

屏幕截图

HTML呈现: screen shot of Google Chrome's rendering of a sample table. the text in the th elements is aligned center while the text in the td elements is aligned left.

第一个th元素上的开发工具: screen shot of Google Chrome Developer Tools showing browser-default styles applied to a th element. th{font-weight:bold;} td,th{display:table-cell;vertical-align:inherit;}/*inherited from table*/table{border-collapse:separate;border-spacing:2px;border-color:gray;}

我在这里看到text-align: center;,但th中的文字显然是居中对齐的。这是Chrome的错误吗?

相关问题

<th> text-align compatibility

更新

自Chrome v67起,th现在按浏览器默认设置为text-align: -internal-center;

1 个答案:

答案 0 :(得分:4)

我现在已经提到了old question提到的新答案。我希望它澄清了这种情况,但由于这个问题并不是真的重复,而是一个关于浏览器行为的具体问题,我在这里包含了相关的观点:

Chrome(和Firefox)以特殊方式实现th元素,这与任何规范都不一致,包括HTML5中的“建议渲染”,即使经常导致相同的结果。实际上,th没有浏览器默认样式表规则,但是当应用了正常的CSS级联并且它不会为text-align的{​​{1}}产生任何值时,那么神奇的值{(1}}已分配给它(而不是根据规范的初始值th)。这可能是center简单居中的旧CSS前几天的延续。

如果您在开发工具“Computeted”选项卡中检查表中没有CSS设置的left元素,则th没有显示任何值,即使内容实际居中。如果您选中“显示继承的属性”(这意味着比名称更多),您可以看到th