为什么不允许设置表格列的样式?
W3 指定表格列只允许使用四个 CSS 规则(其中 元素) - 边框、背景、宽度和可见性。
有谁知道这个决定背后的原因? 如果可以有边框和背景,为什么不能有字体和颜色呢?
W3 specifies that only four CSS rules are allowed for table columns (with the <col>
element) - border, background, width and visibility.
Does anyone know the reasons behind this decision? If you can have borders and backgrounds, why not fonts and colours?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Ian Hixie 在此详细解释:为什么只有四个属性适用于表列的奥秘。 相关报价:
Ian Hixie explains in detail here: The mystery of why only four properties apply to table columns. Relevant quote:
只是基于我有限的理解,在黑暗中进行了疯狂的尝试:
我认为通过与列相关的元素进行样式设置是受到限制的,因为尽管
和
代表列单元格,它实际上并不包含它们(它们实际上包含在
中)。 随之而来的是优先级、特异性和级联问题(因为级联只能在包含的/容器元素之间完成) - 当
和
< 的样式规则发生冲突时/code> (这将是多重继承层次结构中的同一级别)发生 - 单元格实际应该使用哪个?
至于为什么允许特定的少数样式属性:不知道。
Just a wild stab in the dark based upon my limited understanding:
I think styling via column related elements is restricted because although
<col>
and<colgroup>
represent columns of cells, it does not actually contain them (they're actually contained by the<tr>
s). With this comes issues of precedence and specificity and cascading (since cascading can only be done between contained/ container elements) - when conflicting style rules from the<tr>
and<col>
(which would be the same level in a multiple inheritance hierarchy) occur - which should the cell actually use?As to why that particular handful of style attributes is permitted though: no idea.
一个词:暧昧。 单元格必须是行的子单元格; 否则它就不是一张桌子了。 但没有可以下降的柱子。 使用
colspan
意味着一个单元格可以位于两列中。 与其试图想出一些令人困惑的解决办法,为什么不让开发人员在每个第 n 个单元格上放置一个class
呢?如果您仔细查看链接到的规范,您将看到解决歧义的尝试。
width
属性指定最小值;背景
位于行和单元格之后;border
引用了“冲突解决算法”。 甚至存在边界算法的唯一原因是因为可以合理地理解谁应该“获胜”(有关详细信息,请参阅算法)。 但是您能想象尝试找出哪种颜色
或字体
应该“获胜”吗?One word: ambiguity. The cells have to be children of rows; it wouldn't be a table otherwise. But there is no column to descend from. Using
colspan
means that one cell could be in two columns. Rather than trying to come up with some confusing way out, why not just let the developer place aclass
on every nth cell?If you look closely at the spec to which you link, you will see attempts at ambiguity resolution. The
width
property specifies a minimum; thebackground
takes a backseat to the row and cell; andborder
references a "conflict resolution algorithm". The only reason there is even an algorithm forborder
is because it is reasonably understood who should "win" (see the algorithm for details). But could you imagine trying to figure out whichcolor
orfont
should "win"?可能是因为表中的每一行不一定必须显示列的单元格(例如,由于 colspan)。 该单元格应该从哪一列继承其样式? 只是一个猜测。
Possibly because each row in the table does not necessarily have to display a cell for your column (e.g. because of a colspan). Which column should that cell inherit it's style from? Just a guess.