css precedence of standalone attributes like cellpadding

不打扰是莪最后的温柔 提交于 2019-12-11 08:16:42

问题


I know that inline styles via the 'style' attribute take precedence over those specified in an external css file. But what about stand-alone attributes like 'cellpadding'? An example line would be like this:

<table cellpadding="4" class="list">

回答1:


6.4.4 Precedence of non-CSS presentational hints in the CSS 2.1 spec says: “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.”

So any relevant setting in any author style sheet being applied overrides the effect of cellspacing. The attribute sets the padding (in each direction) for each cell of the table to the specified value in pixels. So if you set e.g. for a particular cell padding-right: 0, it will have that right padding and 4px padding in other directions.




回答2:


'Inline styles' - applied to the element itself - have the highest 'specificity', which is the points system used to calculate which rules override others (high specificity overrides low).

I would imagine that cellpadding="4" counts as an inline style, and that the only way you could override it with an !important declaration in your stylesheet.

Here's some reading on specificity: http://reference.sitepoint.com/css/specificity



来源:https://stackoverflow.com/questions/9369271/css-precedence-of-standalone-attributes-like-cellpadding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!