我对此进行了很多搜索,但仍然无法理解这种行为的原因。我有一个指定了 width 属性的 td 元素,并且还在 css 类中指定了宽度。但是css class中的width是有反射(reflect)的,元素的width属性不是优先级更高吗?
示例代码:
<table class="sample">
<tr>
<td width="70%" class="cell">First cell</td>
<td class="cell">Second cell</td>
</tr>
</table>
CSS
table.sample {
border: 1px solid red;
width: 100%;
}
td.cell {
border: 1px solid black;
width: 40%;
}
我知道 width 属性已被弃用,不建议使用,我试图理解这种行为,因为我必须解决类似的问题,我有很多页面使用样式 td.cell 但我需要在在一页上,我可以选择一种方式的样式属性,或者定义另一个样式类。
最佳答案
6.4.4 Precedence of non-CSS presentational hints
The UA may choose to honor presentational attributes in an HTML source document. If so, these attributes are translated to the corresponding CSS rules with specificity equal to 0, and are treated as if they were inserted at the start of the author style sheet. They may therefore be overridden by subsequent style sheet rules. In a transition phase, this policy will make it easier for stylistic attributes to coexist with style sheets.
因此,CSS 样式的排名始终高于 HTML 中的表示属性。
关于html - td 的 width 属性不会覆盖 css width 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16829889/