html - td 的 width 属性不会覆盖 css width 属性

标签 html css width

我对此进行了很多搜索,但仍然无法理解这种行为的原因。我有一个指定了 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 但我需要在在一页上,我可以选择一种方式的样式属性,或者定义另一个样式类。

最佳答案

来自CSS 2.1 spec

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/

相关文章:

javascript - 如果在 jQuery 中 mouseleave 超过特定时间,则隐藏文本框

java - 通过浏览器使用 Java/C 访问 HTML TextField 组件

jquery - 导航栏中的子下拉列表

jquery - 覆盖 jquery 移动按钮边距

css - 对图像使用媒体查询

javascript - 自动将 CSS 属性调整到文本区域

html - 如何在 PRE 或类似的东西中显示原始 HTML 代码但不转义

css - Firefox 是否仍然需要 "::moz-selection"来更改 wordpress 中的背景文本颜色?

html - 如何在不使用javascript的情况下设置默认图像宽度的1/2

width - Flexslider 滑动宽度问题