为什么HTML5不能验证但也没有提供替代方案?

5

我不明白HTML 5。一个验证器显示:

iframe元素上的marginwidth属性已过时。请改用CSS。
iframe元素上的marginheight属性已过时。请改用CSS。

但根据这个被接受的答案

在样式表中没有设置iframe标签的marginheight,marginwidth和frameborder属性的方法。

为什么要让我做一些根本不可能的事情呢?

要么将可行的方法放入规范中,要么提供真正的替代方案。他们似乎已经废弃了某些内容,但他们提供的替代方案并不起作用。

我错过了什么吗?


CSS的marginpadding有效吗? - SLaks
@TylerH:我想知道OP无法通过使用CSS属性margin、padding和border具体实现什么… - CBroe
@CBroe,你假设问题是“我怎样才能实现……”,但目前看来它似乎仍然只是“我能实现吗”。 - TylerH
@TylerH: “可以实现什么”背后的“什么”部分仍然缺失-这就是为什么我要求提供具体的问题描述。 - CBroe
@CBroe 我认为这很明显是隐含的。如果你觉得不够明显,你应该编辑问题并加以说明。 - TylerH
显示剩余3条评论
2个回答

3
HTML5规范确实描述了marginheight和marginwidth属性的工作原理。它所说的是:

For each property in the table below, given a body element, the first attribute that exists maps to the pixel length property on the body element. If none of the attributes for a property are found, or if the value of the attribute that was found cannot be parsed successfully, then a default value of 8px is expected to be used for that property instead.

Property        Source
'margin-top'    body element's marginheight attribute
                The body element's container frame element's marginheight attribute
                body element's topmargin attribute
'margin-right'  body element's marginwidth attribute
                The body element's container frame element's marginwidth attribute
                body element's rightmargin attribute
'margin-bottom' body element's marginheight attribute
                The body element's container frame element's marginheight attribute
                body element's bottommargin attribute
'margin-left'   body element's marginwidth attribute
                The body element's container frame element's marginwidth attribute
                body element's leftmargin attribute

If the body element's Document's browsing context is a nested browsing context, and the browsing context container of that nested browsing context is a frame or iframe element, then the container frame element of the body element is that frame or iframe element. Otherwise, there is no container frame element.

为了达到相同的效果,您必须在包含页面的body元素上设置CSS margin值,而不是iframe元素的CSS margin值。
规范接着解释了为什么marginwidth和marginheight可能不被支持(甚至不应该被支持)浏览器:
警告!上述要求意味着页面可以使用iframe更改另一个页面(包括来自另一个源的页面)的边距。这可能会带来安全风险,因为在某些情况下,它可能允许攻击者制造出一个页面以非作者预期的方式呈现,可能用于网络钓鱼或其他误导用户的目的。

0

我倾向于说,你应该始终对W3C上的信息保持一点怀疑。

如果你想把它归结为字面意思- 它们是正确的- 在CSS中没有直接的等效物,但是CSS提供了现有的替代方案(这就是为什么这两个属性都不再受支持,它们是多余的)。

marginheight 是顶部和底部的边距,所以只需使用 margin-topmargin-bottom...同样适用于 marginwidth,使用 margin-leftmargin-right(或在 margin 中全部指定)

至于 frameborder - 只需在CSS中设置iFrame的边框即可。

<iframe marginheight='10' marginwidth=20' frameborder='0'>

可以在HTML/CSS中完成:

<iframe>

iframe{
  margin:10px 20px;
  border:none;
}

当我使用margin:0;时,iframe仍然有边距。使用marginheight="0" marginwidth="0"可以解决问题,但不符合验证要求。 - Michael Rogers
您可能有冲突的CSS,或者周围内容的边距/填充。 - SW4
Chrome不显示冲突margin=0; padding=0;对iframe边距毫无影响。border=none;按预期工作,但无法去除边距。只有内联marginheight="0" marginwidth="0"才能达到预期效果。我认为tatty最后的陈述在我之前提到的问题上非常恰当。 - Michael Rogers
你是在设置CSS样式还是属性? - SW4
嗯,我只是给iframe添加了一个类和样式,然后在我的CSS中对该类进行了样式设置。 - Michael Rogers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接