【问题标题】:default text-align for th in Google ChromeGoogle Chrome 中 th 的默认文本对齐
【发布时间】:2018-07-14 11:33:32
【问题描述】:

问题

当浏览器默认没有指定text-align: center; 时,为什么th 元素的文本居中?

系统设置

  • 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 渲染:

第一个 th 元素上的开发工具:

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

相关问题

<th> text-align compatibility

更新

从 Chrome v67 开始,th 现在默认具有指定的 text-align: -internal-center;

【问题讨论】:

  • 我猜&lt;th&gt; 默认情况下会以这种方式出现,因为(据我所知)根据 HTML 规范不需要它是其他任何东西。 编辑: 事实上,this W3 page 建议&lt;th&gt; 的默认样式应该是{ font-weight: bolder; text-align: center }
  • 我知道HTML recommends th elements appear centered and bold。我不知道的是 Google Chrome 是如何在 CSS 中没有 text-align: center; 的情况下呈现的。但是我确实看到了font-weight: bold;,如我的屏幕截图所示。

标签: html css google-chrome


【解决方案1】:

我现在对提到的old question 发布了一个新答案。我希望它能澄清情况,但由于这个问题实际上并不是重复的,而是关于浏览器行为的具体问题,所以我在此处包含相关点:

Chrome(和 Firefox)以特殊方式实现 th 元素,这与任何规范都不一致,包括 HTML5 中的“建议渲染”,尽管它经常导致相同的结果.实际上,th 没有浏览器默认样式表规则,但是当应用了正常的 CSS 级联并且它不会为 th 产生任何 text-align 的值时,然后神奇地分配了值 center它(而不是按照规范的 left 的初始值)。这可能是旧 CSS 前时代的遗留物,当时 th 只是居中。

如果您在开发工具的“已计算”选项卡中检查没有 CSS 设置的表格中的 th 元素,即使内容实际上居中,text-align 也不会显示任何值。如果您选中“显示继承的属性”(这意味着比名称所说的更多),您可以看到text-align: center

【讨论】:

  • 这似乎是一种非常奇怪的做法。为什么 Chrome 不像 caption 元素那样只使用普通的 CSS? (text-align: -webkit-center;)
  • Chrome 并不孤单; Firefox 做同样的事情。
猜你喜欢
  • 1970-01-01
  • 2011-09-15
  • 2011-12-10
  • 2011-12-09
  • 2017-09-08
  • 2015-06-04
  • 2016-12-21
  • 2011-07-02
相关资源
最近更新 更多