为什么th
元素在浏览器默认值未指定text-align: center;
时会居中显示文字?
<!-- 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呈现:
第一个th
元素上的开发工具:
我在这里看到text-align: center;
,但th
中的文字显然是居中对齐的。这是Chrome的错误吗?
自Chrome v67起,th
现在按浏览器默认设置为text-align: -internal-center;
。
答案 0 :(得分:4)
我现在已经提到了old question提到的新答案。我希望它澄清了这种情况,但由于这个问题并不是真的重复,而是一个关于浏览器行为的具体问题,我在这里包含了相关的观点:
Chrome(和Firefox)以特殊方式实现th
元素,这与任何规范都不一致,包括HTML5中的“建议渲染”,即使经常导致相同的结果。实际上,th
没有浏览器默认样式表规则,但是当应用了正常的CSS级联并且它不会为text-align
的{{1}}产生任何值时,那么神奇的值{(1}}已分配给它(而不是根据规范的初始值th
)。这可能是center
简单居中的旧CSS前几天的延续。
如果您在开发工具“Computeted”选项卡中检查表中没有CSS设置的left
元素,则th
没有显示任何值,即使内容实际居中。如果您选中“显示继承的属性”(这意味着比名称更多),您可以看到th
。